001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.test.server.appserver;
006:
007: import org.apache.commons.io.FileUtils;
008: import org.codehaus.cargo.util.internal.log.AbstractLogger;
009: import org.codehaus.cargo.util.log.LogLevel;
010:
011: import com.tc.test.Handle;
012: import com.tc.text.Banner;
013:
014: import java.io.File;
015: import java.io.FileOutputStream;
016: import java.io.IOException;
017: import java.text.DateFormat;
018: import java.text.SimpleDateFormat;
019: import java.util.Date;
020: import java.util.Properties;
021:
022: /**
023: * Handles installation and dynamic port assignment for an appserver instance.
024: */
025: public abstract class AbstractAppServer implements AppServer {
026:
027: private final AppServerStartupEnvironment installation;
028: private File instance;
029:
030: public AbstractAppServer(AppServerInstallation installation) {
031: this .installation = (AppServerStartupEnvironment) installation;
032: }
033:
034: protected final synchronized File createInstance(
035: AppServerParameters params) throws Exception {
036: instance = new File(installation.sandboxDirectory()
037: + File.separator + params.instanceName());
038: if (instance.exists()) {
039: try {
040: FileUtils.deleteDirectory(instance);
041: } catch (IOException e) {
042: Banner.warnBanner(instance + " exists.\n"
043: + Handle.getJavaProcessFileHandles(instance));
044: instance = new File(instance.getAbsolutePath()
045: + "_"
046: + new SimpleDateFormat("yyyyMMdd-HHmmss")
047: .format(new Date()));
048: }
049: }
050: instance.mkdir();
051: return instance;
052: }
053:
054: /**
055: * The specific appserver implementation uses the server install directory to locate the immutable appserver
056: * installation files used to start running instances in the working directory.
057: */
058: protected final File serverInstallDirectory() {
059: if (!installation.isRepoInstall())
060: return installation.serverInstallDirectory();
061: return new File(installation.serverInstallDirectory()
062: + File.separator + serverType() + "-" + majorVersion()
063: + "." + minorVersion());
064:
065: }
066:
067: /**
068: * The server name is used to create a parent directory for the server install directory which the appserver
069: * implementation refers to as it's home directory.
070: */
071: protected final String serverType() {
072: return installation.serverType();
073: }
074:
075: protected final String majorVersion() {
076: return installation.majorVersion();
077: }
078:
079: protected final String minorVersion() {
080: return installation.minorVersion();
081: }
082:
083: protected final File sandboxDirectory() {
084: return installation.sandboxDirectory();
085: }
086:
087: /**
088: * Implementing classes call this method to assign a series of properties to be available as system properties to the
089: * appserver's JVM. Properties are optionally set by calling {@link StandardAppServerParameters}, passing a
090: * <tt>Properties</tt> object to it's overloaded constructor. These instance specific properties are written to disk
091: * and read by the appserver JVM. Two properties are always set by default: {@link AppServerConstants.APP_INSTANCE}
092: * and {@link AppServerConstants.APP_PORT}.
093: */
094: protected final void setProperties(AppServerParameters params,
095: int port, File instance) {
096: Properties props = params.properties();
097: if (props == null)
098: props = new Properties();
099: props.setProperty(AppServerConstants.APP_INSTANCE, params
100: .instanceName());
101: props.setProperty(AppServerConstants.APP_PORT, Integer
102: .toString(port));
103: File propsFile = new File(instance + ".properties");
104: FileOutputStream fos = null;
105: try {
106: propsFile.createNewFile();
107: fos = new FileOutputStream(propsFile, false);
108: props.store(fos, "Available Application System Properties");
109: } catch (IOException ioe) {
110: throw new RuntimeException(
111: "Unable to write properties file to: " + propsFile,
112: ioe);
113: } finally {
114: if (fos != null) {
115: try {
116: fos.close();
117: } catch (IOException ioe) {
118: throw new RuntimeException(
119: "Unable to write properties file to: "
120: + propsFile, ioe);
121: }
122: }
123: }
124: }
125:
126: public final static class ConsoleLogger extends AbstractLogger {
127:
128: private static final DateFormat FORMAT = new SimpleDateFormat(
129: "MM-dd-yyyy HH:mm:ss.SSS");
130:
131: private final String instance;
132:
133: public ConsoleLogger(String instance) {
134: this .instance = instance;
135: }
136:
137: protected void doLog(LogLevel level, String message,
138: String category) {
139: String msg = "[" + FORMAT.format(new Date()) + "]" + "["
140: + level.getLevel() + "][" + instance + "] "
141: + message;
142: System.out.println(msg);
143: }
144: }
145: }
|