KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > webman > ProcessStarter


1 package com.teamkonzept.webman;
2
3 import java.io.*;
4 import java.net.*;
5
6 import com.teamkonzept.lib.*;
7 import com.teamkonzept.web.TKEvent;
8 import com.teamkonzept.web.TKHttpInterface;
9 import org.apache.log4j.Category;
10
11 /**
12  * Starts an external java process.
13  * <p>
14  * To use the ProcessStarter create an instance of the class, then call "start".
15  * All output of the running process to stdout and stderr can be redirected
16  * to some PrintWriters specified in the constructor.
17  * @author $Author: gregor $
18  * @version $Revision: 1.16 $
19  */

20 public class ProcessStarter
21 {
22
23     /** Logging Category */
24     private static Category cat = Category.getInstance(ProcessStarter.class.getName());
25
26     /** Setze SocketTimeout deswegen so hoch (30 min), weil der Stager (d.h. SiteTransmitter)
27      u.U. über einen laaangen Zeitraum nichts an den ProcessStarter sendet (nämlich beim Übertragen der Site)
28      Bricht dann der ProcessStarter während dessen die Verbindung ab, wird der Socket also nicht mehr ausgelesen,
29      so schreibt der SiteTransmitter, wenn er die Site fertig übertragen hat, seine Ausgaben trotzdem weiter in den Socket
30      -> Buffer is irgendwann voll -> SiteTransmitter kann nich mehr schreiben -> SiteTransmitter hängt, wird nich mehr fertig
31      Da dieses Timeout ziemlich hoch ist, sollte versucht werden, es davon abhängig zu setzen, ob nun der Generator oder der
32      Stager gestartet wird*/

33     private final static int SOCKET_TIME_OUT = 1800000;
34
35     /** ... */
36     private PrintWriter out;
37     private PrintWriter err;
38
39     /**
40      * a tag used to identified a keep alive log message send from the
41      * generator
42      **/

43     public final static String JavaDoc KEEP_ALIVE_TAG = "-- keep alive --";
44
45     /**
46     Constructor.
47     @param out a PrintWriter to which stdout messages of the process are redirected.
48     @param err a PrintWriter to which stderr messages of the process are redirected.
49     */

50     public ProcessStarter (PrintWriter out, PrintWriter err)
51     {
52         this.out = out;
53         this.err = err;
54     }
55
56     /**
57     Starts the process. The default classpath will be the the classpath as given
58     by System.getProperty("java.class.path") of the parent process.
59     @param startClassname the name of the java class to start (should
60         contain a main-method)
61     @param arguments the (optional) argument string given to the proces
62     @param addClasspath some additional classpath strings added to the default
63         classpath. The string should start with a classpath separator.
64     */

65     public boolean start(String JavaDoc startClassname, String JavaDoc arguments, String JavaDoc addClasspath)
66     {
67         Runtime JavaDoc rt = Runtime.getRuntime();
68         Process JavaDoc proc = null;
69         ServerSocket server = null;
70         try
71         {
72             // Serversocket starten - welcher Port ???
73
server = new ServerSocket(0);
74             int port = server.getLocalPort();
75             String JavaDoc startCmd = genStartCmd(startClassname, arguments + " port=" +port, addClasspath);
76             ProcessStarter.cat.debug(startCmd);
77             proc = rt.exec(startCmd);
78             Socket socket = server.accept();
79             //Kommentar zum SocketTimeout s.o.
80
socket.setSoTimeout(SOCKET_TIME_OUT);
81             InputStream in = socket.getInputStream();
82             BufferedReader reader = new BufferedReader(new InputStreamReader(in));
83             String JavaDoc line;
84             while ((line = reader.readLine()) != null)
85             {
86                 if (!line.endsWith(KEEP_ALIVE_TAG)) {
87                     out.println(line);
88                     out.flush();
89                 }
90
91                 /*
92                 try {
93                     Thread.sleep(100);
94                 } catch (InterruptedException e) {
95                     out.println(e.toString());
96                 }
97                 */

98             }
99             /*
100             reader = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
101             while ((line = reader.readLine()) != null) {
102                 err.println(line);
103                 err.flush();
104             }
105             */

106         }
107         catch (Exception JavaDoc e)
108         {
109             ProcessStarter.cat.error("Processstarter.start", e);
110         }
111         finally
112         {
113             try
114             {
115                 server.close();
116             }
117             catch (Exception JavaDoc e)
118             {
119                 ProcessStarter.cat.warn("Processstarter.start.finally", e);
120             }
121         }
122         try {
123             if (proc != null)
124                 proc.waitFor();
125         }
126         catch (InterruptedException JavaDoc e)
127         {
128             ProcessStarter.cat.warn("Processstarter.start.waitfor", e);
129         }
130
131         int exitValue = proc.exitValue();
132
133         proc.destroy();
134
135         return exitValue == 0;
136     }
137
138
139     /**
140     Determines the path to the webman classes.
141     The returned string is of the form:
142     "docRoot/WEB-INF/classes;docRoot/WEB-INF/classes/webman.zip"
143     ("WEB-INF/classes" accords with the Java Servlet 2.2 recommendation.)
144     @param docRoot the absolute path to the document root of the web application
145     */

146     // TODO: put this method in a a web application interface class?
147
public static String JavaDoc getWMClasspath(String JavaDoc docRoot)
148     {
149         String JavaDoc pathSeparator = System.getProperty("path.separator");
150         String JavaDoc servletDir = pathSeparator + docRoot + File.separator +
151             "WEB-INF" + File.separator + "classes";
152         // there should be a WEB-INF/lib dir with some jar files...
153
String JavaDoc addPath = "";
154         String JavaDoc libDir = docRoot + "WEB-INF" + File.separator + "lib";
155         File d = new File(libDir);
156         libDir = pathSeparator + libDir + File.separator;
157         if ( d.isDirectory() )
158         {
159             String JavaDoc[] list = d.list();
160             for ( int i = 0; i < list.length; i++ )
161                 if ( list[i].endsWith(".jar")
162                 || list[i].endsWith(".zip") )
163                 addPath += libDir + list[i];
164         }
165         // hier Spezial fuer Tomcat 4.0 und servlet.jar
166
String JavaDoc privateDir = docRoot + "WEB-INF" + File.separator + "lib" + File.separator + "webman-only";
167         libDir = pathSeparator + privateDir + File.separator;
168         d = new File(privateDir);
169         if ( d.isDirectory() )
170         {
171             String JavaDoc[] list = d.list();
172             for ( int i = 0; i < list.length; i++ )
173                 if ( list[i].endsWith(".jar")
174                 || list[i].endsWith(".zip") )
175                 addPath += libDir + list[i];
176         }
177         return servletDir + addPath;
178     }
179
180     /**
181     Generates the start string which can be passed to the Runtime.exec() method.
182     @see ProcessStarter.start()
183     @return the generated string
184     */

185     protected String JavaDoc genStartCmd(String JavaDoc startClassname, String JavaDoc arguments,
186                                 String JavaDoc addClasspath)
187     {
188         String JavaDoc javaHome = System.getProperty("java.home");
189         String JavaDoc javaProg = javaHome + File.separator + "bin" + File.separator + "java";
190         String JavaDoc javaClasspath = System.getProperty("java.class.path") + addClasspath;
191         
192         // Memory Einstellungen für den Start !
193
String JavaDoc memory = getMemoryProps();
194         String JavaDoc cmd = javaProg + memory + " -classpath " + javaClasspath + " " + startClassname + " " + arguments;
195
196         String JavaDoc os = System.getProperty("os.name");
197         if (os.equalsIgnoreCase("Windows NT"))
198         {
199             cmd = "cmd.exe /c " + cmd;
200         }
201         ProcessStarter.cat.debug(cmd);
202         return cmd;
203     }
204
205     private String JavaDoc getMemoryProps()
206     {
207         try
208         {
209             PropertyManager man = PropertyManager.getPropertyManager("MEMORY_EXTERNAL");
210             String JavaDoc initial = man.getValue("INITIAL_HEAP", null);
211             String JavaDoc max = man.getValue("MAX_HEAP", null);
212             String JavaDoc back = " ";
213             if (initial != null)
214                 back += "-Xms" + initial + " ";
215             if (max != null)
216                 back += "-Xmx" + max + " ";
217             return back;
218         }
219         catch (Exception JavaDoc e)
220         {
221             ProcessStarter.cat.info("getMemoryProps(): exception caught");
222         }
223         return "";
224     }
225     
226     /**
227     Determine if the host operating system is a Microsoft OS
228     
229     private static boolean isMSOS()
230     {
231         // TODO: Abfrage vollstaendig machen...
232         String os = System.getProperty("os.name");
233         if (os.equalsIgnoreCase("Windows NT") ||
234             os.equalsIgnoreCase("Windows 95") ||
235             os.equalsIgnoreCase("Windows 98")) {
236             return true;
237         }
238         else {
239             return false;
240         }
241     }*/

242
243 }
244
Popular Tags