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.glassfishv1;
006:
007: import org.apache.commons.io.FileUtils;
008: import org.apache.tools.ant.DefaultLogger;
009: import org.apache.tools.ant.Project;
010: import org.apache.tools.ant.ProjectHelper;
011: import org.w3c.dom.Document;
012: import org.w3c.dom.Element;
013: import org.w3c.dom.Node;
014: import org.w3c.dom.NodeList;
015:
016: import com.tc.test.TestConfigObject;
017: import com.tc.test.server.appserver.AppServer;
018: import com.tc.test.server.appserver.AppServerFactory;
019: import com.tc.test.server.appserver.AppServerInstallation;
020: import com.tc.test.server.appserver.AppServerParameters;
021: import com.tc.test.server.appserver.StandardAppServerParameters;
022:
023: import java.io.File;
024: import java.io.IOException;
025: import java.io.StringWriter;
026: import java.util.Properties;
027:
028: import javax.xml.parsers.DocumentBuilder;
029: import javax.xml.parsers.DocumentBuilderFactory;
030: import javax.xml.transform.Transformer;
031: import javax.xml.transform.TransformerFactory;
032: import javax.xml.transform.dom.DOMSource;
033: import javax.xml.transform.stream.StreamResult;
034:
035: /**
036: * This class creates specific implementations of return values for the given methods. To obtain an instance you must
037: * call {@link NewAppServerFactory.createFactoryFromProperties()}.
038: */
039: public final class GlassfishV1AppServerFactory extends AppServerFactory {
040:
041: // This class may only be instantiated by its parent which contains the ProtectedKey
042: public GlassfishV1AppServerFactory(ProtectedKey protectedKey,
043: TestConfigObject config) {
044: super (protectedKey, config);
045: }
046:
047: public AppServerParameters createParameters(String instanceName,
048: Properties props) {
049: return new StandardAppServerParameters(instanceName, props);
050: }
051:
052: public AppServer createAppServer(AppServerInstallation installation) {
053: return new GlassfishV1AppServer(
054: (GlassfishV1AppServerInstallation) installation);
055: }
056:
057: private void doSetup(GlassfishV1AppServerInstallation install)
058: throws IOException, Exception {
059: File installDir = install.serverInstallDirectory();
060: File configDir = new File(installDir, "config");
061: File domainsDir = new File(installDir, "domains");
062:
063: // These directories should be cleaned for each run since there is VM specific information baked into files here
064: FileUtils.deleteDirectory(configDir);
065: FileUtils.deleteDirectory(domainsDir);
066:
067: // execute the equivalent of "ant -f setup.xml"
068: File antScript = new File(installDir, "setup.xml");
069: if (!antScript.isFile() || !antScript.canRead()) {
070: throw new RuntimeException("missing ant script "
071: + antScript.getAbsolutePath());
072: }
073: modifySetupXml(antScript);
074:
075: Project p = new Project();
076: DefaultLogger consoleLogger = new DefaultLogger();
077: consoleLogger.setErrorPrintStream(System.err);
078: consoleLogger.setOutputPrintStream(System.out);
079: consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
080: // consoleLogger.setMessageOutputLevel(Project.MSG_DEBUG);
081: p.addBuildListener(consoleLogger);
082:
083: p.setUserProperty("ant.file", antScript.getAbsolutePath());
084: p.init();
085: ProjectHelper helper = ProjectHelper.getProjectHelper();
086: p.addReference("ant.projectHelper", helper);
087: helper.parse(p, antScript);
088:
089: p.executeTarget(p.getDefaultTarget());
090: }
091:
092: private void modifySetupXml(File antScript) throws Exception {
093: // make the "create.domain" target a NOOP in glassfish setup
094: // Do this for two reasons, (1) It crashes on windows with long pathnames, (2) speed things up a little
095:
096: DocumentBuilderFactory factory = DocumentBuilderFactory
097: .newInstance();
098: DocumentBuilder builder = factory.newDocumentBuilder();
099: Document document = builder.parse(antScript);
100:
101: NodeList list = document.getElementsByTagName("target");
102:
103: int numTargets = list.getLength();
104:
105: Node createDomainTarget = null;
106: for (int i = 0; i < numTargets; i++) {
107: Node inspect = list.item(i);
108: Node nameAttr = inspect.getAttributes()
109: .getNamedItem("name");
110: if (nameAttr != null) {
111: if ("create.domain".equals(nameAttr.getNodeValue())) {
112: createDomainTarget = inspect;
113: break;
114: }
115: }
116: }
117:
118: if (createDomainTarget == null) {
119: throw new RuntimeException("Cannot find target in "
120: + antScript.getAbsolutePath());
121: }
122:
123: while (createDomainTarget.getChildNodes().getLength() > 0) {
124: createDomainTarget.removeChild(createDomainTarget
125: .getChildNodes().item(0));
126: }
127:
128: // Also workaround bug with long pathnames (https://glassfish.dev.java.net/issues/show_bug.cgi?id=2849)
129: NodeList chmodTasks = document.getElementsByTagName("chmod");
130: for (int i = 0; i < chmodTasks.getLength(); i++) {
131: Element chmod = (Element) chmodTasks.item(i);
132: chmod.setAttribute("parallel", "false");
133: }
134:
135: TransformerFactory transformerFactory = TransformerFactory
136: .newInstance();
137: Transformer transformer = transformerFactory.newTransformer();
138:
139: StringWriter sw = new StringWriter();
140: transformer.transform(new DOMSource(document),
141: new StreamResult(sw));
142:
143: FileUtils.writeStringToFile(antScript, sw.toString(), "UTF-8");
144: }
145:
146: public AppServerInstallation createInstallation(File home,
147: File workingDir) throws Exception {
148: GlassfishV1AppServerInstallation install = new GlassfishV1AppServerInstallation(
149: home, workingDir, config.appserverMajorVersion(),
150: config.appserverMinorVersion());
151: doSetup(install);
152: return install;
153: }
154: }
|