1 88 89 package org.openejb.server; 90 91 import java.io.File ; 92 import java.io.InputStream ; 93 import java.io.OutputStream ; 94 import java.lang.reflect.Method ; 95 import java.net.Socket ; 96 import java.util.ArrayList ; 97 import java.util.Iterator ; 98 import java.util.Map ; 99 import java.util.Set ; 100 104 public class Start { 105 106 public static void main(String [] args) { 107 new Start().start(); 109 } 110 111 public boolean start(){ 112 if (!connect()) { 113 forkServerProcess(); 114 return connect(10); 115 } else { 116 System.out.println(":: server already started ::"); 117 return true; 118 } 119 } 120 121 private void forkServerProcess() { 122 try{ 123 ArrayList cmd = new ArrayList (); 124 125 String s = java.io.File.separator; 126 127 String java = System.getProperty("java.home")+s+"bin"+s+"java"; 128 129 cmd.add(java); 130 131 addSystemProperties(cmd); 132 133 cmd.add("-classpath"); 134 cmd.add(getClasspath()); 135 cmd.add("org.openejb.server.Main"); 136 137 String [] command = (String []) cmd.toArray(new String [0]); 138 139 Runtime runtime = Runtime.getRuntime(); 140 Process server = runtime.exec( command ); 141 142 InputStream out = server.getInputStream(); 144 Thread serverOut = new Thread (new Pipe(out, System.out)); 145 serverOut.setDaemon(true); 146 serverOut.start(); 147 148 InputStream err = server.getErrorStream(); 150 Thread serverErr = new Thread (new Pipe(err, System.err)); 151 serverErr.setDaemon(true); 152 serverErr.start(); 153 } catch (Exception e){ 154 throw new RuntimeException ("Cannot start the server."); 155 } 156 } 157 158 private void addSystemProperties(ArrayList cmd) { 159 Set set = System.getProperties().entrySet(); 160 for (Iterator iter = set.iterator(); iter.hasNext();) { 161 Map.Entry entry = (Map.Entry ) iter.next(); 162 String key = (String )entry.getKey(); 163 String value = (String )entry.getValue(); 164 if ( key.matches("^-X.*") ){ 165 cmd.add(key+value); 166 } else if ( !key.matches("^(java|javax|os|sun|user|file|awt|line|path)\\..*") ){ 167 cmd.add("-D"+key+"="+value); 168 } 169 } 170 } 171 172 private String getClasspath() { 173 String classpath = System.getProperty("java.class.path"); 174 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 175 String antLoader = "org.apache.tools.ant.AntClassLoader"; 176 if (cl.getClass().getName().equals(antLoader)){ 177 try { 178 Class ant = cl.getClass(); 179 Method getClasspath = ant.getMethod("getClasspath", new Class [0]); 180 classpath += File.pathSeparator + getClasspath.invoke(cl,new Object [0]); 181 } catch (Exception e){ 182 e.printStackTrace(); 183 } 184 } 185 return classpath; 186 } 187 188 private boolean connect() { 189 return connect( 1 ); 190 } 191 192 private boolean connect(int tries) { 193 try{ 194 Socket socket = new Socket ("localhost", 4201); 195 OutputStream out = socket.getOutputStream(); 196 } catch (Exception e){ 197 if ( tries < 2 ) { 198 return false; 199 } else { 200 try{ 201 Thread.sleep(2000); 202 } catch (Exception e2){ 203 e.printStackTrace(); 204 } 205 return connect(--tries); 206 } 207 } 208 return true; 209 } 210 211 private static final class Pipe implements Runnable { 212 private final InputStream is; 213 private final OutputStream out; 214 private Pipe(InputStream is, OutputStream out) { 215 super(); 216 this.is = is; 217 this.out = out; 218 } 219 public void run() { 220 try{ 221 int i = is.read(); 222 out.write( i ); 223 while ( i != -1 ){ 224 i = is.read(); 225 out.write( i ); 226 } 227 } catch (Exception e){ 228 e.printStackTrace(); 229 } 230 } 231 } 232 } 233 | Popular Tags |