KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.tc.config.Directories;
8 import com.tc.exception.ImplementMe;
9 import com.tc.test.TempDirectoryHelper;
10 import com.tc.test.TestConfigObject;
11 import com.tc.test.server.appserver.jboss4x.JBoss4xAppServerFactory;
12 import com.tc.test.server.appserver.tomcat5x.Tomcat5xAppServerFactory;
13 import com.tc.test.server.appserver.war.War;
14 import com.tc.test.server.appserver.wasce1x.Wasce1xAppServerFactory;
15 import com.tc.test.server.appserver.weblogic8x.Weblogic8xAppServerFactory;
16 import com.tc.test.server.tcconfig.StandardTerracottaAppServerConfig;
17 import com.tc.util.Assert;
18 import com.tc.util.io.FileUtils;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 /**
26  * This factory is meant to be used by the general public. The properties file supplied in obtaining an instance may be
27  * blank, which will fall on the default appserver implementations. This class should be the only point for reference in
28  * creating a working appserver. Never instantiate specific appserver classes explicitly.
29  */

30 public abstract class NewAppServerFactory {
31
32   public static final String JavaDoc WEBLOGIC = "weblogic";
33   public static final String JavaDoc JBOSS = "jboss";
34   public static final String JavaDoc TOMCAT = "tomcat";
35   public static final String JavaDoc WASCE = "wasce";
36
37   protected final TestConfigObject config;
38   private boolean licenseIsSet;
39
40   protected NewAppServerFactory(ProtectedKey protectedKey, TestConfigObject config) {
41     Assert.assertNotNull(protectedKey);
42     Assert.assertNotNull(config);
43     copyLicenseIfAvailable();
44     this.config = config;
45   }
46
47   public abstract AppServerParameters createParameters(String JavaDoc instanceName, Properties JavaDoc props);
48
49   public AppServerParameters createParameters(String JavaDoc instanceName) {
50     return createParameters(instanceName, new Properties JavaDoc());
51   }
52
53   public abstract AppServer createAppServer(AppServerInstallation installation);
54
55   public abstract AppServerInstallation createInstallation(URL JavaDoc host, File JavaDoc serverDir, File JavaDoc workingDir) throws Exception JavaDoc;
56
57   public abstract AppServerInstallation createInstallation(File JavaDoc home, File JavaDoc workingDir) throws Exception JavaDoc;
58
59   public abstract War createWar(String JavaDoc appName);
60
61   public abstract StandardTerracottaAppServerConfig createTcConfig(File JavaDoc baseDir);
62
63   public static final NewAppServerFactory createFactoryFromProperties(TestConfigObject config) {
64     Assert.assertNotNull(config);
65     String JavaDoc factoryName = config.appserverFactoryName();
66     String JavaDoc majorVersion = config.appserverMajorVersion();
67
68     if (TOMCAT.equals(factoryName)) {
69       if ("5".equals(majorVersion)) return new Tomcat5xAppServerFactory(new ProtectedKey(), config);
70     } else if (WEBLOGIC.equals(factoryName)) {
71       if ("8".equals(majorVersion)) return new Weblogic8xAppServerFactory(new ProtectedKey(), config);
72     } else if (WASCE.equals(factoryName)) {
73       if ("1".equals(majorVersion)) return new Wasce1xAppServerFactory(new ProtectedKey(), config);
74     } else if (JBOSS.equals(factoryName)) {
75       if ("4".equals(majorVersion)) return new JBoss4xAppServerFactory(new ProtectedKey(), config);
76     }
77     
78     throw new ImplementMe("App server named '" + factoryName + "' with major version " + majorVersion
79                           + " is not yet supported.");
80   }
81
82   private final synchronized void copyLicenseIfAvailable() {
83     if (this.licenseIsSet) return;
84
85     try {
86       File JavaDoc licenseFile = new File JavaDoc(Directories.getLicenseLocation(), "license.lic");
87
88       if (!licenseFile.exists()) {
89         this.licenseIsSet = true;
90         return;
91       }
92
93       TempDirectoryHelper helper = new TempDirectoryHelper(getClass());
94       File JavaDoc toDir = helper.getDirectory();
95       File JavaDoc toFile = new File JavaDoc(toDir, licenseFile.getName());
96       FileUtils.copyFile(licenseFile, toFile);
97       this.licenseIsSet = true;
98     } catch (IOException JavaDoc ioe) {
99       throw new RuntimeException JavaDoc("Can't set up license file", ioe);
100     }
101   }
102
103   protected static class ProtectedKey {
104     // ensure that only this class may invoke it's children
105
}
106 }
107
Popular Tags