1 28 29 package org.objectweb.openccm.task; 30 31 import java.io.File ; 33 import java.util.ArrayList ; 34 import java.util.Enumeration ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.Properties ; 39 import org.apache.tools.ant.BuildException; 40 41 48 public abstract class LauncherApplication 49 { 50 56 57 protected Properties project_properties_; 58 59 60 private Properties properties_; 61 62 63 private List pathes_; 64 65 66 private List arguments_; 67 68 69 private String xml_file_; 70 71 72 private String run_id_; 73 74 75 private Process process_; 76 77 78 private String arg_delimiter_; 79 80 81 private File working_directory_; 82 83 89 92 public LauncherApplication() 93 { 94 project_properties_ = new Properties (); 95 properties_ = new Properties (); 96 pathes_ = new ArrayList (); 97 arguments_ = new ArrayList (); 98 run_id_ = null; 99 xml_file_ = null; 100 process_ = null; 101 if ( System.getProperty("file.separator").compareTo("\\") == 0 ) 102 arg_delimiter_ = "\""; 103 else 104 arg_delimiter_ = ""; 105 working_directory_ = null; 106 } 107 108 113 public LauncherApplication(Map project_properties) 114 { 115 this(); 116 project_properties_.putAll(project_properties); 117 } 118 119 125 130 private String 131 getJDK() 132 { 133 return System.getProperty("java.home") + "/bin/java"; 134 } 135 136 141 private String 142 getBootClasspath() 143 { 144 String cp = null; 145 146 if (project_properties_.getProperty("CORBA.launcher.bootclasspath") 147 != null) 148 cp = project_properties_.getProperty("CORBA.launcher.bootclasspath"); 149 if ( cp == null) 150 return ""; 151 else 152 return "-Xbootclasspath/p:" 153 + cp ; 154 } 155 156 161 private String 162 getProperties() 163 { 164 String props = ""; 165 166 for (Enumeration e = properties_.keys() ; e.hasMoreElements() ;) 167 { 168 String key = (String ) e.nextElement(); 169 props += arg_delimiter_+ "-D" + key + "=" + properties_.getProperty(key) + arg_delimiter_ + " "; 170 } 171 172 180 for ( 181 Enumeration openccm_envi = project_properties_.propertyNames(); 182 openccm_envi.hasMoreElements(); 183 ) 184 { 185 String key = (String ) openccm_envi.nextElement(); 186 if (key.startsWith("OpenCCM_") || key.equals("ORB_HOMEDIR")) 187 props += arg_delimiter_+ "-D" + key + "=" + project_properties_.getProperty(key) + arg_delimiter_ + " "; 188 } 189 190 191 return props; 192 } 193 194 199 private String 200 getClasspath() 201 { 202 String classpath = "-classpath " + arg_delimiter_; 203 String separator = System.getProperty("path.separator"); 204 205 if (project_properties_.getProperty("CORBA.launcher.classpath") 207 !=null) 208 classpath += project_properties_.getProperty("CORBA.launcher.classpath")+ separator; 209 210 for (Iterator it = pathes_.iterator(); it.hasNext();) 212 { 213 classpath += it.next() + separator; 214 } 215 216 return classpath.substring(0, classpath.length()-1) + arg_delimiter_; 218 } 219 220 225 private String 226 getArguments() 227 { 228 String args = ""; 229 230 for (Iterator it = arguments_.iterator(); it.hasNext();) 231 { 232 args += arg_delimiter_ + it.next() + arg_delimiter_ + " "; 233 } 234 235 return args; 236 } 237 238 243 private String 244 getRunId() 245 { 246 if (run_id_ != null) 247 return "--runid " + run_id_; 248 else 249 return ""; 250 } 251 252 257 private String 258 getXmlFile() 259 { 260 return xml_file_; 261 } 262 263 269 274 public void 275 setProjectProperties(Map project_properties) 276 { 277 project_properties_.putAll(project_properties); 278 } 279 280 285 public void 286 setWorkingDirectory(File dir) 287 { 288 working_directory_ = dir; 289 } 290 291 297 public void 298 addProperty(String name, String value) 299 { 300 properties_.setProperty(name, value); 301 } 302 303 308 public void 309 addPath(String path) 310 { 311 pathes_.add(path); 312 } 313 314 319 public void 320 addArgument(String arg) 321 { 322 arguments_.add(arg); 323 } 324 325 330 public void 331 setXmlFile(String file) 332 { 333 this.xml_file_ = file; 334 } 335 336 341 public void 342 setRunId(String run_id) 343 { 344 this.run_id_ = run_id; 345 } 346 347 353 public String 354 getCommandLine() 355 { 356 String result = null; 357 358 result = getJDK() + " "; 359 result += getBootClasspath() + " "; 360 result += getProperties() + " "; 361 result += getClasspath() + " "; 362 result += "org.objectweb.util.launcher.Launcher "; 363 result += arg_delimiter_ + "file:" + getXmlFile() + arg_delimiter_ + " "; 364 result += getRunId() + " "; 365 result += getArguments(); 366 367 return result; 368 } 369 370 374 abstract public void 375 configure(); 376 377 381 public void 382 post_exec() {} 383 384 390 public void 391 run() 392 throws BuildException 393 { 394 run(false); 395 } 396 397 405 public void 406 run(boolean standalone) 407 throws BuildException 408 { 409 Redirector redirector = null; 410 411 configure(); 412 try 413 { 414 process_ = Runtime.getRuntime().exec( getCommandLine(), null, working_directory_ ); 416 417 redirector = new Redirector(process_); 418 redirector.redirectOutput(); 419 420 post_exec(); 421 }catch(Exception e){ 422 e.printStackTrace(); 423 } 424 425 if (!standalone) 426 { 427 try 428 { 429 process_.waitFor(); 430 }catch (InterruptedException e) { 431 e.printStackTrace(); 432 } 433 if (process_.exitValue() == -1) 434 { 435 throw new BuildException("OpenCCM build failed!"); 436 } 437 } 438 } 439 440 443 public void 444 stop() 445 { 446 if (process_ != null) 447 { 448 process_.destroy(); 449 } 450 } 451 } 452 | Popular Tags |