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 com.tc.config.Directories;
008: import com.tc.exception.ImplementMe;
009: import com.tc.test.TempDirectoryHelper;
010: import com.tc.test.TestConfigObject;
011: import com.tc.test.server.appserver.glassfishv1.GlassfishV1AppServerFactory;
012: import com.tc.test.server.appserver.jboss3x.JBoss3xAppServerFactory;
013: import com.tc.test.server.appserver.jboss4x.JBoss4xAppServerFactory;
014: import com.tc.test.server.appserver.jetty6x.Jetty6xAppServerFactory;
015: import com.tc.test.server.appserver.tomcat5x.Tomcat5xAppServerFactory;
016: import com.tc.test.server.appserver.was6x.Was6xAppServerFactory;
017: import com.tc.test.server.appserver.wasce1x.Wasce1xAppServerFactory;
018: import com.tc.test.server.appserver.weblogic8x.Weblogic8xAppServerFactory;
019: import com.tc.test.server.appserver.weblogic9x.Weblogic9xAppServerFactory;
020: import com.tc.util.Assert;
021: import com.tc.util.io.TCFileUtils;
022:
023: import java.io.File;
024: import java.io.IOException;
025: import java.util.Properties;
026:
027: /**
028: * This factory is meant to be used by the general public. The properties file supplied in obtaining an instance may be
029: * blank, which will fall on the default appserver implementations. This class should be the only point for reference in
030: * creating a working appserver. Never instantiate specific appserver classes explicitly.
031: */
032: public abstract class AppServerFactory {
033:
034: public static final int WEBLOGIC = 0;
035: public static final int JBOSS = 1;
036: public static final int TOMCAT = 2;
037: public static final int WASCE = 3;
038: public static final int GLASSFISH = 4;
039: public static final int JETTY = 5;
040: public static final int WEBSPHERE = 6;
041:
042: protected final TestConfigObject config;
043: private boolean licenseIsSet;
044:
045: protected AppServerFactory(ProtectedKey protectedKey,
046: TestConfigObject config) {
047: Assert.assertNotNull(protectedKey);
048: Assert.assertNotNull(config);
049: copyLicenseIfAvailable();
050: this .config = config;
051: }
052:
053: public abstract AppServerParameters createParameters(
054: String instanceName, Properties props);
055:
056: public AppServerParameters createParameters(String instanceName) {
057: return createParameters(instanceName, new Properties());
058: }
059:
060: public abstract AppServer createAppServer(
061: AppServerInstallation installation);
062:
063: public abstract AppServerInstallation createInstallation(File home,
064: File workingDir) throws Exception;
065:
066: public static final AppServerFactory createFactoryFromProperties(
067: TestConfigObject config) {
068: Assert.assertNotNull(config);
069: String factoryName = config.appserverFactoryName();
070: String majorVersion = config.appserverMajorVersion();
071: int appId = getAppServerId(factoryName);
072: switch (appId) {
073: case TOMCAT:
074: if ("5".equals(majorVersion) || "6".equals(majorVersion))
075: return new Tomcat5xAppServerFactory(new ProtectedKey(),
076: config);
077: case WEBLOGIC:
078: if ("8".equals(majorVersion))
079: return new Weblogic8xAppServerFactory(
080: new ProtectedKey(), config);
081: if ("9".equals(majorVersion))
082: return new Weblogic9xAppServerFactory(
083: new ProtectedKey(), config);
084: case WASCE:
085: if ("1".equals(majorVersion))
086: return new Wasce1xAppServerFactory(new ProtectedKey(),
087: config);
088: case JBOSS:
089: if ("3".equals(majorVersion))
090: return new JBoss3xAppServerFactory(new ProtectedKey(),
091: config);
092: if ("4".equals(majorVersion))
093: return new JBoss4xAppServerFactory(new ProtectedKey(),
094: config);
095: case GLASSFISH:
096: if ("v1".equals(majorVersion))
097: return new GlassfishV1AppServerFactory(
098: new ProtectedKey(), config);
099: case JETTY:
100: if ("6".equals(majorVersion))
101: return new Jetty6xAppServerFactory(new ProtectedKey(),
102: config);
103: case WEBSPHERE:
104: if ("6".equals(majorVersion))
105: return new Was6xAppServerFactory(new ProtectedKey(),
106: config);
107: }
108:
109: throw new ImplementMe("App server named '" + factoryName
110: + "' with major version " + majorVersion
111: + " is not yet supported.");
112: }
113:
114: private final synchronized void copyLicenseIfAvailable() {
115: if (this .licenseIsSet)
116: return;
117:
118: try {
119: File licenseFile = new File(Directories
120: .getLicenseLocation(), "license.lic");
121:
122: if (!licenseFile.exists()) {
123: this .licenseIsSet = true;
124: return;
125: }
126:
127: TempDirectoryHelper helper = new TempDirectoryHelper(
128: getClass());
129: File toDir = helper.getDirectory();
130: File toFile = new File(toDir, licenseFile.getName());
131: TCFileUtils.copyFile(licenseFile, toFile);
132: this .licenseIsSet = true;
133: } catch (IOException ioe) {
134: throw new RuntimeException("Can't set up license file", ioe);
135: }
136: }
137:
138: protected static class ProtectedKey {
139: // ensure that only this class may invoke it's children
140: }
141:
142: public static int getAppServerId(String appserverName) {
143: if (appserverName.equals("weblogic")) {
144: return WEBLOGIC;
145: } else if (appserverName.equals("jboss")) {
146: return JBOSS;
147: } else if (appserverName.equals("tomcat")) {
148: return TOMCAT;
149: } else if (appserverName.equals("wasce")) {
150: return WASCE;
151: } else if (appserverName.equals("glassfish")) {
152: return GLASSFISH;
153: } else if (appserverName.equals("jetty")) {
154: return JETTY;
155: } else if (appserverName.equals("websphere")) {
156: return WEBSPHERE;
157: } else {
158: throw new RuntimeException("App server [" + appserverName
159: + "] is not yet defined!");
160: }
161: }
162:
163: public static int getCurrentAppServerId() {
164: return getAppServerId(TestConfigObject.getInstance()
165: .appserverFactoryName());
166: }
167:
168: public static String getCurrentAppServerMajorVersion() {
169: return TestConfigObject.getInstance().appserverMajorVersion();
170: }
171: }
|