1 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 ; 12 import java.io.FileOutputStream ; 13 import java.io.IOException ; 14 import java.text.DateFormat ; 15 import java.text.SimpleDateFormat ; 16 import java.util.Date ; 17 import java.util.Properties ; 18 19 22 public abstract class AbstractAppServer implements AppServer { 23 24 private final AppServerStartupEnvironment installation; 25 private File instance; 26 27 public AbstractAppServer(AppServerInstallation installation) { 28 this.installation = (AppServerStartupEnvironment) installation; 29 } 30 31 protected final synchronized File createInstance(AppServerParameters params) throws Exception { 32 instance = new File (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 45 protected final File serverInstallDirectory() { 46 if (!installation.isRepoInstall()) return installation.serverInstallDirectory(); 47 return new File (installation.serverInstallDirectory() + File.separator + serverType() + "-" + majorVersion() + "." 48 + minorVersion()); 49 50 } 51 52 56 protected final String serverType() { 57 return installation.serverType(); 58 } 59 60 protected final String majorVersion() { 61 return installation.majorVersion(); 62 } 63 64 protected final String minorVersion() { 65 return installation.minorVersion(); 66 } 67 68 protected final File serverBaseDirectory() { 69 return installation.serverBaseDir(); 70 } 71 72 protected final File sandboxDirectory() { 73 return installation.sandboxDirectory(); 74 } 75 76 80 protected void initiateStartupAppender(File sandboxDir) throws Exception { 81 } 83 84 91 protected final void setProperties(AppServerParameters params, int port, File instance) { 92 Properties props = params.properties(); 93 if (props == null) props = new Properties (); 94 props.setProperty(AppServerConstants.APP_INSTANCE, params.instanceName()); 95 props.setProperty(AppServerConstants.APP_PORT, Integer.toString(port)); 96 File propsFile = new File (instance + ".properties"); 97 FileOutputStream fos = null; 98 try { 99 propsFile.createNewFile(); 100 fos = new FileOutputStream (propsFile, false); 101 props.store(fos, "Available Application System Properties"); 102 } catch (IOException ioe) { 103 throw new RuntimeException ("Unable to write properties file to: " + propsFile, ioe); 104 } finally { 105 if (fos != null) { 106 try { 107 fos.close(); 108 } catch (IOException ioe) { 109 throw new RuntimeException ("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 FORMAT = new SimpleDateFormat ("MM-dd-yyyy HH:mm:ss.SSS"); 118 119 private final String instance; 120 private LogLevel logLevel; 121 122 public ConsoleLogger(String instance) { 123 this.instance = instance; 124 this.logLevel = LogLevel.INFO; 125 } 126 127 protected void doLog(LogLevel level, String message, String category) { 128 String msg = "[" + FORMAT.format(new Date ()) + "]" + "[" + level.getLevel() + "][" + instance + "] " + message; 129 System.out.println(msg); 130 } 131 132 public void warn(String message, String category) { 133 log("warn", message, category); 134 } 135 136 private void log(String severity, String message, String category) { 137 System.out.println(FORMAT.format(new Date ()) + " [" + 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 |