01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.test.server.appserver.cargo;
06:
07: import com.tc.process.HeartBeatService;
08: import com.tc.process.LinkedJavaProcessPollingAgent;
09:
10: import java.io.File;
11: import java.io.FileInputStream;
12: import java.io.IOException;
13: import java.lang.reflect.Method;
14: import java.util.Enumeration;
15: import java.util.Properties;
16:
17: /**
18: * This class serves three purposes. It delegates to {@link LinkedJavaProcessPollingAgent} to page the parent process.
19: * And it loads a properties file which was written by the parent process (in the same directory as the log) and sets
20: * all name value pairs as system properties for the appserver's JVM. This makes these available to servlets running in
21: * the container.
22: */
23: public final class CargoLinkedChildProcess {
24:
25: private static File instanceDir;
26:
27: private CargoLinkedChildProcess() {
28: // cannot instantiate
29: }
30:
31: public static void main(String[] args) throws Exception {
32: String className = args[0];
33: int port = Integer.parseInt(args[1]);
34: instanceDir = new File(args[2]);
35:
36: String[] serverArgs = new String[0];
37: if (args.length > 3) {
38: serverArgs = new String[args.length - 3];
39: for (int i = 3; i < args.length; i++) {
40: serverArgs[i - 3] = args[i];
41: }
42: }
43:
44: System.out.println("JAVA VERSION: "
45: + System.getProperty("java.version"));
46:
47: HeartBeatService.registerForHeartBeat(port, className, true);
48: loadProperties();
49:
50: try {
51: Class startServer = Class.forName(className);
52: Method main = startServer.getMethod("main",
53: new Class[] { String[].class });
54: main.invoke(null, new Object[] { serverArgs });
55:
56: } catch (ClassNotFoundException cnfe) {
57: System.err.println("unable to locate server class: "
58: + className);
59: cnfe.printStackTrace();
60: } catch (NoSuchMethodException nsme) {
61: System.err.println("unable to access method: main()");
62: nsme.printStackTrace();
63: }
64: }
65:
66: private static void loadProperties() {
67: File sandbox = instanceDir.getParentFile();
68: Properties props = new Properties();
69: try {
70: props.load(new FileInputStream(new File(sandbox
71: + File.separator + instanceDir.getName()
72: + ".properties")));
73: } catch (IOException ioe) {
74: throw new RuntimeException(
75: "Unable to load properties file: " + sandbox
76: + File.separator + instanceDir.getName()
77: + ".properties");
78: }
79: String name = null;
80: for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
81: name = (String) e.nextElement();
82: System.setProperty(name, props.getProperty(name));
83: }
84: }
85:
86: }
|