1 22 23 package com.izforge.izpack.event; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileNotFoundException ; 28 import java.io.FileOutputStream ; 29 import java.io.PrintStream ; 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Properties ; 34 35 import org.apache.tools.ant.BuildLogger; 36 import org.apache.tools.ant.DefaultLogger; 37 import org.apache.tools.ant.DemuxOutputStream; 38 import org.apache.tools.ant.Project; 39 import org.apache.tools.ant.Target; 40 import org.apache.tools.ant.input.DefaultInputHandler; 41 import org.apache.tools.ant.taskdefs.Ant; 42 import org.apache.tools.ant.util.JavaEnvUtils; 43 44 51 public class AntAction extends ActionBase 52 { 53 54 57 private static final long serialVersionUID = 3258131345250005557L; 58 59 public static final String ANTACTIONS = "antactions"; 60 61 public static final String ANTACTION = "antaction"; 62 63 public static final String ANTCALL = "antcall"; 64 65 private boolean quiet = false; 66 67 private boolean verbose = false; 68 69 private Properties properties = null; 70 71 private List targets = null; 72 73 private List uninstallTargets = null; 74 75 private String logFile = null; 76 77 private String buildFile = null; 78 79 private List propertyFiles = null; 80 81 84 public AntAction() 85 { 86 super(); 87 properties = new Properties (); 88 targets = new ArrayList (); 89 uninstallTargets = new ArrayList (); 90 propertyFiles = new ArrayList (); 91 } 92 93 100 public void performInstallAction() throws Exception 101 { 102 performAction(false); 103 } 104 105 112 public void performUninstallAction() throws Exception 113 { 114 performAction(true); 115 } 116 117 128 public void performAction(boolean uninstall) throws Exception 129 { 130 if (verbose) System.out.println("Calling ANT with buildfile: " + buildFile); 131 SecurityManager oldsm = null; 132 if (!JavaEnvUtils.isJavaVersion("1.0") && !JavaEnvUtils.isJavaVersion("1.1")) 133 oldsm = System.getSecurityManager(); 134 PrintStream err = System.err; 135 PrintStream out = System.out; 136 try 137 { 138 Project antProj = new Project(); 139 antProj.setName("antcallproject"); 140 antProj.addBuildListener(createLogger()); 141 antProj.setInputHandler(new DefaultInputHandler()); 142 antProj.setSystemProperties(); 143 addProperties(antProj, getProperties()); 144 addPropertiesFromPropertyFiles(antProj); 145 antProj.fireBuildStarted(); 147 antProj.init(); 148 List antcalls = new ArrayList (); 149 List choosenTargets = (uninstall) ? uninstallTargets : targets; 150 if (choosenTargets.size() > 0) 151 { 152 Ant antcall = null; 153 for (int i = 0; i < choosenTargets.size(); i++) 154 { 155 antcall = (Ant) antProj.createTask("ant"); 156 antcall.setAntfile(getBuildFile()); 157 antcall.setTarget((String ) choosenTargets.get(i)); 158 antcalls.add(antcall); 159 } 160 } 161 Target target = new Target(); 162 target.setName("calltarget"); 163 164 for (int i = 0; i < antcalls.size(); i++) 165 { 166 target.addTask((Ant) antcalls.get(i)); 167 } 168 antProj.addTarget(target); 169 System.setOut(new PrintStream (new DemuxOutputStream(antProj, false))); 170 System.setErr(new PrintStream (new DemuxOutputStream(antProj, true))); 171 antProj.executeTarget("calltarget"); 172 } 173 finally 174 { 175 if (oldsm != null) System.setSecurityManager(oldsm); 176 System.setOut(out); 177 System.setErr(err); 178 } 179 } 180 181 186 public String getBuildFile() 187 { 188 return buildFile; 189 } 190 191 196 public void setBuildFile(String buildFile) 197 { 198 this.buildFile = buildFile; 199 } 200 201 206 public String getLogFile() 207 { 208 return logFile; 209 } 210 211 216 public void setLogFile(String logFile) 217 { 218 this.logFile = logFile; 219 } 220 221 226 public List getPropertyFiles() 227 { 228 return propertyFiles; 229 } 230 231 236 public void addPropertyFile(String propertyFile) 237 { 238 this.propertyFiles.add(propertyFile); 239 } 240 241 246 public void setPropertyFiles(List propertyFiles) 247 { 248 this.propertyFiles = propertyFiles; 249 } 250 251 256 public Properties getProperties() 257 { 258 return properties; 259 } 260 261 266 public void setProperties(Properties properties) 267 { 268 this.properties = properties; 269 } 270 271 277 public void setProperty(String name, String value) 278 { 279 this.properties.put(name, value); 280 } 281 282 288 public String getProperty(String name) 289 { 290 return this.properties.getProperty(name); 291 } 292 293 298 public boolean isQuiet() 299 { 300 return quiet; 301 } 302 303 308 public void setQuiet(boolean quiet) 309 { 310 this.quiet = quiet; 311 } 312 313 318 public List getTargets() 319 { 320 return targets; 321 } 322 323 328 public void setTargets(ArrayList targets) 329 { 330 this.targets = targets; 331 } 332 333 338 public void addTarget(String target) 339 { 340 this.targets.add(target); 341 } 342 343 348 public List getUninstallTargets() 349 { 350 return uninstallTargets; 351 } 352 353 358 public void setUninstallTargets(ArrayList targets) 359 { 360 this.uninstallTargets = targets; 361 } 362 363 368 public void addUninstallTarget(String target) 369 { 370 this.uninstallTargets.add(target); 371 } 372 373 378 public boolean isVerbose() 379 { 380 return verbose; 381 } 382 383 388 public void setVerbose(boolean verbose) 389 { 390 this.verbose = verbose; 391 } 392 393 private BuildLogger createLogger() 394 { 395 int msgOutputLevel = 2; 396 if (verbose) 397 msgOutputLevel = 4; 398 else if (quiet) msgOutputLevel = 1; 399 BuildLogger logger = new DefaultLogger(); 400 logger.setMessageOutputLevel(msgOutputLevel); 401 if (logFile != null) 402 { 403 PrintStream printStream; 404 try 405 { 406 printStream = new PrintStream (new FileOutputStream (logFile)); 407 logger.setOutputPrintStream(printStream); 408 logger.setErrorPrintStream(printStream); 409 } 410 catch (FileNotFoundException e) 411 { 412 logger.setOutputPrintStream(System.out); 413 logger.setErrorPrintStream(System.err); 414 } 415 } 416 else 417 { 418 logger.setOutputPrintStream(System.out); 419 logger.setErrorPrintStream(System.err); 420 } 421 return logger; 422 } 423 424 private void addProperties(Project proj, Properties props) 425 { 426 if (proj == null) return; 427 if (props.size() > 0) 428 { 429 Iterator iter = props.keySet().iterator(); 430 String key = null; 431 while (iter.hasNext()) 432 { 433 key = (String ) iter.next(); 434 proj.setProperty(key, props.getProperty(key)); 435 } 436 } 437 } 438 439 private void addPropertiesFromPropertyFiles(Project proj) throws Exception 440 { 441 if (proj == null) return; 442 Properties props = new Properties (); 443 File pf = null; 444 FileInputStream fis = null; 445 try 446 { 447 for (int i = 0; i < propertyFiles.size(); i++) 448 { 449 pf = new File ((String ) propertyFiles.get(i)); 450 if (pf.exists()) 451 { 452 fis = new FileInputStream (pf); 453 props.load(fis); 454 fis.close(); 455 } 456 else 457 { 458 throw new Exception ("Required propertyfile " + pf 459 + " for antcall doesn't exist."); 460 } 461 } 462 } 463 finally 464 { 465 if (fis != null) fis.close(); 466 } 467 addProperties(proj, props); 468 } 469 470 } 471 | Popular Tags |