001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.config;
017:
018: import java.io.File;
019: import java.io.InputStream;
020: import java.io.OutputStream;
021: import java.net.Socket;
022: import java.util.Properties;
023:
024: /**
025: * @version $Rev: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
026: */
027: public class RemoteServer {
028: private static final boolean DEBUG = System.getProperty(
029: "openejb.server.debug", "false").equalsIgnoreCase("TRUE");
030: private static final boolean TOMCAT;
031: static {
032: File home = getHome();
033: TOMCAT = (home != null)
034: && (new File(new File(home, "bin"), "catalina.sh")
035: .exists());
036: }
037:
038: /**
039: * Has the remote server's instance been already running ?
040: */
041: private boolean serverHasAlreadyBeenStarted = true;
042:
043: private Properties properties;
044: private Process server;
045:
046: public void init(Properties props) {
047: properties = props;
048:
049: props
050: .put("java.naming.factory.initial",
051: "org.apache.openejb.client.RemoteInitialContextFactory");
052: props.put("java.naming.provider.url", "127.0.0.1:4201");
053: props.put("java.naming.security.principal", "testuser");
054: props.put("java.naming.security.credentials", "testpassword");
055: }
056:
057: public static void main(String[] args) {
058: assert args.length > 0 : "no arguments supplied: valid arguments are 'start' or 'stop'";
059: if (args[0].equalsIgnoreCase("start")) {
060: new RemoteServer().start();
061: } else if (args[0].equalsIgnoreCase("stop")) {
062: RemoteServer remoteServer = new RemoteServer();
063: remoteServer.serverHasAlreadyBeenStarted = false;
064: remoteServer.stop();
065: } else {
066: throw new RuntimeException(
067: "valid arguments are 'start' or 'stop'");
068: }
069: }
070:
071: public Properties getProperties() {
072: return properties;
073: }
074:
075: public void destroy() {
076: stop();
077: }
078:
079: public void start() {
080: if (!connect()) {
081: try {
082: System.out.println("[] START SERVER");
083:
084: File home = getHome();
085: System.out.println("OPENEJB_HOME = "
086: + home.getAbsolutePath());
087: String systemInfo = "Java "
088: + System.getProperty("java.version") + "; "
089: + System.getProperty("os.name") + "/"
090: + System.getProperty("os.version");
091: System.out.println("SYSTEM_INFO = " + systemInfo);
092:
093: serverHasAlreadyBeenStarted = false;
094:
095: File openejbJar = null;
096: File javaagentJar = null;
097:
098: File lib;
099: if (!TOMCAT) {
100: lib = new File(home, "lib");
101: } else {
102: lib = new File(new File(new File(home, "webapps"),
103: "openejb"), "lib");
104: }
105:
106: for (File file : lib.listFiles()) {
107: if (file.getName().startsWith("openejb-core")
108: && file.getName().endsWith("jar")) {
109: openejbJar = file;
110: }
111: if (file.getName().startsWith("openejb-javaagent")
112: && file.getName().endsWith("jar")) {
113: javaagentJar = file;
114: }
115: }
116:
117: if (openejbJar == null) {
118: throw new IllegalStateException(
119: "Cannot find the openejb-core jar in "
120: + lib.getAbsolutePath());
121: }
122: if (javaagentJar == null) {
123: throw new IllegalStateException(
124: "Cannot find the openejb-javaagent jar in "
125: + lib.getAbsolutePath());
126: }
127:
128: //File openejbJar = new File(lib, "openejb-core-" + version + ".jar");
129:
130: //DMB: If you don't use an array, you get problems with jar paths containing spaces
131: // the command won't parse correctly
132: String[] args;
133: if (!TOMCAT) {
134: if (DEBUG) {
135: args = new String[] {
136: "java",
137: "-XX:+HeapDumpOnOutOfMemoryError",
138: "-Xdebug",
139: "-Xnoagent",
140: "-Djava.compiler=NONE",
141: "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005",
142:
143: "-javaagent:"
144: + javaagentJar
145: .getAbsolutePath(),
146:
147: "-jar", openejbJar.getAbsolutePath(),
148: "start" };
149: } else {
150: args = new String[] {
151: "java",
152: "-XX:+HeapDumpOnOutOfMemoryError",
153: "-javaagent:"
154: + javaagentJar
155: .getAbsolutePath(),
156: "-jar", openejbJar.getAbsolutePath(),
157: "start" };
158: }
159: } else {
160: File bin = new File(home, "bin");
161: File bootstrapJar = new File(bin, "bootstrap.jar");
162: File commonsLoggingJar = new File(bin,
163: "commons-logging-api.jar");
164:
165: File conf = new File(home, "conf");
166: File loggingProperties = new File(conf,
167: "logging.properties");
168:
169: File endorsed = new File(home, "endorsed");
170: File temp = new File(home, "temp");
171:
172: if (DEBUG) {
173: args = new String[] {
174: "java",
175: "-XX:+HeapDumpOnOutOfMemoryError",
176: "-Xdebug",
177: "-Xnoagent",
178: "-Djava.compiler=NONE",
179: "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005",
180:
181: "-javaagent:"
182: + javaagentJar
183: .getAbsolutePath(),
184:
185: "-Dcom.sun.management.jmxremote",
186:
187: "-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager",
188: "-Djava.util.logging.config.file="
189: + loggingProperties
190: .getAbsolutePath(),
191:
192: "-Djava.io.tmpdir="
193: + temp.getAbsolutePath(),
194: "-Djava.endorsed.dirs="
195: + endorsed.getAbsolutePath(),
196: "-Dcatalina.base="
197: + home.getAbsolutePath(),
198: "-Dcatalina.home="
199: + home.getAbsolutePath(),
200: "-Dopenejb.servicemanager.enabled="
201: + Boolean
202: .getBoolean("openejb.servicemanager.enabled"),
203:
204: "-classpath",
205: bootstrapJar.getAbsolutePath()
206: + ":"
207: + commonsLoggingJar
208: .getAbsolutePath(),
209:
210: "org.apache.catalina.startup.Bootstrap",
211: "start" };
212: } else {
213: args = new String[] {
214: "java",
215: "-XX:+HeapDumpOnOutOfMemoryError",
216: "-javaagent:"
217: + javaagentJar
218: .getAbsolutePath(),
219:
220: "-Dcom.sun.management.jmxremote",
221:
222: "-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager",
223: "-Djava.util.logging.config.file="
224: + loggingProperties
225: .getAbsolutePath(),
226:
227: "-Djava.io.tmpdir="
228: + temp.getAbsolutePath(),
229: "-Djava.endorsed.dirs="
230: + endorsed.getAbsolutePath(),
231: "-Dcatalina.base="
232: + home.getAbsolutePath(),
233: "-Dcatalina.home="
234: + home.getAbsolutePath(),
235: "-Dopenejb.servicemanager.enabled="
236: + Boolean
237: .getBoolean("openejb.servicemanager.enabled"),
238:
239: "-classpath",
240: bootstrapJar.getAbsolutePath()
241: + ":"
242: + commonsLoggingJar
243: .getAbsolutePath(),
244:
245: "org.apache.catalina.startup.Bootstrap",
246: "start" };
247: }
248: }
249: server = Runtime.getRuntime().exec(args);
250:
251: // Pipe the processes STDOUT to ours
252: InputStream out = server.getInputStream();
253: Thread serverOut = new Thread(new Pipe(out, System.out));
254:
255: serverOut.setDaemon(true);
256: serverOut.start();
257:
258: // Pipe the processes STDERR to ours
259: InputStream err = server.getErrorStream();
260: Thread serverErr = new Thread(new Pipe(err, System.err));
261:
262: serverErr.setDaemon(true);
263: serverErr.start();
264: } catch (Exception e) {
265: throw (RuntimeException) new RuntimeException(
266: "Cannot start the server. Exception: "
267: + e.getClass().getName() + ": "
268: + e.getMessage()).initCause(e);
269: }
270: if (DEBUG) {
271: connect(Integer.MAX_VALUE);
272: } else {
273: connect(10);
274: }
275: } else {
276: //System.out.println("[] SERVER STARTED");
277: }
278: }
279:
280: private static File getHome() {
281: String openejbHome = System.getProperty("openejb.home");
282:
283: if (openejbHome != null) {
284: return new File(openejbHome);
285: } else {
286: return null;
287: }
288: }
289:
290: public void stop() {
291: if (!serverHasAlreadyBeenStarted) {
292: try {
293: System.out.println("[] STOP SERVER");
294:
295: int port;
296: String command;
297: if (!TOMCAT) {
298: port = 4200;
299: command = "Stop";
300: } else {
301: port = 8005;
302: command = "SHUTDOWN";
303: }
304:
305: Socket socket = new Socket("localhost", port);
306: OutputStream out = socket.getOutputStream();
307: out.write(command.getBytes());
308:
309: if (server != null) {
310: server.waitFor();
311: server = null;
312: }
313: } catch (Exception e) {
314: e.printStackTrace();
315: }
316: }
317: }
318:
319: private boolean connect() {
320: return connect(1);
321: }
322:
323: private boolean connect(int tries) {
324: //System.out.println("CONNECT "+ tries);
325: try {
326: int port;
327: if (!TOMCAT) {
328: port = 4200;
329: } else {
330: port = 8005;
331: }
332:
333: Socket socket = new Socket("localhost", port);
334: OutputStream out = socket.getOutputStream();
335: out.close();
336: } catch (Exception e) {
337: //System.out.println(e.getMessage());
338: if (tries < 2) {
339: return false;
340: } else {
341: try {
342: Thread.sleep(2000);
343: } catch (Exception e2) {
344: e.printStackTrace();
345: }
346: return connect(--tries);
347: }
348: }
349:
350: return true;
351: }
352:
353: private static final class Pipe implements Runnable {
354: private final InputStream is;
355: private final OutputStream out;
356:
357: private Pipe(InputStream is, OutputStream out) {
358: super ();
359: this .is = is;
360: this .out = out;
361: }
362:
363: public void run() {
364: try {
365: int i = is.read();
366: out.write(i);
367:
368: while (i != -1) {
369: i = is.read();
370: out.write(i);
371: }
372:
373: } catch (Exception e) {
374: e.printStackTrace();
375: }
376: }
377: }
378: }
|