1 17 package org.openejb.test; 18 19 import org.openejb.client.RemoteInitialContextFactory; 20 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.net.URL ; 27 import java.util.Properties ; 28 29 32 public class TomcatRemoteTestServer implements TestServer { 33 private Properties properties; 34 private String servletUrl; 35 private File tomcatHome; 36 37 private boolean serverHasAlreadyBeenStarted = true; 38 39 public void init(Properties props) { 40 properties = props; 41 servletUrl = System.getProperty("remote.serlvet.url", "http://127.0.0.1:8080/openejb/remote"); 42 props.put("java.naming.factory.initial", RemoteInitialContextFactory.class.getName()); 44 props.put("java.naming.provider.url", servletUrl); 45 46 String homeProperty = System.getProperty("tomcat.home"); 47 if (homeProperty == null) { 48 throw new IllegalStateException ("The system property tomcat.home must be defined."); 49 } 50 51 tomcatHome = new File (homeProperty); 52 53 if (!tomcatHome.exists()) { 54 throw new IllegalStateException ("The tomcat.home directory does not exist: " + tomcatHome.getAbsolutePath()); 55 } 56 } 57 58 public void start() { 59 if (connect()) { 60 return; 61 } 62 63 try { 64 System.out.println("[] START TOMCAT SERVER"); 65 System.out.println("CATALINA_HOME = " + tomcatHome.getAbsolutePath()); 66 67 String systemInfo = "Java " + System.getProperty("java.version") + "; " + System.getProperty("os.name") + "/" + System.getProperty("os.version"); 68 System.out.println("SYSTEM_INFO = " + systemInfo); 69 70 serverHasAlreadyBeenStarted = false; 71 72 73 execBootstrap("start"); 74 } catch (Exception e) { 75 e.printStackTrace(); 76 throw new RuntimeException ("Cannot start the server: " + e.getClass().getName() + ": " + e.getMessage(), e); 77 } 78 connect(10); 79 try { 81 Thread.sleep(5000); 82 } catch (Exception e) { 83 e.printStackTrace(); 84 } 85 } 86 87 public void stop() { 88 if (!serverHasAlreadyBeenStarted) { 89 try { 90 System.out.println("[] STOP TOMCAT SERVER"); 91 execBootstrap("stop"); 92 93 disconnect(10); 94 } catch (Exception e) { 95 e.printStackTrace(); 96 } 97 } 98 } 99 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 InputStream out = server.getInputStream(); 109 Thread serverOut = new Thread (new Pipe(out, catalinaOut)); 110 111 serverOut.setDaemon(true); 112 serverOut.start(); 113 114 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 try { 150 URL url = new URL (servletUrl); 151 url.openStream(); 152 } catch (Exception e) { 153 tries--; 154 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-5.5") != -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.0") != -1) { 190 return new String []{javaHome.l("bin").s("java"), 191 "-Djava.endorsed.dirs=" + tomcat.l("common").l("endorsed"), 192 "-classpath", tomcatBin.l("bootstrap.jar") + s + tomcatBin.l("commons-logging-api.jar") + s + javaHome.l("lib").s("tools.jar"), 193 "-Dcatalina.base=" + tomcat, 194 "-Dcatalina.home=" + tomcat, 195 "-Djava.io.tmpdir=" + tomcat.l("temp"), 196 "org.apache.catalina.startup.Bootstrap", command}; 197 } else if (path.indexOf("tomcat-4.1") != -1) { 198 return new String []{javaHome.l("bin").s("java"), 199 "-Djava.endorsed.dirs=" + tomcat.l("common").l("endorsed"), 200 "-classpath", tomcatBin.s("bootstrap.jar") + s + javaHome.l("lib").s("tools.jar"), 201 "-Dcatalina.base=" + tomcat, 202 "-Dcatalina.home=" + tomcat, 203 "-Djava.io.tmpdir=" + tomcat.l("temp"), 204 "org.apache.catalina.startup.Bootstrap", command}; 205 } else { 206 throw new IllegalArgumentException ("Unsupported Tomcat version: " + tomcatHome.getName()); 207 } 208 } 209 210 public static class FilePathBuilder { 211 private final File file; 212 213 public FilePathBuilder(File file) { 214 this.file = file; 215 } 216 217 public FilePathBuilder(String filePath) { 218 this.file = new File (filePath); 219 } 220 221 public FilePathBuilder l(String name) { 222 return new FilePathBuilder(f(name)); 223 } 224 225 public File f(String name) { 226 return new File (file, name); 227 } 228 229 public String s(String name) { 230 return new File (file, name).getAbsolutePath(); 231 } 232 233 public String toString() { 234 return file.getAbsolutePath(); 235 } 236 } 237 238 private static final class Pipe implements Runnable { 239 private final InputStream is; 240 private final OutputStream out; 241 242 private Pipe(InputStream is, OutputStream out) { 243 super(); 244 this.is = is; 245 this.out = out; 246 } 247 248 public void run() { 249 try { 250 int i = is.read(); 251 out.write(i); 252 253 while (i != -1) { 254 i = is.read(); 255 out.write(i); 256 } 257 258 } catch (Exception e) { 259 e.printStackTrace(); 260 } 261 } 262 } 263 264 293 294 295 } 296 | Popular Tags |