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.weblogic9x;
006:
007: import org.apache.commons.io.IOUtils;
008: import org.apache.tools.ant.BuildListener;
009: import org.apache.tools.ant.taskdefs.Java;
010: import org.apache.tools.ant.types.Path;
011: import org.codehaus.cargo.container.InstalledLocalContainer;
012: import org.codehaus.cargo.container.State;
013: import org.codehaus.cargo.container.configuration.LocalConfiguration;
014: import org.codehaus.cargo.container.internal.AntContainerExecutorThread;
015: import org.codehaus.cargo.container.weblogic.WebLogic9xInstalledLocalContainer;
016:
017: import com.tc.test.server.appserver.AppServerParameters;
018: import com.tc.test.server.appserver.cargo.CargoAppServer;
019: import com.tc.util.ReplaceLine;
020: import com.tc.util.runtime.Os;
021:
022: import java.io.File;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.util.Vector;
027:
028: /**
029: * Weblogic9x AppServer implementation
030: */
031: public final class Weblogic9xAppServer extends CargoAppServer {
032:
033: public Weblogic9xAppServer(
034: Weblogic9xAppServerInstallation installation) {
035: super (installation);
036: }
037:
038: protected String cargoServerKey() {
039: return "weblogic9x";
040: }
041:
042: protected InstalledLocalContainer container(
043: LocalConfiguration config) {
044: return new TCWebLogic9xInstalledLocalContainer(config);
045: }
046:
047: protected void setExtraClasspath(AppServerParameters params) {
048: container().setExtraClasspath(
049: params.classpath().split(
050: String.valueOf(File.pathSeparatorChar)));
051: }
052:
053: protected void setConfigProperties(LocalConfiguration config)
054: throws Exception {
055: // config.setProperty(WebLogicPropertySet.DOMAIN, "domain");
056: }
057:
058: private static class TCWebLogic9xInstalledLocalContainer extends
059: WebLogic9xInstalledLocalContainer {
060:
061: public TCWebLogic9xInstalledLocalContainer(
062: LocalConfiguration configuration) {
063: super (configuration);
064: }
065:
066: public void doStop(Java java) throws Exception {
067: // Use the weblogic scripting interface to stop WLS 9.2. Sometimes we get RMI exceptions trying to stop WLS9.2
068: // using the 8.1 stop method. The stop scripts supplied with version 9 use this scripting stuff so I doing the
069: // same here.
070:
071: // Hack --> remove the extra build listener that gets added to the ant project
072: Vector buildListeners = java.getProject()
073: .getBuildListeners();
074: if (buildListeners.size() > 1) {
075: BuildListener listener = (BuildListener) buildListeners
076: .get(buildListeners.size() - 1);
077: java.getProject().removeBuildListener(listener);
078: }
079:
080: File serverDir = new File(getHome(), "server");
081: Path classpath = java.createClasspath();
082: classpath.createPathElement().setLocation(
083: new File(serverDir, "lib/weblogic_sp.jar"));
084: classpath.createPathElement().setLocation(
085: new File(serverDir, "lib/weblogic.jar"));
086: java.setClassname("weblogic.WLST");
087: java.createArg().setValue(
088: createShutdownScript().getAbsolutePath());
089: AntContainerExecutorThread webLogicRunner = new AntContainerExecutorThread(
090: java);
091: webLogicRunner.start();
092: webLogicRunner.join();
093: }
094:
095: private File createShutdownScript() throws IOException {
096: String port = getConfiguration().getPropertyValue(
097: "cargo.servlet.port");
098: String user = getConfiguration().getPropertyValue(
099: "cargo.weblogic.administrator.user");
100: String passwd = getConfiguration().getPropertyValue(
101: "cargo.weblogic.administrator.password");
102:
103: File tmp = File.createTempFile("wls92shutdown", ".py");
104: tmp.deleteOnExit();
105:
106: FileOutputStream out = null;
107:
108: try {
109: out = new FileOutputStream(tmp);
110:
111: String connect = "connect(url='t3://localhost:" + port
112: + "',adminServerName='AdminServer',username='"
113: + user + "',password='" + passwd + "')\n";
114: out.write(connect.getBytes("UTF-8"));
115: out.write("dumpStack()\n".getBytes("UTF-8"));
116: out
117: .write("shutdown(name='AdminServer',entityType='Server',force='true',block='true')\n"
118: .getBytes("UTF-8"));
119: out.write("exit()\n".getBytes("UTF-8"));
120: out.flush();
121: } finally {
122: if (out != null) {
123: out.close();
124: }
125: }
126:
127: return tmp;
128: }
129:
130: protected void setState(State state) {
131: if (state.equals(State.STARTING)) {
132: adjustConfig();
133: setBeaHomeIfNeeded();
134: prepareSecurityFile();
135: }
136: }
137:
138: private void adjustConfig() {
139: ReplaceLine.Token[] tokens = new ReplaceLine.Token[1];
140: tokens[0] = new ReplaceLine.Token(
141: 5,
142: "(NativeIOEnabled=\"false\")",
143: "NativeIOEnabled=\"false\" SocketReaderTimeoutMaxMillis=\"1000\" SocketReaderTimeoutMinMillis=\"1000\" StdoutDebugEnabled=\"true\" StdoutSeverityLevel=\"64\"");
144:
145: try {
146: ReplaceLine.parseFile(tokens, new File(
147: getConfiguration().getHome(),
148: "/config/config.xml"));
149: } catch (IOException ioe) {
150: throw new RuntimeException(ioe);
151: }
152: }
153:
154: private void setBeaHomeIfNeeded() {
155: File license = new File(getHome(), "license.bea");
156: if (license.exists()) {
157: this .setBeaHome(this .getHome());
158: }
159: }
160:
161: private void prepareSecurityFile() {
162: if (Os.isLinux()) {
163: try {
164: String[] resources = new String[] { "security/SerializedSystemIni.dat" };
165: for (int i = 0; i < resources.length; i++) {
166: String resource = "linux/" + resources[i];
167: File dest = new File(getConfiguration()
168: .getHome(), resources[i]);
169: copyResource(resource, dest);
170: }
171: } catch (IOException e) {
172: throw new RuntimeException(e);
173: }
174:
175: }
176: }
177:
178: private void copyResource(String name, File dest)
179: throws IOException {
180: dest.getParentFile().mkdirs();
181: InputStream in = getClass().getResourceAsStream(name);
182: FileOutputStream out = new FileOutputStream(dest);
183: try {
184: IOUtils.copy(in, out);
185: } finally {
186: IOUtils.closeQuietly(in);
187: IOUtils.closeQuietly(out);
188: }
189: }
190: }
191:
192: }
|