KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > boot > ProcessManager


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.boot;
31
32 import com.caucho.vfs.ReadStream;
33 import com.caucho.vfs.Vfs;
34
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.util.logging.Level;
38 import java.util.logging.Logger;
39
40 /**
41  * Thread for managing the child process.
42  */

43 public class ProcessManager {
44   private static final Logger log =
45     Logger.getLogger(ProcessManager.class.getName());
46
47   private volatile Process _process;
48
49   void start()
50     throws IOException
51   {
52     Runtime runtime = Runtime.getRuntime();
53
54     _process = runtime.exec(new String[] {"java", "com.caucho.server.resin.Resin" },
55                 new String[] {"CLASSPATH=lib/resin.jar"});
56
57     try {
58       new ReadThread(_process.getInputStream()).start();
59       new ReadThread(_process.getErrorStream()).start();
60
61       int status = waitForStatus();
62
63       System.out.println("STATUS:" + status);
64     } finally {
65       _process = null;
66     }
67   }
68
69   private int waitForStatus()
70   {
71     while (true) {
72       try {
73     return _process.waitFor();
74       } catch (InterruptedException e) {
75     log.log(Level.WARNING, e.toString(), e);
76       }
77     }
78   }
79
80   class ReadThread extends Thread {
81     private ReadStream _is;
82     private char []cBuf = new char[1024];
83
84     ReadThread(InputStream is)
85     {
86       _is = Vfs.openRead(is);
87     }
88
89     public void run()
90     {
91       while (_process != null) {
92     try {
93       int len = _is.read(cBuf, 0, cBuf.length);
94
95       if (len < 0)
96         return;
97
98       System.out.print(new String(cBuf, 0, len));
99     } catch (IOException e) {
100     }
101       }
102     }
103   }
104 }
105
Popular Tags