KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > TomcatRemoteTestServer


1 /**
2  *
3  * Copyright 2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.openejb.test;
18
19 import org.openejb.client.RemoteInitialContextFactory;
20
21 import java.io.File JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 /**
30  * @version $Revision$ $Date$
31  */

32 public class TomcatRemoteTestServer implements TestServer {
33     private Properties JavaDoc properties;
34     private String JavaDoc servletUrl;
35     private File JavaDoc tomcatHome;
36
37     private boolean serverHasAlreadyBeenStarted = true;
38
39     public void init(Properties JavaDoc props) {
40         properties = props;
41         servletUrl = System.getProperty("remote.serlvet.url", "http://127.0.0.1:8080/openejb/remote");
42 // props.put("test.server.class", TomcatRemoteTestServer.class.getName());
43
props.put("java.naming.factory.initial", RemoteInitialContextFactory.class.getName());
44         props.put("java.naming.provider.url", servletUrl);
45
46         String JavaDoc homeProperty = System.getProperty("tomcat.home");
47         if (homeProperty == null) {
48             throw new IllegalStateException JavaDoc("The system property tomcat.home must be defined.");
49         }
50
51         tomcatHome = new File JavaDoc(homeProperty);
52
53         if (!tomcatHome.exists()) {
54             throw new IllegalStateException JavaDoc("The tomcat.home directory does not exist: " + tomcatHome.getAbsolutePath());
55         }
56     }
57
58     public void start() {
59         if (connect()) {
60             return;
61         }
62
63         try {
64             System.out.println("[] START TOMCAT SERVER");
65             System.out.println("CATALINA_HOME = " + tomcatHome.getAbsolutePath());
66
67             String JavaDoc systemInfo = "Java " + System.getProperty("java.version") + "; " + System.getProperty("os.name") + "/" + System.getProperty("os.version");
68             System.out.println("SYSTEM_INFO = " + systemInfo);
69
70             serverHasAlreadyBeenStarted = false;
71
72
73             execBootstrap("start");
74         } catch (Exception JavaDoc e) {
75             e.printStackTrace();
76             throw new RuntimeException JavaDoc("Cannot start the server: " + e.getClass().getName() + ": " + e.getMessage(), e);
77         }
78         connect(10);
79         // Wait a wee bit longer for good measure
80
try {
81             Thread.sleep(5000);
82         } catch (Exception JavaDoc e) {
83             e.printStackTrace();
84         }
85     }
86
87     public void stop() {
88         if (!serverHasAlreadyBeenStarted) {
89             try {
90                 System.out.println("[] STOP TOMCAT SERVER");
91                 execBootstrap("stop");
92
93                 disconnect(10);
94             } catch (Exception JavaDoc e) {
95                 e.printStackTrace();
96             }
97         }
98     }
99
100     private void execBootstrap(String JavaDoc command) throws IOException JavaDoc {
101         String JavaDoc[] bootstrapCommand = getBootstrapCommand(tomcatHome, command);
102         Process JavaDoc server = Runtime.getRuntime().exec(bootstrapCommand);
103
104         FilePathBuilder tomcat = new FilePathBuilder(tomcatHome);
105         OutputStream JavaDoc catalinaOut = new FileOutputStream JavaDoc(tomcat.l("logs").f("catalina.out"));
106
107         // Pipe the processes STDOUT to ours
108
InputStream JavaDoc out = server.getInputStream();
109         Thread JavaDoc serverOut = new Thread JavaDoc(new Pipe(out, catalinaOut));
110
111         serverOut.setDaemon(true);
112         serverOut.start();
113
114         // Pipe the processes STDERR to ours
115
InputStream JavaDoc err = server.getErrorStream();
116         Thread JavaDoc serverErr = new Thread JavaDoc(new Pipe(err, catalinaOut));
117
118         serverErr.setDaemon(true);
119         serverErr.start();
120     }
121
122     public Properties JavaDoc getContextEnvironment() {
123         return (Properties JavaDoc) properties.clone();
124     }
125
126     private boolean disconnect(int tries) {
127         if (connect()) {
128             tries--;
129             if (tries < 1) {
130                 return false;
131             } else {
132                 try {
133                     Thread.sleep(5000);
134                 } catch (InterruptedException JavaDoc e) {
135                 }
136                 disconnect(tries);
137             }
138         }
139
140         return true;
141     }
142
143     private boolean connect() {
144         return connect(1);
145     }
146
147     private boolean connect(int tries) {
148         //System.out.println("CONNECT "+ tries);
149
try {
150             URL JavaDoc url = new URL JavaDoc(servletUrl);
151             url.openStream();
152         } catch (Exception JavaDoc e) {
153             tries--;
154             //System.out.println(e.getMessage());
155
if (tries < 1) {
156                 return false;
157             } else {
158                 try {
159                     Thread.sleep(5000);
160                 } catch (Exception JavaDoc e2) {
161                     e.printStackTrace();
162                 }
163                 return connect(tries);
164             }
165         }
166
167         return true;
168     }
169
170     private String JavaDoc[] getBootstrapCommand(File JavaDoc tomcatHome, String JavaDoc command) {
171         FilePathBuilder tomcat = new FilePathBuilder(tomcatHome);
172         FilePathBuilder tomcatBin = tomcat.l("bin");
173
174         FilePathBuilder javaHome = new FilePathBuilder(System.getProperty("java.home"));
175         String JavaDoc path = tomcatHome.getAbsolutePath();
176
177         String JavaDoc s = File.pathSeparator;
178
179         if (path.indexOf("tomcat-5.5") != -1) {
180             return new String JavaDoc[]{javaHome.l("bin").s("java"),
181                     "-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager",
182                     "-Djava.util.logging.config.file=" + tomcat.l("conf").l("logging.properties"),
183                     "-Djava.endorsed.dirs=" + tomcat.l("common").l("endorsed"),
184                     "-classpath", tomcatBin.l("bootstrap.jar") + s + tomcatBin.l("commons-logging-api.jar"),
185                     "-Dcatalina.base=" + tomcat,
186                     "-Dcatalina.home=" + tomcat,
187                     "-Djava.io.tmpdir=" + tomcat.l("temp"),
188                     "org.apache.catalina.startup.Bootstrap", command};
189         } else if (path.indexOf("tomcat-5.0") != -1) {
190             return new String JavaDoc[]{javaHome.l("bin").s("java"),
191                     "-Djava.endorsed.dirs=" + tomcat.l("common").l("endorsed"),
192                     "-classpath", tomcatBin.l("bootstrap.jar") + s + tomcatBin.l("commons-logging-api.jar") + s + javaHome.l("lib").s("tools.jar"),
193                     "-Dcatalina.base=" + tomcat,
194                     "-Dcatalina.home=" + tomcat,
195                     "-Djava.io.tmpdir=" + tomcat.l("temp"),
196                     "org.apache.catalina.startup.Bootstrap", command};
197         } else if (path.indexOf("tomcat-4.1") != -1) {
198             return new String JavaDoc[]{javaHome.l("bin").s("java"),
199                     "-Djava.endorsed.dirs=" + tomcat.l("common").l("endorsed"),
200                     "-classpath", tomcatBin.s("bootstrap.jar") + s + javaHome.l("lib").s("tools.jar"),
201                     "-Dcatalina.base=" + tomcat,
202                     "-Dcatalina.home=" + tomcat,
203                     "-Djava.io.tmpdir=" + tomcat.l("temp"),
204                     "org.apache.catalina.startup.Bootstrap", command};
205         } else {
206             throw new IllegalArgumentException JavaDoc("Unsupported Tomcat version: " + tomcatHome.getName());
207         }
208     }
209
210     public static class FilePathBuilder {
211         private final File JavaDoc file;
212
213         public FilePathBuilder(File JavaDoc file) {
214             this.file = file;
215         }
216
217         public FilePathBuilder(String JavaDoc filePath) {
218             this.file = new File JavaDoc(filePath);
219         }
220
221         public FilePathBuilder l(String JavaDoc name) {
222             return new FilePathBuilder(f(name));
223         }
224
225         public File JavaDoc f(String JavaDoc name) {
226             return new File JavaDoc(file, name);
227         }
228
229         public String JavaDoc s(String JavaDoc name) {
230             return new File JavaDoc(file, name).getAbsolutePath();
231         }
232
233         public String JavaDoc toString() {
234             return file.getAbsolutePath();
235         }
236     }
237
238     private static final class Pipe implements Runnable JavaDoc {
239         private final InputStream JavaDoc is;
240         private final OutputStream JavaDoc out;
241
242         private Pipe(InputStream JavaDoc is, OutputStream JavaDoc out) {
243             super();
244             this.is = is;
245             this.out = out;
246         }
247
248         public void run() {
249             try {
250                 int i = is.read();
251                 out.write(i);
252
253                 while (i != -1) {
254                     i = is.read();
255                     out.write(i);
256                 }
257
258             } catch (Exception JavaDoc e) {
259                 e.printStackTrace();
260             }
261         }
262     }
263
264 /**
265
266  5.5.x startup
267
268
269
270  5.0.x
271
272  -Djava.endorsed.dirs=/Users/dblevins/work/openejb1/target/jakarta-tomcat-5.0.30/common/endorsed
273  -classpath
274  /System/Library/Frameworks/JavaVM.framework/Versions/1.4/Home/lib/tools.jar:/Users/dblevins/work/openejb1/target/jakarta-tomcat-5.0.30/bin/bootstrap.jar:/Users/dblevins/work/openejb1/target/jakarta-tomcat-5.0.30/bin/commons-logging-api.jar
275  -Dcatalina.base=/Users/dblevins/work/openejb1/target/jakarta-tomcat-5.0.30
276  -Dcatalina.home=/Users/dblevins/work/openejb1/target/jakarta-tomcat-5.0.30
277  -Djava.io.tmpdir=/Users/dblevins/work/openejb1/target/jakarta-tomcat-5.0.30/temp
278  org.apache.catalina.startup.Bootstrap
279  start
280
281  4.1.x
282
283  -Djava.endorsed.dirs=/Users/dblevins/work/openejb1/target/jakarta-tomcat-4.1.31/common/endorsed
284  -classpath
285  /System/Library/Frameworks/JavaVM.framework/Versions/1.4/Home/lib/tools.jar:/Users/dblevins/work/openejb1/target/jakarta-tomcat-4.1.31/bin/bootstrap.jar
286  -Dcatalina.base=/Users/dblevins/work/openejb1/target/jakarta-tomcat-4.1.31
287  -Dcatalina.home=/Users/dblevins/work/openejb1/target/jakarta-tomcat-4.1.31
288  -Djava.io.tmpdir=/Users/dblevins/work/openejb1/target/jakarta-tomcat-4.1.31/temp
289  org.apache.catalina.startup.Bootstrap
290  start
291
292  */

293
294
295 }
296
Popular Tags