001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test;
017:
018: import java.net.URL;
019: import java.util.Properties;
020:
021: /**
022: * @version $Revision: 573752 $ $Date: 2007-09-07 16:19:41 -0700 $
023: */
024: public class TomcatRemoteTestServer implements TestServer {
025: private Properties properties;
026: private String serverUri;
027:
028: // private File tomcatHome;
029: //
030: // private boolean serverHasAlreadyBeenStarted = true;
031:
032: public void init(Properties props) {
033: properties = props;
034: serverUri = System.getProperty("openejb.server.uri",
035: "http://127.0.0.1:8080/openejb/ejb");
036: if (!serverUri.startsWith("http:")) {
037: throw new IllegalStateException(
038: "TomcatRemoteTestServer requires that openejb.server.uri property starts with http:");
039: }
040: // props.put("test.server.class", TomcatRemoteTestServer.class.getName());
041: props
042: .put("java.naming.factory.initial",
043: "org.apache.openejb.client.RemoteInitialContextFactory");
044: props.put("java.naming.provider.url", serverUri);
045:
046: // String homeProperty = System.getProperty("tomcat.home");
047: // if (homeProperty == null) {
048: // throw new IllegalStateException("The system property tomcat.home must be defined.");
049: // }
050:
051: // tomcatHome = new File(homeProperty);
052: //
053: // if (!tomcatHome.exists()) {
054: // throw new IllegalStateException("The tomcat.home directory does not exist: " + tomcatHome.getAbsolutePath());
055: // }
056: }
057:
058: public void start() {
059: if (connect()) {
060: return;
061: }
062:
063: // try {
064: // System.out.println("[] START TOMCAT SERVER");
065: // System.out.println("CATALINA_HOME = " + tomcatHome.getAbsolutePath());
066: //
067: // String systemInfo = "Java " + System.getProperty("java.version") + "; " + System.getProperty("os.name") + "/" + System.getProperty("os.version");
068: // System.out.println("SYSTEM_INFO = " + systemInfo);
069: //
070: // serverHasAlreadyBeenStarted = false;
071: //
072: //
073: // execBootstrap("start");
074: // } catch (Exception e) {
075: // e.printStackTrace();
076: // throw new RuntimeException("Cannot start the server: " + e.getClass().getName() + ": " + e.getMessage(), e);
077: // }
078: connect(10);
079: // Wait a wee bit longer for good measure
080: try {
081: Thread.sleep(5000);
082: } catch (Exception e) {
083: e.printStackTrace();
084: }
085: }
086:
087: public void stop() {
088: // if (!serverHasAlreadyBeenStarted) {
089: // try {
090: // System.out.println("[] STOP TOMCAT SERVER");
091: // execBootstrap("stop");
092: //
093: // disconnect(10);
094: // } catch (Exception e) {
095: // e.printStackTrace();
096: // }
097: // }
098: }
099:
100: // private void execBootstrap(String command) throws IOException {
101: // String[] bootstrapCommand = getBootstrapCommand(tomcatHome, command);
102: // Process server = Runtime.getRuntime().exec(bootstrapCommand);
103: //
104: // FilePathBuilder tomcat = new FilePathBuilder(tomcatHome);
105: // OutputStream catalinaOut = new FileOutputStream(tomcat.l("logs").f("catalina.out"));
106: //
107: // // Pipe the processes STDOUT to ours
108: // InputStream out = server.getInputStream();
109: // Thread serverOut = new Thread(new Pipe(out, catalinaOut));
110: //
111: // serverOut.setDaemon(true);
112: // serverOut.start();
113: //
114: // // Pipe the processes STDERR to ours
115: // InputStream err = server.getErrorStream();
116: // Thread serverErr = new Thread(new Pipe(err, catalinaOut));
117: //
118: // serverErr.setDaemon(true);
119: // serverErr.start();
120: // }
121:
122: public Properties getContextEnvironment() {
123: return (Properties) 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 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 url = new URL(serverUri);
151: url.openStream();
152: } catch (Exception 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 e2) {
161: e.printStackTrace();
162: }
163: return connect(tries);
164: }
165: }
166:
167: return true;
168: }
169:
170: // private String[] getBootstrapCommand(File tomcatHome, String 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 path = tomcatHome.getAbsolutePath();
176: //
177: // String s = File.pathSeparator;
178: //
179: // if (path.indexOf("tomcat-6") != -1) {
180: // return new String[]{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.5") != -1) {
190: // return new String[]{javaHome.l("bin").s("java"),
191: // "-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager",
192: // "-Djava.util.logging.config.file=" + tomcat.l("conf").l("logging.properties"),
193: // "-Djava.endorsed.dirs=" + tomcat.l("common").l("endorsed"),
194: // "-classpath", tomcatBin.l("bootstrap.jar") + s + tomcatBin.l("commons-logging-api.jar"),
195: // "-Dcatalina.base=" + tomcat,
196: // "-Dcatalina.home=" + tomcat,
197: // "-Djava.io.tmpdir=" + tomcat.l("temp"),
198: // "org.apache.catalina.startup.Bootstrap", command};
199: // } else if (path.indexOf("tomcat-5.0") != -1) {
200: // return new String[]{javaHome.l("bin").s("java"),
201: // "-Djava.endorsed.dirs=" + tomcat.l("common").l("endorsed"),
202: // "-classpath", tomcatBin.l("bootstrap.jar") + s + tomcatBin.l("commons-logging-api.jar") + s + javaHome.l("lib").s("tools.jar"),
203: // "-Dcatalina.base=" + tomcat,
204: // "-Dcatalina.home=" + tomcat,
205: // "-Djava.io.tmpdir=" + tomcat.l("temp"),
206: // "org.apache.catalina.startup.Bootstrap", command};
207: // } else if (path.indexOf("tomcat-4.1") != -1) {
208: // return new String[]{javaHome.l("bin").s("java"),
209: // "-Djava.endorsed.dirs=" + tomcat.l("common").l("endorsed"),
210: // "-classpath", tomcatBin.s("bootstrap.jar") + s + javaHome.l("lib").s("tools.jar"),
211: // "-Dcatalina.base=" + tomcat,
212: // "-Dcatalina.home=" + tomcat,
213: // "-Djava.io.tmpdir=" + tomcat.l("temp"),
214: // "org.apache.catalina.startup.Bootstrap", command};
215: // } else {
216: // throw new IllegalArgumentException("Unsupported Tomcat version: " + tomcatHome.getName());
217: // }
218: // }
219: //
220: // public static class FilePathBuilder {
221: // private final File file;
222: //
223: // public FilePathBuilder(File file) {
224: // this.file = file;
225: // }
226: //
227: // public FilePathBuilder(String filePath) {
228: // this.file = new File(filePath);
229: // }
230: //
231: // public FilePathBuilder l(String name) {
232: // return new FilePathBuilder(f(name));
233: // }
234: //
235: // public File f(String name) {
236: // return new File(file, name);
237: // }
238: //
239: // public String s(String name) {
240: // return new File(file, name).getAbsolutePath();
241: // }
242: //
243: // public String toString() {
244: // return file.getAbsolutePath();
245: // }
246: // }
247: //
248: // private static final class Pipe implements Runnable {
249: // private final InputStream is;
250: // private final OutputStream out;
251: //
252: // private Pipe(InputStream is, OutputStream out) {
253: // super();
254: // this.is = is;
255: // this.out = out;
256: // }
257: //
258: // public void run() {
259: // try {
260: // int i = is.read();
261: // out.write(i);
262: //
263: // while (i != -1) {
264: // i = is.read();
265: // out.write(i);
266: // }
267: //
268: // } catch (Exception e) {
269: // e.printStackTrace();
270: // }
271: // }
272: // }
273:
274: /**
275:
276: 5.5.x startup
277:
278:
279:
280: 5.0.x
281:
282: -Djava.endorsed.dirs=/Users/dblevins/work/openejb1/target/jakarta-tomcat-5.0.30/common/endorsed
283: -classpath
284: /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
285: -Dcatalina.base=/Users/dblevins/work/openejb1/target/jakarta-tomcat-5.0.30
286: -Dcatalina.home=/Users/dblevins/work/openejb1/target/jakarta-tomcat-5.0.30
287: -Djava.io.tmpdir=/Users/dblevins/work/openejb1/target/jakarta-tomcat-5.0.30/temp
288: org.apache.catalina.startup.Bootstrap
289: start
290:
291: 4.1.x
292:
293: -Djava.endorsed.dirs=/Users/dblevins/work/openejb1/target/jakarta-tomcat-4.1.31/common/endorsed
294: -classpath
295: /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
296: -Dcatalina.base=/Users/dblevins/work/openejb1/target/jakarta-tomcat-4.1.31
297: -Dcatalina.home=/Users/dblevins/work/openejb1/target/jakarta-tomcat-4.1.31
298: -Djava.io.tmpdir=/Users/dblevins/work/openejb1/target/jakarta-tomcat-4.1.31/temp
299: org.apache.catalina.startup.Bootstrap
300: start
301:
302: */
303:
304: }
|