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.io.File;
019: import java.io.FileNotFoundException;
020: import java.io.FileOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.net.Socket;
025: import java.util.Properties;
026:
027: /**
028: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
029: */
030: public class RemoteTestServer implements
031: org.apache.openejb.test.TestServer {
032:
033: static {
034: System.setProperty("noBanner", "true");
035: }
036:
037: /**
038: * Has the remote server's instance been already running ?
039: */
040: private boolean serverHasAlreadyBeenStarted = true;
041:
042: private Properties properties;
043:
044: public void init(Properties props) {
045: properties = props;
046: if (props.contains("java.naming.security.principal"))
047: throw new IllegalArgumentException(
048: "Not allowed 'java.naming.security.principal'");
049: // props.put("test.server.class","org.apache.openejb.test.RemoteTestServer");
050: props
051: .put("java.naming.factory.initial",
052: "org.apache.openejb.client.RemoteInitialContextFactory");
053: props.put("java.naming.provider.url", "127.0.0.1:4201");
054: // props.put("java.naming.security.principal", "testuser");
055: // props.put("java.naming.security.credentials", "testpassword");
056: }
057:
058: public Properties getProperties() {
059: return properties;
060: }
061:
062: public void destroy() {
063: }
064:
065: public void start() {
066: if (!connect()) {
067: try {
068: System.out.println("[] START SERVER");
069:
070: String openejbHome = System.getProperty("openejb.home");
071:
072: File home = new File(openejbHome);
073: System.out.println("OPENEJB_HOME = "
074: + home.getAbsolutePath());
075: String systemInfo = "Java "
076: + System.getProperty("java.version") + "; "
077: + System.getProperty("os.name") + "/"
078: + System.getProperty("os.version");
079: System.out.println("SYSTEM_INFO = " + systemInfo);
080:
081: serverHasAlreadyBeenStarted = false;
082: String version = null;
083:
084: File openejbJar = null;
085: File lib = new File(home, "lib");
086: File[] files = lib.listFiles();
087: for (int i = 0; i < files.length && openejbJar == null; i++) {
088: File file = files[i];
089: if (file.getName().startsWith("openejb-core")
090: && file.getName().endsWith("jar")) {
091: openejbJar = file;
092: }
093: }
094:
095: if (openejbJar == null) {
096: throw new IllegalStateException(
097: "Cannot find the openejb-core jar in "
098: + lib.getAbsolutePath());
099: }
100:
101: //File openejbJar = new File(lib, "openejb-core-" + version + ".jar");
102:
103: //DMB: If you don't use an array, you get problems with jar paths containing spaces
104: // the command won't parse correctly
105: String[] args = { "java", "-jar",
106: openejbJar.getAbsolutePath(), "start" };
107: Process server = Runtime.getRuntime().exec(args);
108:
109: // Pipe the processes STDOUT to ours
110: InputStream out = server.getInputStream();
111: Thread serverOut = new Thread(new Pipe(out, System.out));
112:
113: serverOut.setDaemon(true);
114: serverOut.start();
115:
116: // Pipe the processes STDERR to ours
117: InputStream err = server.getErrorStream();
118: Thread serverErr = new Thread(new Pipe(err, System.err));
119:
120: serverErr.setDaemon(true);
121: serverErr.start();
122: } catch (Exception e) {
123: throw (RuntimeException) new RuntimeException(
124: "Cannot start the server.").initCause(e);
125: }
126: connect(10);
127: } else {
128: //System.out.println("[] SERVER STARTED");
129: }
130: }
131:
132: private void oldStart() throws IOException, FileNotFoundException {
133: String s = java.io.File.separator;
134: String java = System.getProperty("java.home") + s + "bin" + s
135: + "java";
136: String classpath = System.getProperty("java.class.path");
137: String openejbHome = System.getProperty("openejb.home");
138:
139: String[] cmd = new String[5];
140: cmd[0] = java;
141: cmd[1] = "-classpath";
142: cmd[2] = classpath;
143: cmd[3] = "-Dopenejb.home=" + openejbHome;
144: cmd[4] = "org.apache.openejb.server.Main";
145: for (int i = 0; i < cmd.length; i++) {
146: //System.out.println("[] "+cmd[i]);
147: }
148:
149: Process remoteServerProcess = Runtime.getRuntime().exec(cmd);
150:
151: // it seems as if OpenEJB wouldn't start up till the output stream was read
152: final java.io.InputStream is = remoteServerProcess
153: .getInputStream();
154: final java.io.OutputStream out = new FileOutputStream(
155: "logs/testsuite.out");
156: Thread serverOut = new Thread(new Runnable() {
157: public void run() {
158: try {
159: //while ( is.read() != -1 );
160: int i = is.read();
161: out.write(i);
162: while (i != -1) {
163: //System.out.write( i );
164: i = is.read();
165: out.write(i);
166: }
167: } catch (Exception e) {
168: e.printStackTrace();
169: }
170: }
171: });
172: serverOut.setDaemon(true);
173: serverOut.start();
174:
175: final java.io.InputStream is2 = remoteServerProcess
176: .getErrorStream();
177: Thread serverErr = new Thread(new Runnable() {
178: public void run() {
179: try {
180: //while ( is.read() != -1 );
181: int i = is2.read();
182: out.write(i);
183: while (i != -1) {
184: //System.out.write( i );
185: i = is2.read();
186: out.write(i);
187: }
188: } catch (Exception e) {
189: e.printStackTrace();
190: }
191: }
192: });
193: serverErr.setDaemon(true);
194: serverErr.start();
195: }
196:
197: public void stop() {
198: if (!serverHasAlreadyBeenStarted) {
199: try {
200: System.out.println("[] STOP SERVER");
201:
202: Socket socket = new Socket("localhost", 4200);
203: OutputStream out = socket.getOutputStream();
204:
205: out.write("Stop".getBytes());
206:
207: } catch (Exception e) {
208: e.printStackTrace();
209: }
210: }
211: }
212:
213: public Properties getContextEnvironment() {
214: return (Properties) properties.clone();
215: }
216:
217: private boolean connect() {
218: return connect(1);
219: }
220:
221: private boolean connect(int tries) {
222: //System.out.println("CONNECT "+ tries);
223: try {
224: Socket socket = new Socket("localhost", 4200);
225: OutputStream out = socket.getOutputStream();
226: } catch (Exception e) {
227: //System.out.println(e.getMessage());
228: if (tries < 2) {
229: return false;
230: } else {
231: try {
232: Thread.sleep(2000);
233: } catch (Exception e2) {
234: e.printStackTrace();
235: }
236: return connect(--tries);
237: }
238: }
239:
240: return true;
241: }
242:
243: private static final class Pipe implements Runnable {
244: private final InputStream is;
245: private final OutputStream out;
246:
247: private Pipe(InputStream is, OutputStream out) {
248: super ();
249: this .is = is;
250: this .out = out;
251: }
252:
253: public void run() {
254: try {
255: int i = is.read();
256: out.write(i);
257:
258: while (i != -1) {
259: i = is.read();
260: out.write(i);
261: }
262:
263: } catch (Exception e) {
264: e.printStackTrace();
265: }
266: }
267: }
268: }
|