KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > process > LinkedJavaProcessStarter


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.process;
5
6 import java.lang.reflect.Method JavaDoc;
7
8 /**
9  * Runs another main class, with full arguments, but first establishes a socket heartbeat protocol with a parent process
10  * on a specified port — and kills itself if this ping protocol is broken. This prevents runaway Java processes.
11  */

12 public class LinkedJavaProcessStarter {
13
14   public static void main(String JavaDoc args[], boolean useSystemClassLoader) throws Exception JavaDoc {
15     int pingPort = Integer.parseInt(args[0]);
16     String JavaDoc childClass = args[1];
17
18     String JavaDoc[] realArgs = new String JavaDoc[args.length - 2];
19     if (realArgs.length > 0) System.arraycopy(args, 2, realArgs, 0, realArgs.length);
20
21     LinkedJavaProcessPollingAgent.startClientWatchdogService(pingPort, childClass);
22
23     final Class JavaDoc mainClass;
24     if (useSystemClassLoader) {
25       mainClass = ClassLoader.getSystemClassLoader().loadClass(childClass);
26     } else {
27       mainClass = Class.forName(childClass);
28     }
29
30     Method JavaDoc mainMethod = mainClass.getMethod("main", new Class JavaDoc[] { String JavaDoc[].class });
31     mainMethod.invoke(null, new Object JavaDoc[] { realArgs });
32
33   }
34
35   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
36     main(args, false);
37   }
38
39 }
Popular Tags