001: package com.tc.test.server.appserver.jetty6x;
002:
003: import org.apache.commons.io.IOUtils;
004:
005: import com.tc.process.Exec;
006: import com.tc.process.HeartBeatService;
007: import com.tc.process.Exec.Result;
008: import com.tc.test.TestConfigObject;
009: import com.tc.test.server.ServerParameters;
010: import com.tc.test.server.ServerResult;
011: import com.tc.test.server.appserver.AbstractAppServer;
012: import com.tc.test.server.appserver.AppServerParameters;
013: import com.tc.test.server.appserver.AppServerResult;
014: import com.tc.test.server.appserver.cargo.CargoLinkedChildProcess;
015: import com.tc.test.server.util.AppServerUtil;
016: import com.tc.util.PortChooser;
017:
018: import java.io.BufferedReader;
019: import java.io.File;
020: import java.io.FileReader;
021: import java.io.FileWriter;
022: import java.io.PrintWriter;
023: import java.util.ArrayList;
024: import java.util.Arrays;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028: import java.util.Set;
029:
030: public class Jetty6xAppServer extends AbstractAppServer {
031: private static final String JAVA_CMD = System
032: .getProperty("java.home")
033: + File.separator + "bin" + File.separator + "java";
034: private final String STOP_KEY = "secret";
035: private final String JETTY_MAIN_CLASS = "org.mortbay.start.Main";
036: private static final long START_STOP_TIMEOUT = 240 * 1000;
037:
038: private static final String target = "<SystemProperty name=\"jetty.home\" default=\".\"/>/webapps";
039:
040: private String configFile;
041: private String instanceName;
042: private File instanceDir;
043: private File workDir;
044:
045: private int jetty_port = 0;
046: private int stop_port = 0;
047: private Thread runner = null;
048:
049: public Jetty6xAppServer(Jetty6xAppServerInstallation installation) {
050: super (installation);
051: }
052:
053: public ServerResult start(ServerParameters parameters)
054: throws Exception {
055: AppServerParameters params = (AppServerParameters) parameters;
056: return startJetty(params);
057: }
058:
059: public void stop() throws Exception {
060: final String[] cmd = new String[] { JAVA_CMD,
061: "-DSTOP.PORT=" + stop_port, "-DSTOP.KEY=" + STOP_KEY,
062: "-jar", "start.jar", "--stop" };
063:
064: System.err.println("Stopping instance " + instanceName + "...");
065: Process stop_cmd = Runtime.getRuntime().exec(cmd, null,
066: this .serverInstallDirectory());
067: int exit_code = stop_cmd.waitFor();
068: if (exit_code != 0) {
069: System.err.println("error stopping isntance "
070: + instanceName);
071: }
072:
073: if (runner != null) {
074: runner.join(START_STOP_TIMEOUT);
075: }
076:
077: if (runner.isAlive()) {
078: System.err.println("Instance " + instanceName + " on port "
079: + jetty_port + " still alive.");
080: } else {
081: System.err.println("jetty instance " + instanceName
082: + " stopped");
083: }
084: }
085:
086: private AppServerResult startJetty(AppServerParameters params)
087: throws Exception {
088: prepareDeployment(params);
089:
090: String[] jvmargs = params.jvmArgs().replaceAll("'", "").split(
091: "\\s+");
092: List cmd = new ArrayList(Arrays.asList(jvmargs));
093: cmd.add(0, JAVA_CMD);
094: cmd.add("-cp");
095: cmd.add(this .serverInstallDirectory()
096: + File.separator
097: + "start.jar"
098: + File.pathSeparator
099: + TestConfigObject.getInstance()
100: .linkedChildProcessClasspath());
101: cmd.add("-Djetty.home=" + this .serverInstallDirectory());
102: cmd.add("-Djetty.port=" + jetty_port);
103: cmd.add("-DSTOP.PORT=" + stop_port);
104: cmd.add("-DSTOP.KEY=" + STOP_KEY);
105: cmd.add("-Djava.io.tmpdir=" + workDir.getAbsolutePath());
106: cmd.add(CargoLinkedChildProcess.class.getName());
107: cmd.add(JETTY_MAIN_CLASS);
108: cmd.add(String.valueOf(HeartBeatService.listenPort()));
109: cmd.add(instanceDir.getAbsolutePath());
110: cmd.add(configFile);
111:
112: final String[] cmdArray = (String[]) cmd
113: .toArray(new String[] {});
114: final String nodeLogFile = new File(instanceDir + ".log")
115: .getAbsolutePath();
116:
117: runner = new Thread("runner for " + instanceName) {
118: public void run() {
119: try {
120: Result result = Exec.execute(cmdArray, nodeLogFile,
121: null, instanceDir);
122: if (result.getExitCode() != 0) {
123: System.err.println(result);
124: }
125: } catch (Exception e) {
126: e.printStackTrace();
127: }
128: }
129: };
130: runner.start();
131: System.err.println("Starting jetty " + instanceName
132: + " on port " + jetty_port + "...");
133: AppServerUtil.waitForPort(jetty_port, START_STOP_TIMEOUT);
134: System.err.println("Started " + instanceName + " on port "
135: + jetty_port);
136: return new AppServerResult(jetty_port, this );
137: }
138:
139: private void prepareDeployment(AppServerParameters params)
140: throws Exception {
141: // move wars into the correct location
142: Map wars = params.wars();
143: if (wars != null && wars.size() > 0) {
144: File wars_dir = getWarsDirectory();
145:
146: Set war_entries = wars.entrySet();
147: Iterator war_entries_it = war_entries.iterator();
148: while (war_entries_it.hasNext()) {
149: Map.Entry war_entry = (Map.Entry) war_entries_it.next();
150: File war_file = (File) war_entry.getValue();
151: war_file
152: .renameTo(new File(wars_dir, war_file.getName()));
153: }
154: }
155:
156: // setup deployment config
157: PortChooser portChooser = new PortChooser();
158: jetty_port = portChooser.chooseRandomPort();
159: stop_port = portChooser.chooseRandomPort();
160:
161: instanceName = params.instanceName();
162: instanceDir = new File(sandboxDirectory(), instanceName);
163: workDir = new File(sandboxDirectory(), "work");
164: workDir.mkdirs();
165:
166: if (new File(instanceDir, "logs").mkdirs() == false) {
167: throw new Exception(
168: "Can't create logs directory for jetty instance: "
169: + instanceName);
170: }
171: setProperties(params, jetty_port, instanceDir);
172: createConfigFile();
173: }
174:
175: protected File getWarsDirectory() {
176: return new File(this .sandboxDirectory(), "war");
177: }
178:
179: private void createConfigFile() throws Exception {
180: String origialConfig = this .serverInstallDirectory()
181: .getAbsolutePath()
182: + File.separator + "etc" + File.separator + "jetty.xml";
183: if (new File(origialConfig).exists() == false) {
184: throw new Exception(origialConfig + " doesn't exist.");
185: }
186:
187: StringBuffer buffer = new StringBuffer(1024);
188: BufferedReader in = null;
189: PrintWriter out = null;
190:
191: try {
192: in = new BufferedReader(new FileReader(origialConfig));
193: String line;
194: while ((line = in.readLine()) != null) {
195: buffer.append(line).append("\n");
196: }
197:
198: int startIndex = buffer.indexOf(target);
199: if (startIndex > 0) {
200: int endIndex = startIndex + target.length();
201: buffer.replace(startIndex, endIndex, getWarsDirectory()
202: .getAbsolutePath());
203: } else {
204: throw new RuntimeException("Can't find target: "
205: + target);
206: }
207:
208: configFile = this .sandboxDirectory().getAbsolutePath()
209: + File.separator + "jetty.xml";
210: out = new PrintWriter(new FileWriter(configFile));
211: out.println(buffer.toString());
212:
213: } finally {
214: IOUtils.closeQuietly(in);
215: IOUtils.closeQuietly(out);
216: }
217: }
218: }
|