1 17 18 package org.apache.tools.ant.taskdefs.optional.ide; 19 20 21 import java.beans.PropertyChangeListener ; 22 import java.beans.PropertyChangeSupport ; 23 import java.io.File ; 24 import java.util.Enumeration ; 25 import java.util.StringTokenizer ; 26 import java.util.Vector ; 27 import org.apache.tools.ant.BuildEvent; 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.BuildListener; 30 import org.apache.tools.ant.Project; 31 import org.apache.tools.ant.ProjectHelper; 32 import org.apache.tools.ant.Target; 33 34 47 48 class VAJBuildInfo implements Runnable { 49 52 public static class BuildInterruptedException extends BuildException { 53 public String toString() { 54 return "BUILD INTERRUPTED"; 55 } 56 } 57 58 63 private class InterruptedChecker implements BuildListener { 64 BuildListener wrappedListener; 66 67 71 public InterruptedChecker(BuildListener listener) { 72 super(); 73 wrappedListener = listener; 74 } 75 76 81 protected void checkInterrupted() { 82 if (buildThread.isInterrupted()) { 83 throw new BuildInterruptedException(); 84 } 85 } 86 87 91 public void buildFinished(BuildEvent event) { 92 wrappedListener.buildFinished(event); 93 checkInterrupted(); 94 } 95 96 99 public void buildStarted(BuildEvent event) { 100 wrappedListener.buildStarted(event); 101 checkInterrupted(); 102 } 103 104 107 public void messageLogged(BuildEvent event) { 108 wrappedListener.messageLogged(event); 109 checkInterrupted(); 110 } 111 112 116 public void targetFinished(BuildEvent event) { 117 wrappedListener.targetFinished(event); 118 checkInterrupted(); 119 } 120 121 124 public void targetStarted(BuildEvent event) { 125 wrappedListener.targetStarted(event); 126 checkInterrupted(); 127 } 128 129 133 public void taskFinished(BuildEvent event) { 134 wrappedListener.taskFinished(event); 135 checkInterrupted(); 136 } 137 138 141 public void taskStarted(BuildEvent event) { 142 wrappedListener.taskStarted(event); 143 checkInterrupted(); 144 } 145 } 146 147 private String vajProjectName = ""; 149 150 private String buildFileName = ""; 152 153 private Vector projectTargets = new Vector (); 155 156 private String target = ""; 158 159 private int outputMessageLevel = Project.MSG_INFO; 161 162 private transient Project project; 164 165 private transient boolean projectInitialized = false; 167 168 protected transient PropertyChangeSupport propertyChange; 170 171 private Thread buildThread; 173 174 private BuildListener projectLogger; 176 177 178 182 public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { 183 getPropertyChange().addPropertyChangeListener(listener); 184 } 185 186 191 public String asDataString() { 192 String result = getOutputMessageLevel() + "|" + getBuildFileName() 193 + "|" + getTarget(); 194 for (Enumeration e = getProjectTargets().elements(); 195 e.hasMoreElements();) { 196 result = result + "|" + e.nextElement(); 197 } 198 199 return result; 200 } 201 202 206 private static int findTargetPosition(Vector names, String name) { 207 int res = names.size(); 208 for (int i = 0; i < names.size() && res == names.size(); i++) { 209 if (name.compareTo((String ) names.elementAt(i)) < 0) { 210 res = i; 211 } 212 } 213 return res; 214 } 215 216 219 public void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 220 getPropertyChange().firePropertyChange(propertyName, oldValue, newValue); 221 } 222 223 227 public String getBuildFileName() { 228 return buildFileName; 229 } 230 231 235 public int getOutputMessageLevel() { 236 return outputMessageLevel; 237 } 238 239 243 private Project getProject() { 244 if (project == null) { 245 project = new Project(); 246 } 247 return project; 248 } 249 250 253 public Vector getProjectTargets() { 254 return projectTargets; 255 } 256 257 260 protected PropertyChangeSupport getPropertyChange() { 261 if (propertyChange == null) { 262 propertyChange = new PropertyChangeSupport (this); 263 } 264 return propertyChange; 265 } 266 267 270 public String getTarget() { 271 return target; 272 } 273 274 277 public String getVAJProjectName() { 278 return vajProjectName; 279 } 280 281 285 private void initProject() { 286 try { 287 project.init(); 288 File buildFile = new File (getBuildFileName()); 289 project.setUserProperty("ant.file", buildFile.getAbsolutePath()); 290 ProjectHelper.configureProject(project, buildFile); 291 setProjectInitialized(true); 292 } catch (RuntimeException exc) { 293 setProjectInitialized(false); 294 throw exc; 295 } catch (Error err) { 296 setProjectInitialized(false); 297 throw err; 298 } 299 } 300 301 305 public boolean isProjectInitialized() { 306 return projectInitialized; 307 } 308 309 317 public static VAJBuildInfo parse(String data) { 318 VAJBuildInfo result = new VAJBuildInfo(); 319 320 try { 321 StringTokenizer tok = new StringTokenizer (data, "|"); 322 result.setOutputMessageLevel(tok.nextToken()); 323 result.setBuildFileName(tok.nextToken()); 324 result.setTarget(tok.nextToken()); 325 while (tok.hasMoreTokens()) { 326 result.projectTargets.addElement(tok.nextToken()); 327 } 328 } catch (Throwable t) { 329 } 332 return result; 333 } 334 335 339 public synchronized void removePropertyChangeListener(PropertyChangeListener listener) { 340 getPropertyChange().removePropertyChangeListener(listener); 341 } 342 343 347 public void setBuildFileName(String newBuildFileName) { 348 String oldValue = buildFileName; 349 buildFileName = newBuildFileName; 350 setProjectInitialized(false); 351 firePropertyChange("buildFileName", oldValue, buildFileName); 352 } 353 354 358 public void setOutputMessageLevel(int newOutputMessageLevel) { 359 int oldValue = outputMessageLevel; 360 outputMessageLevel = newOutputMessageLevel; 361 firePropertyChange("outputMessageLevel", 362 new Integer (oldValue), new Integer (outputMessageLevel)); 363 } 364 365 369 private void setOutputMessageLevel(String outputMessageLevel) { 370 int level = Integer.parseInt(outputMessageLevel); 371 setOutputMessageLevel(level); 372 } 373 374 377 private void setProjectInitialized(boolean initialized) { 378 Boolean oldValue = new Boolean (projectInitialized); 379 projectInitialized = initialized; 380 firePropertyChange("projectInitialized", oldValue, new Boolean (projectInitialized)); 381 } 382 383 387 public void setTarget(String newTarget) { 388 String oldValue = target; 389 target = newTarget; 390 firePropertyChange("target", oldValue, target); 391 } 392 393 398 public void setVAJProjectName(String newVAJProjectName) { 399 String oldValue = vajProjectName; 400 vajProjectName = newVAJProjectName; 401 firePropertyChange("VAJProjectName", oldValue, vajProjectName); 402 } 403 404 407 public void updateTargetList() { 408 project = new Project(); 409 initProject(); 410 projectTargets.removeAllElements(); 411 Enumeration ptargets = project.getTargets().elements(); 412 while (ptargets.hasMoreElements()) { 413 Target currentTarget = (Target) ptargets.nextElement(); 414 if (currentTarget.getDescription() != null) { 415 String targetName = currentTarget.getName(); 416 int pos = findTargetPosition(projectTargets, targetName); 417 projectTargets.insertElementAt(targetName, pos); 418 } 419 } 420 } 421 422 423 426 public void cancelBuild() { 427 buildThread.interrupt(); 428 } 429 430 434 public void executeProject(BuildListener logger) { 435 Throwable error; 436 projectLogger = logger; 437 try { 438 buildThread = new Thread (this); 439 buildThread.setPriority(Thread.MIN_PRIORITY); 440 buildThread.start(); 441 } catch (RuntimeException exc) { 442 error = exc; 443 throw exc; 444 } catch (Error err) { 445 error = err; 446 throw err; 447 } 448 } 449 450 454 public void run() { 455 try { 456 InterruptedChecker ic = new InterruptedChecker(projectLogger); 457 BuildEvent e = new BuildEvent(getProject()); 458 try { 459 ic.buildStarted(e); 460 461 if (!isProjectInitialized()) { 462 initProject(); 463 } 464 465 project.addBuildListener(ic); 466 project.executeTarget(target); 467 468 ic.buildFinished(e); 469 } catch (Throwable t) { 470 e.setException(t); 471 ic.buildFinished(e); 472 } finally { 473 project.removeBuildListener(ic); 474 } 475 } catch (Throwable t2) { 476 System.out.println("unexpected exception!"); 477 t2.printStackTrace(); 478 } 479 } 480 } 481 | Popular Tags |