1 45 package org.openejb.test; 46 47 import java.io.File ; 48 import java.io.FileNotFoundException ; 49 import java.io.FileOutputStream ; 50 import java.io.IOException ; 51 import java.io.InputStream ; 52 import java.io.OutputStream ; 53 import java.net.Socket ; 54 import java.net.URL ; 55 import java.util.Properties ; 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 66 public class RemoteTestServer implements org.openejb.test.TestServer { 67 68 static{ 69 System.setProperty("noBanner", "true"); 70 } 71 72 75 private boolean serverHasAlreadyBeenStarted = true; 76 77 private Properties properties; 78 79 public void init(Properties props){ 80 properties = props; 81 82 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 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 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 version = null; 108 109 JarUtils.setHandlerSystemProperty(); 110 Properties versionInfo = new Properties (); 111 versionInfo.load( new URL ( "resource:/openejb-version.properties" ).openConnection().getInputStream() ); 112 version = (String )versionInfo.get( "version" ); 113 114 File lib = home.getDirectory("lib"); 115 File openejbJar = new File (lib, "openejb-core-" + version + ".jar"); 116 117 String [] args = {"java", "-jar", openejbJar.getAbsolutePath(), "start"}; 120 Process server = Runtime.getRuntime().exec(args); 121 122 InputStream out = server.getInputStream(); 124 Thread serverOut = new Thread (new Pipe(out, System.out)); 125 126 serverOut.setDaemon(true); 127 serverOut.start(); 128 129 InputStream err = server.getErrorStream(); 131 Thread serverErr = new Thread (new Pipe(err, System.err)); 132 133 serverErr.setDaemon(true); 134 serverErr.start(); 135 } catch (Exception e){ 136 throw new RuntimeException ("Cannot start the server."); 137 } 138 connect(10); 139 } else { 140 } 142 } 143 144 private void oldStart() throws IOException , FileNotFoundException { 145 String s = java.io.File.separator; 146 String java = System.getProperty("java.home")+s+"bin"+s+"java"; 147 String classpath = System.getProperty("java.class.path"); 148 String openejbHome = System.getProperty("openejb.home"); 149 150 151 String [] cmd = new String [ 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 } 160 161 Process remoteServerProcess = Runtime.getRuntime().exec( cmd ); 162 163 final java.io.InputStream is = remoteServerProcess.getInputStream(); 165 final java.io.OutputStream out = new FileOutputStream ("logs/testsuite.out"); 166 Thread serverOut = new Thread (new Runnable (){ 167 public void run() { 168 try{ 169 int i = is.read(); 171 out.write( i ); 172 while ( i != -1 ){ 173 i = is.read(); 175 out.write( i ); 176 } 177 } catch (Exception e){ 178 e.printStackTrace(); 179 } 180 } 181 }); 182 serverOut.setDaemon(true); 183 serverOut.start(); 184 185 final java.io.InputStream is2 = remoteServerProcess.getErrorStream(); 186 Thread serverErr = new Thread (new Runnable (){ 187 public void run() { 188 try{ 189 int i = is2.read(); 191 out.write( i ); 192 while ( i != -1 ){ 193 i = is2.read(); 195 out.write( i ); 196 } 197 } catch (Exception 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 socket = new Socket ("localhost", 4200); 212 OutputStream out = socket.getOutputStream(); 213 214 out.write( "Stop".getBytes() ); 215 216 } catch (Exception e){ 217 e.printStackTrace(); 218 } 219 } 220 } 221 222 public Properties getContextEnvironment(){ 223 return (Properties )properties.clone(); 224 } 225 226 private boolean connect() { 227 return connect( 1 ); 228 } 229 230 private boolean connect(int tries) { 231 try{ 233 Socket socket = new Socket ("localhost", 4200); 234 OutputStream out = socket.getOutputStream(); 235 } catch (Exception e){ 236 if ( tries < 2 ) { 238 return false; 239 } else { 240 try{ 241 Thread.sleep(2000); 242 } catch (Exception 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 { 253 private final InputStream is; 254 private final OutputStream out; 255 256 private Pipe(InputStream is, OutputStream 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 e){ 273 e.printStackTrace(); 274 } 275 } 276 } 277 } 278 | Popular Tags |