KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > server > appserver > AbstractAppServer


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.test.server.appserver;
6
7 import org.apache.commons.io.FileUtils;
8 import org.codehaus.cargo.util.internal.log.AbstractLogger;
9 import org.codehaus.cargo.util.log.LogLevel;
10
11 import java.io.File JavaDoc;
12 import java.io.FileOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.text.DateFormat JavaDoc;
15 import java.text.SimpleDateFormat JavaDoc;
16 import java.util.Date JavaDoc;
17 import java.util.Properties JavaDoc;
18
19 /**
20  * Handles installation and dynamic port assignment for an appserver instance.
21  */

22 public abstract class AbstractAppServer implements AppServer {
23
24   private final AppServerStartupEnvironment installation;
25   private File JavaDoc instance;
26
27   public AbstractAppServer(AppServerInstallation installation) {
28     this.installation = (AppServerStartupEnvironment) installation;
29   }
30
31   protected final synchronized File JavaDoc createInstance(AppServerParameters params) throws Exception JavaDoc {
32     instance = new File JavaDoc(installation.sandboxDirectory() + File.separator + params.instanceName());
33     if (instance.exists()) {
34       FileUtils.deleteDirectory(instance);
35     }
36     instance.mkdir();
37     initiateStartupAppender(installation.sandboxDirectory());
38     return instance;
39   }
40
41   /**
42    * The specific appserver implementation uses the server install directory to locate the immutable appserver
43    * installation files used to start running instances in the working directory.
44    */

45   protected final File JavaDoc serverInstallDirectory() {
46     if (!installation.isRepoInstall()) return installation.serverInstallDirectory();
47     return new File JavaDoc(installation.serverInstallDirectory() + File.separator + serverType() + "-" + majorVersion() + "."
48                     + minorVersion());
49
50   }
51
52   /**
53    * The server name is used to create a parent directory for the server install directory which the appserver
54    * implementation refers to as it's home directory.
55    */

56   protected final String JavaDoc serverType() {
57     return installation.serverType();
58   }
59
60   protected final String JavaDoc majorVersion() {
61     return installation.majorVersion();
62   }
63
64   protected final String JavaDoc minorVersion() {
65     return installation.minorVersion();
66   }
67
68   protected final File JavaDoc serverBaseDirectory() {
69     return installation.serverBaseDir();
70   }
71
72   protected final File JavaDoc sandboxDirectory() {
73     return installation.sandboxDirectory();
74   }
75
76   /**
77    * Subclasses may provide implementations to allow preprocessing to take place in the appserver child process JVM.
78    * Appenders must implement <tt>StartupAppender</tt>.
79    */

80   protected void initiateStartupAppender(File JavaDoc sandboxDir) throws Exception JavaDoc {
81     // not implemented
82
}
83
84   /**
85    * Implementing classes call this method to assign a series of properties to be available as system properties to the
86    * appserver's JVM. Properties are optionally set by calling {@link StandardAppServerParameters}, passing a
87    * <tt>Properties</tt> object to it's overloaded constructor. These instance specific properties are written to disk
88    * and read by the appserver JVM. Two properties are always set by default: {@link AppServerConstants.APP_INSTANCE}
89    * and {@link AppServerConstants.APP_PORT}.
90    */

91   protected final void setProperties(AppServerParameters params, int port, File JavaDoc instance) {
92     Properties JavaDoc props = params.properties();
93     if (props == null) props = new Properties JavaDoc();
94     props.setProperty(AppServerConstants.APP_INSTANCE, params.instanceName());
95     props.setProperty(AppServerConstants.APP_PORT, Integer.toString(port));
96     File JavaDoc propsFile = new File JavaDoc(instance + ".properties");
97     FileOutputStream JavaDoc fos = null;
98     try {
99       propsFile.createNewFile();
100       fos = new FileOutputStream JavaDoc(propsFile, false);
101       props.store(fos, "Available Application System Properties");
102     } catch (IOException JavaDoc ioe) {
103       throw new RuntimeException JavaDoc("Unable to write properties file to: " + propsFile, ioe);
104     } finally {
105       if (fos != null) {
106         try {
107           fos.close();
108         } catch (IOException JavaDoc ioe) {
109           throw new RuntimeException JavaDoc("Unable to write properties file to: " + propsFile, ioe);
110         }
111       }
112     }
113   }
114
115   public final static class ConsoleLogger extends AbstractLogger {
116
117     private static final DateFormat JavaDoc FORMAT = new SimpleDateFormat JavaDoc("MM-dd-yyyy HH:mm:ss.SSS");
118
119     private final String JavaDoc instance;
120     private LogLevel logLevel;
121
122     public ConsoleLogger(String JavaDoc instance) {
123       this.instance = instance;
124       this.logLevel = LogLevel.INFO;
125     }
126
127     protected void doLog(LogLevel level, String JavaDoc message, String JavaDoc category) {
128       String JavaDoc msg = "[" + FORMAT.format(new Date JavaDoc()) + "]" + "[" + level.getLevel() + "][" + instance + "] " + message;
129       System.out.println(msg);
130     }
131
132     public void warn(String JavaDoc message, String JavaDoc category) {
133       log("warn", message, category);
134     }
135
136     private void log(String JavaDoc severity, String JavaDoc message, String JavaDoc category) {
137       System.out.println(FORMAT.format(new Date JavaDoc()) + " [" + severity + "][" + category + "][" + instance + "] "
138                          + message);
139     }
140
141     public LogLevel getLevel() {
142       return logLevel;
143     }
144
145     public void setLevel(LogLevel level) {
146       logLevel = level;
147     }
148   }
149 }
150
Popular Tags