KickJava   Java API By Example, From Geeks To Geeks.

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


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

45 package org.openejb.test;
46
47 import java.io.File JavaDoc;
48 import java.io.FileNotFoundException JavaDoc;
49 import java.io.FileOutputStream JavaDoc;
50 import java.io.IOException JavaDoc;
51 import java.io.InputStream JavaDoc;
52 import java.io.OutputStream JavaDoc;
53 import java.net.Socket JavaDoc;
54 import java.net.URL JavaDoc;
55 import java.util.Properties JavaDoc;
56
57 import org.openejb.util.JarUtils;
58 import org.openejb.util.FileUtils;
59 import org.openejb.client.RemoteInitialContextFactory;
60 import org.openejb.loader.SystemInstance;
61
62 /**
63  *
64  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
65  */

66 public class RemoteTestServer implements org.openejb.test.TestServer {
67
68     static{
69         System.setProperty("noBanner", "true");
70     }
71
72     /**
73      * Has the remote server's instance been already running ?
74      */

75     private boolean serverHasAlreadyBeenStarted = true;
76
77     private Properties JavaDoc properties;
78
79     public void init(Properties JavaDoc props){
80         properties = props;
81
82 // props.put("test.server.class","org.openejb.test.RemoteTestServer");
83
props.put("java.naming.factory.initial","org.openejb.client.RemoteInitialContextFactory");
84         props.put("java.naming.provider.url","127.0.0.1:4201");
85         props.put("java.naming.security.principal","testuser");
86         props.put("java.naming.security.credentials","testpassword");
87     }
88
89     public Properties JavaDoc getProperties() {
90         return properties;
91     }
92
93     public void destroy(){
94     }
95
96     public void start(){
97         if (!connect()) {
98             try{
99                 System.out.println("[] START SERVER");
100                 FileUtils home = SystemInstance.get().getHome();
101
102                 System.out.println("OPENEJB_HOME = "+home.getDirectory().getAbsolutePath());
103                 String JavaDoc systemInfo = "Java " + System.getProperty("java.version") + "; " + System.getProperty("os.name") + "/" + System.getProperty("os.version");
104                 System.out.println("SYSTEM_INFO = "+systemInfo);
105
106                 serverHasAlreadyBeenStarted = false;
107                 String JavaDoc version = null;
108
109                 JarUtils.setHandlerSystemProperty();
110                 Properties JavaDoc versionInfo = new Properties JavaDoc();
111                 versionInfo.load( new URL JavaDoc( "resource:/openejb-version.properties" ).openConnection().getInputStream() );
112                 version = (String JavaDoc)versionInfo.get( "version" );
113
114                 File JavaDoc lib = home.getDirectory("lib");
115                 File JavaDoc openejbJar = new File JavaDoc(lib, "openejb-core-" + version + ".jar");
116
117                 //DMB: If you don't use an array, you get problems with jar paths containing spaces
118
// the command won't parse correctly
119
String JavaDoc[] args = {"java", "-jar", openejbJar.getAbsolutePath(), "start"};
120                 Process JavaDoc server = Runtime.getRuntime().exec(args);
121
122                 // Pipe the processes STDOUT to ours
123
InputStream JavaDoc out = server.getInputStream();
124                 Thread JavaDoc serverOut = new Thread JavaDoc(new Pipe(out, System.out));
125
126                 serverOut.setDaemon(true);
127                 serverOut.start();
128
129                 // Pipe the processes STDERR to ours
130
InputStream JavaDoc err = server.getErrorStream();
131                 Thread JavaDoc serverErr = new Thread JavaDoc(new Pipe(err, System.err));
132
133                 serverErr.setDaemon(true);
134                 serverErr.start();
135             } catch (Exception JavaDoc e){
136                 throw new RuntimeException JavaDoc("Cannot start the server.");
137             }
138             connect(10);
139         } else {
140             //System.out.println("[] SERVER STARTED");
141
}
142     }
143
144     private void oldStart() throws IOException JavaDoc, FileNotFoundException JavaDoc {
145         String JavaDoc s = java.io.File.separator;
146         String JavaDoc java = System.getProperty("java.home")+s+"bin"+s+"java";
147         String JavaDoc classpath = System.getProperty("java.class.path");
148         String JavaDoc openejbHome = System.getProperty("openejb.home");
149
150
151         String JavaDoc[] cmd = new String JavaDoc[ 5 ];
152         cmd[ 0 ] = java;
153         cmd[ 1 ] = "-classpath";
154         cmd[ 2 ] = classpath;
155         cmd[ 3 ] = "-Dopenejb.home="+openejbHome;
156         cmd[ 4 ] = "org.openejb.server.Main";
157         for (int i=0; i < cmd.length; i++){
158             //System.out.println("[] "+cmd[i]);
159
}
160
161         Process JavaDoc remoteServerProcess = Runtime.getRuntime().exec( cmd );
162
163         // it seems as if OpenEJB wouldn't start up till the output stream was read
164
final java.io.InputStream JavaDoc is = remoteServerProcess.getInputStream();
165         final java.io.OutputStream JavaDoc out = new FileOutputStream JavaDoc("logs/testsuite.out");
166         Thread JavaDoc serverOut = new Thread JavaDoc(new Runnable JavaDoc(){
167                 public void run() {
168                     try{
169                         //while ( is.read() != -1 );
170
int i = is.read();
171                         out.write( i );
172                         while ( i != -1 ){
173                             //System.out.write( i );
174
i = is.read();
175                             out.write( i );
176                         }
177                     } catch (Exception JavaDoc e){
178                         e.printStackTrace();
179                     }
180                 }
181         });
182         serverOut.setDaemon(true);
183         serverOut.start();
184
185         final java.io.InputStream JavaDoc is2 = remoteServerProcess.getErrorStream();
186         Thread JavaDoc serverErr = new Thread JavaDoc(new Runnable JavaDoc(){
187                 public void run() {
188                     try{
189                         //while ( is.read() != -1 );
190
int i = is2.read();
191                         out.write( i );
192                         while ( i != -1 ){
193                             //System.out.write( i );
194
i = is2.read();
195                             out.write( i );
196                         }
197                     } catch (Exception JavaDoc e){
198                         e.printStackTrace();
199                     }
200                 }
201         });
202         serverErr.setDaemon(true);
203         serverErr.start();
204     }
205
206     public void stop(){
207         if ( !serverHasAlreadyBeenStarted ) {
208             try{
209                 System.out.println("[] STOP SERVER");
210
211                 Socket JavaDoc socket = new Socket JavaDoc("localhost", 4200);
212                 OutputStream JavaDoc out = socket.getOutputStream();
213
214                 out.write( "Stop".getBytes() );
215
216             } catch (Exception JavaDoc e){
217                 e.printStackTrace();
218             }
219         }
220     }
221
222     public Properties JavaDoc getContextEnvironment(){
223         return (Properties JavaDoc)properties.clone();
224     }
225
226     private boolean connect() {
227         return connect( 1 );
228     }
229
230     private boolean connect(int tries) {
231         //System.out.println("CONNECT "+ tries);
232
try{
233             Socket JavaDoc socket = new Socket JavaDoc("localhost", 4200);
234             OutputStream JavaDoc out = socket.getOutputStream();
235         } catch (Exception JavaDoc e){
236             //System.out.println(e.getMessage());
237
if ( tries < 2 ) {
238                 return false;
239             } else {
240                 try{
241                     Thread.sleep(2000);
242                 } catch (Exception JavaDoc e2){
243                     e.printStackTrace();
244                 }
245                 return connect(--tries);
246             }
247         }
248
249         return true;
250     }
251
252     private static final class Pipe implements Runnable JavaDoc {
253         private final InputStream JavaDoc is;
254         private final OutputStream JavaDoc out;
255
256         private Pipe(InputStream JavaDoc is, OutputStream JavaDoc out) {
257             super();
258             this.is = is;
259             this.out = out;
260         }
261
262         public void run() {
263             try{
264                 int i = is.read();
265                 out.write( i );
266
267                 while ( i != -1 ){
268                     i = is.read();
269                     out.write( i );
270                 }
271
272             } catch (Exception JavaDoc e){
273                 e.printStackTrace();
274             }
275         }
276     }
277 }
278
Popular Tags