1 45 package org.openejb.test; 46 47 import java.io.DataInputStream ; 48 import java.io.File ; 49 import java.net.URL ; 50 import java.util.Properties ; 51 52 import javax.naming.Context ; 53 54 64 public class RiTestServer implements TestServer { 65 66 protected Process server; 67 protected boolean startServerProcess; 68 protected String configFile; 69 protected String serverClassName = " org.openejb.ri.server.Server "; 70 protected String classPath; 71 protected DataInputStream in; 72 protected DataInputStream err; 73 protected String testHomePath; 74 protected File testHome; 75 76 80 public static final String TEST_HOME = "test.home"; 81 public static final String SERVER_CLASSPATH = "server.classpath"; 82 public static final String SERVER_CONFIG = "test.server.config"; 83 public static final String START_SERVER_PROCESS = "test.start.server.process"; 84 public static final String BAD_ENVIRONMENT_ERROR = "The following environment variables must be set before running the test suite:\n"; 85 86 87 static{ 88 System.setProperty("noBanner", "true"); 89 } 90 91 public RiTestServer(){} 92 93 public void init(Properties props){ 94 try{ 95 96 try{ 97 System.setSecurityManager(new TestSecurityManager()); 98 } catch (Exception e){ 99 e.printStackTrace(); 100 } 101 102 103 String tmp = props.getProperty(START_SERVER_PROCESS, "true").trim(); 104 startServerProcess = "true".equalsIgnoreCase(tmp); 105 106 110 if (!startServerProcess) return; 111 112 testHomePath = props.getProperty(TEST_HOME); 113 classPath = props.getProperty(SERVER_CLASSPATH); 114 configFile = props.getProperty(SERVER_CONFIG); 115 116 checkEnvironment(); 117 118 testHome = new File (testHomePath); 119 testHome = testHome.getAbsoluteFile(); 120 testHomePath = testHome.getAbsolutePath(); 121 122 prepareServerClasspath(); 123 }catch (Exception e){ 124 e.printStackTrace (); 125 System.exit(-1); 126 } 127 } 128 129 public void destroy(){ 130 131 } 132 133 139 public void start(){ 140 141 if (!startServerProcess) return; 142 143 String command = "java -classpath "+classPath+" "+serverClassName +" "+configFile; 144 try{ 145 server = Runtime.getRuntime().exec( command ); 146 in = new DataInputStream ( server.getInputStream()); 147 err = new DataInputStream ( server.getErrorStream()); 148 while(true){ 149 try{ 150 String line = in.readLine(); 151 System.out.println(line); 152 if (line == null || "[RI Server] Ready!".equals(line)) break; 153 154 } catch (Exception e){ break; } 155 } 156 157 Thread t = new Thread (new Runnable (){ 158 public void run(){ 159 while(true){ 160 try{ 161 String line = in.readLine(); 162 if ( line == null ) break; 163 System.out.println(line); 164 } catch (Exception e){ break; } 165 } 166 167 } 168 }); 169 t.start(); 170 Thread t2 = new Thread (new Runnable (){ 171 public void run(){ 172 while(true){ 173 try{ 174 String line = err.readLine(); 175 if ( line == null ) break; 176 } catch (Exception e){ break; } 178 } 179 180 } 181 }); 182 t2.start(); 183 } catch (Exception e){ 184 e.printStackTrace(); 185 } 186 } 187 188 public void stop(){ 189 if (!startServerProcess) return; 190 191 if (server != null) server.destroy(); 192 server = null; 193 try{ 194 in.close(); 195 err.close(); 196 } catch (Exception e){} 197 } 198 199 public Properties getContextEnvironment(){ 200 Properties properties = new Properties (); 201 properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.openejb.ri.server.RiInitCtxFactory"); 202 203 try{ 204 properties.put(Context.PROVIDER_URL, new URL ("http","127.0.0.1",1098,"")); 205 } catch (Exception e){} 206 207 properties.put(Context.SECURITY_PRINCIPAL, "STATEFUL_TEST_CLIENT"); 208 properties.put(Context.SECURITY_CREDENTIALS, "STATEFUL_TEST_CLIENT"); 209 210 return properties; 211 } 212 213 private String getConfFilePath(String confFileName){ 218 String str = getConfFile(confFileName).getAbsolutePath(); 219 return str; 220 } 221 222 private File getConfFile(String confFileName){ 223 return new File (testHome, confFileName); 224 } 225 226 private void checkEnvironment(){ 227 228 if ( testHomePath == null || classPath == null || configFile == null ) { 229 String error = BAD_ENVIRONMENT_ERROR; 230 error += ( testHomePath == null )? TEST_HOME +"\n" : ""; 231 error += ( classPath == null )? SERVER_CLASSPATH+"\n" : ""; 232 error += ( configFile == null )? SERVER_CONFIG +"\n" : ""; 233 throw new RuntimeException (error); 234 } 235 } 236 237 private void prepareServerClasspath(){ 238 char PS = File.pathSeparatorChar; 239 char FS = File.separatorChar; 240 241 String javaTools = System.getProperty("java.home")+FS+"lib"+FS+"tools.jar"; 242 classPath = classPath.replace('/', FS); 243 classPath = classPath.replace(':', PS); 244 classPath+= PS + javaTools; 245 } 246 251 } 252 | Popular Tags |