KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > server > Start


1 /**
2
3  * Redistribution and use of this software and associated documentation
4
5  * ("Software"), with or without modification, are permitted provided
6
7  * that the following conditions are met:
8
9  *
10
11  * 1. Redistributions of source code must retain copyright
12
13  * statements and notices. Redistributions must also contain a
14
15  * copy of this document.
16
17  *
18
19  * 2. Redistributions in binary form must reproduce the
20
21  * above copyright notice, this list of conditions and the
22
23  * following disclaimer in the documentation and/or other
24
25  * materials provided with the distribution.
26
27  *
28
29  * 3. The name "OpenEJB" must not be used to endorse or promote
30
31  * products derived from this Software without prior written
32
33  * permission of The OpenEJB Group. For written permission,
34
35  * please contact info@openejb.org.
36
37  *
38
39  * 4. Products derived from this Software may not be called "OpenEJB"
40
41  * nor may "OpenEJB" appear in their names without prior written
42
43  * permission of The OpenEJB Group. OpenEJB is a registered
44
45  * trademark of The OpenEJB Group.
46
47  *
48
49  * 5. Due credit should be given to the OpenEJB Project
50
51  * (http://openejb.org/).
52
53  *
54
55  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
56
57  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
58
59  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
60
61  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
62
63  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
64
65  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
66
67  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
68
69  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70
71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
72
73  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
74
75  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
76
77  * OF THE POSSIBILITY OF SUCH DAMAGE.
78
79  *
80
81  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
82
83  *
84
85  * $Id: Start.java 2504 2006-02-25 05:03:38Z dblevins $
86
87  */

88
89 package org.openejb.server;
90
91 import java.io.File JavaDoc;
92 import java.io.InputStream JavaDoc;
93 import java.io.OutputStream JavaDoc;
94 import java.lang.reflect.Method JavaDoc;
95 import java.net.Socket JavaDoc;
96 import java.util.ArrayList JavaDoc;
97 import java.util.Iterator JavaDoc;
98 import java.util.Map JavaDoc;
99 import java.util.Set JavaDoc;
100 /**
101  *
102  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
103  */

104 public class Start {
105
106     public static void main(String JavaDoc[] args) {
107 // System.exit(new Start().start()?0:1);
108
new Start().start();
109     }
110
111     public boolean start(){
112         if (!connect()) {
113             forkServerProcess();
114             return connect(10);
115         } else {
116             System.out.println(":: server already started ::");
117             return true;
118         }
119     }
120
121     private void forkServerProcess() {
122         try{
123             ArrayList JavaDoc cmd = new ArrayList JavaDoc();
124
125             String JavaDoc s = java.io.File.separator;
126
127             String JavaDoc java = System.getProperty("java.home")+s+"bin"+s+"java";
128
129             cmd.add(java);
130
131             addSystemProperties(cmd);
132
133             cmd.add("-classpath");
134             cmd.add(getClasspath());
135             cmd.add("org.openejb.server.Main");
136
137             String JavaDoc[] command = (String JavaDoc[]) cmd.toArray(new String JavaDoc[0]);
138
139             Runtime JavaDoc runtime = Runtime.getRuntime();
140             Process JavaDoc server = runtime.exec( command );
141
142             // Pipe the processes STDOUT to ours
143
InputStream JavaDoc out = server.getInputStream();
144             Thread JavaDoc serverOut = new Thread JavaDoc(new Pipe(out, System.out));
145             serverOut.setDaemon(true);
146             serverOut.start();
147
148             // Pipe the processes STDERR to ours
149
InputStream JavaDoc err = server.getErrorStream();
150             Thread JavaDoc serverErr = new Thread JavaDoc(new Pipe(err, System.err));
151             serverErr.setDaemon(true);
152             serverErr.start();
153         } catch (Exception JavaDoc e){
154             throw new RuntimeException JavaDoc("Cannot start the server.");
155         }
156     }
157
158     private void addSystemProperties(ArrayList JavaDoc cmd) {
159         Set JavaDoc set = System.getProperties().entrySet();
160         for (Iterator JavaDoc iter = set.iterator(); iter.hasNext();) {
161             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
162             String JavaDoc key = (String JavaDoc)entry.getKey();
163             String JavaDoc value = (String JavaDoc)entry.getValue();
164             if ( key.matches("^-X.*") ){
165                 cmd.add(key+value);
166             } else if ( !key.matches("^(java|javax|os|sun|user|file|awt|line|path)\\..*") ){
167                 cmd.add("-D"+key+"="+value);
168             }
169         }
170     }
171
172     private String JavaDoc getClasspath() {
173         String JavaDoc classpath = System.getProperty("java.class.path");
174         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
175         String JavaDoc antLoader = "org.apache.tools.ant.AntClassLoader";
176         if (cl.getClass().getName().equals(antLoader)){
177             try {
178                 Class JavaDoc ant = cl.getClass();
179                 Method JavaDoc getClasspath = ant.getMethod("getClasspath", new Class JavaDoc[0]);
180                 classpath += File.pathSeparator + getClasspath.invoke(cl,new Object JavaDoc[0]);
181             } catch (Exception JavaDoc e){
182                 e.printStackTrace();
183             }
184         }
185         return classpath;
186     }
187
188     private boolean connect() {
189         return connect( 1 );
190     }
191
192     private boolean connect(int tries) {
193         try{
194             Socket JavaDoc socket = new Socket JavaDoc("localhost", 4201);
195             OutputStream JavaDoc out = socket.getOutputStream();
196         } catch (Exception JavaDoc e){
197             if ( tries < 2 ) {
198                 return false;
199             } else {
200                 try{
201                     Thread.sleep(2000);
202                 } catch (Exception JavaDoc e2){
203                     e.printStackTrace();
204                 }
205                 return connect(--tries);
206             }
207         }
208         return true;
209     }
210
211     private static final class Pipe implements Runnable JavaDoc {
212         private final InputStream JavaDoc is;
213         private final OutputStream JavaDoc out;
214         private Pipe(InputStream JavaDoc is, OutputStream JavaDoc out) {
215             super();
216             this.is = is;
217             this.out = out;
218         }
219         public void run() {
220             try{
221                 int i = is.read();
222                 out.write( i );
223                 while ( i != -1 ){
224                     i = is.read();
225                     out.write( i );
226                 }
227             } catch (Exception JavaDoc e){
228                 e.printStackTrace();
229             }
230         }
231     }
232 }
233
Popular Tags