1 28 package org.objectweb.util.launcher; 29 30 31 import org.objectweb.util.launcher.option.OptionContext; 32 import org.objectweb.util.launcher.option.OptionRun ; 33 import org.objectweb.util.trace.TraceSystem; 34 35 42 public class Launcher 43 { 44 50 51 protected String runid_ ; 52 53 protected String opt_ ; 54 55 protected String file_ ; 56 57 protected String args_ ; 58 59 protected String classPath_ = " -classpath "; 60 61 protected Process process; 62 63 69 73 private class ForkThread extends Thread { 74 75 76 private String toExecute ; 77 78 private Process p=null; 79 80 84 public ForkThread(String toExecute ){ 85 this.toExecute=toExecute; 86 } 87 88 91 public void run(){ 92 try{ 93 p=Runtime.getRuntime().exec(toExecute); 95 96 } 97 catch(Exception e){ 98 System.out.println("Error occured in Thread"+e.getMessage()); 99 e.printStackTrace(); 100 } 101 } 102 103 106 public Process getProcess(){ 107 return p; 108 } 109 110 } 111 112 118 121 public Launcher() { 122 this.runid_ = "" ; 123 this.opt_ = "" ; 124 this.file_ = "" ; 125 this.args_ = "" ; 126 } 127 128 131 public Launcher(String targetXMLFile, 132 String runID, 133 String targetArgs[]) 134 { 135 try{ 136 String toExecute=System.getProperty("java.home")+"/bin/java"+ 138 classPath_ +" "+System.getProperty("java.class.path")+ 139 " org.objectweb.util.launcher.Launcher "; 140 141 for(int i=0; i<targetArgs.length;i++) 143 toExecute+=targetArgs[i]+" "; 144 145 toExecute+="--runid "+runID+" "+targetXMLFile; 147 148 ForkThread ft=new ForkThread(toExecute); 150 ft.start(); 152 while(ft.getProcess()==null) 154 Thread.sleep(1000); 155 this.process=ft.getProcess(); 157 } 158 catch(Exception e){ 159 System.err.println("Cannot launch: "+e.getMessage()); 160 e.printStackTrace(); 161 } 162 } 163 164 173 protected class ProcessStarter 174 extends Thread 175 { 176 177 protected String toExecute ; 178 179 protected Process p=null; 180 181 184 public Process getProcess() { 185 return p; 186 } 187 188 192 public ProcessStarter(String toLaunch ) { 193 toExecute = System.getProperty("java.home") + 194 "/bin/java -classpath " + 195 System.getProperty("java.class.path") + 196 " org.objectweb.util.launcher.Launcher " + 197 toLaunch; 198 } 199 200 203 public void run() { 204 try { p=Runtime.getRuntime().exec(toExecute); 206 } catch(Exception ex) { 207 TraceSystem.get("launcher").error("Exception raised: "+ex); 208 throw new LauncherException(ex); 209 } 210 } 211 } 212 213 214 218 public void setRunid(String run){ 219 runid_ += OptionRun.create(run); 220 } 221 222 226 public void addContext(String opt) { 227 opt_ += OptionContext.create(opt); 228 } 229 230 234 public void setFile(String file){ 235 file_ = file ; 236 } 237 238 242 public void addArgs(String args) { 243 args_ += " " + args ; 244 } 245 246 250 private String getCommandLine() { 251 return runid_ + " " + opt_ + " " + " " + file_ ; 252 } 253 254 258 public Process start() { 259 try { 260 ProcessStarter ps = new ProcessStarter(getCommandLine()); 261 ps.start(); 263 while(ps.getProcess()==null) 265 Thread.sleep(1000); 266 return ps.getProcess(); 267 } catch(Exception ex) { 268 TraceSystem.get("launcher").error("Exception raised: " + ex); 269 throw new LauncherException(ex); 270 } 271 } 272 273 274 280 283 public Process 284 getProcess() 285 { 286 return process; 287 } 288 289 294 public static void 295 main(String [] args) { 296 LauncherApplication application = new LauncherApplication(); 297 application.runMain(args); 298 } 299 } 300 | Popular Tags |