Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 519   Methods: 35
NCLOC: 242   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
VAJBuildInfo.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.taskdefs.optional.ide;
 56   
 
 57   
 
 58   
 import java.beans.PropertyChangeListener;
 59   
 import java.beans.PropertyChangeSupport;
 60   
 import java.io.File;
 61   
 import java.util.Enumeration;
 62   
 import java.util.StringTokenizer;
 63   
 import java.util.Vector;
 64   
 import org.apache.tools.ant.BuildEvent;
 65   
 import org.apache.tools.ant.BuildException;
 66   
 import org.apache.tools.ant.BuildListener;
 67   
 import org.apache.tools.ant.Project;
 68   
 import org.apache.tools.ant.ProjectHelper;
 69   
 import org.apache.tools.ant.Target;
 70   
 
 71   
 /**
 72   
  * This class wraps the Ant project information needed to
 73   
  * start Ant from Visual Age.
 74   
  * It serves the following purposes:
 75   
  * - acts as model for AntMakeFrame
 76   
  * - converts itself to/from String (to store the information
 77   
  *   as ToolData in the VA repository)
 78   
  * - wraps Project functions for the GUI (get target list,
 79   
  *   execute target)
 80   
  * - manages a seperate thread for Ant project execution
 81   
  *   this allows interrupting a running build from a GUI
 82   
  *
 83   
  * @author Wolf Siberski, TUI Infotec GmbH
 84   
  */
 85   
 
 86   
 class VAJBuildInfo implements Runnable {
 87   
     /**
 88   
      * This exception is thrown when a build is interrupted
 89   
      */
 90   
     public static class BuildInterruptedException extends BuildException {
 91  0
         public String toString() {
 92  0
             return "BUILD INTERRUPTED";
 93   
         }
 94   
     }
 95   
 
 96   
     /**
 97   
      * BuildListener which checks for interruption and throws Exception
 98   
      * if build process is interrupted. This class is a wrapper around
 99   
      * a 'real' listener.
 100   
      */
 101   
     private class InterruptedChecker implements BuildListener {
 102   
         // the real listener
 103   
         BuildListener wrappedListener;
 104   
 
 105   
         /**
 106   
          * Can only be constructed as wrapper around a real listener
 107   
          * @param listener the real listener
 108   
          */
 109  0
         public InterruptedChecker(BuildListener listener) {
 110  0
             super();
 111  0
             wrappedListener = listener;
 112   
         }
 113   
 
 114   
         /**
 115   
          * checks if the thread was interrupted. When an
 116   
          * interrupt occured, throw an Exception to stop
 117   
          * the execution.
 118   
          */
 119  0
         protected void checkInterrupted() {
 120  0
             if (buildThread.isInterrupted()) {
 121  0
                 throw new BuildInterruptedException();
 122   
             }
 123   
         }
 124   
 
 125   
         /**
 126   
          *  Fired after the last target has finished. This event
 127   
          *  will still be thrown if an error occured during the build.
 128   
          */
 129  0
         public void buildFinished(BuildEvent event) {
 130  0
             wrappedListener.buildFinished(event);
 131  0
             checkInterrupted();
 132   
         }
 133   
 
 134   
         /**
 135   
          *  Fired before any targets are started.
 136   
          */
 137  0
         public void buildStarted(BuildEvent event) {
 138  0
             wrappedListener.buildStarted(event);
 139  0
             checkInterrupted();
 140   
         }
 141   
 
 142   
         /**
 143   
          *  Fired whenever a message is logged.
 144   
          */
 145  0
         public void messageLogged(BuildEvent event) {
 146  0
             wrappedListener.messageLogged(event);
 147  0
             checkInterrupted();
 148   
         }
 149   
 
 150   
         /**
 151   
          *  Fired when a target has finished. This event will
 152   
          *  still be thrown if an error occured during the build.
 153   
          */
 154  0
         public void targetFinished(BuildEvent event) {
 155  0
             wrappedListener.targetFinished(event);
 156  0
             checkInterrupted();
 157   
         }
 158   
 
 159   
         /**
 160   
          *  Fired when a target is started.
 161   
          */
 162  0
         public void targetStarted(BuildEvent event) {
 163  0
             wrappedListener.targetStarted(event);
 164  0
             checkInterrupted();
 165   
         }
 166   
 
 167   
         /**
 168   
          *  Fired when a task has finished. This event will still
 169   
          *  be throw if an error occured during the build.
 170   
          */
 171  0
         public void taskFinished(BuildEvent event) {
 172  0
             wrappedListener.taskFinished(event);
 173  0
             checkInterrupted();
 174   
         }
 175   
 
 176   
         /**
 177   
          *  Fired when a task is started.
 178   
          */
 179  0
         public void taskStarted(BuildEvent event) {
 180  0
             wrappedListener.taskStarted(event);
 181  0
             checkInterrupted();
 182   
         }
 183   
     }
 184   
 
 185   
     // name of the VA project this BuildInfo belongs to
 186   
     private String vajProjectName = "";
 187   
 
 188   
     // name of the Ant build file
 189   
     private String buildFileName = "";
 190   
 
 191   
     // main targets found in the build file
 192   
     private Vector projectTargets = new Vector();
 193   
 
 194   
     // target selected for execution
 195   
     private java.lang.String target = "";
 196   
 
 197   
     // log level
 198   
     private int outputMessageLevel = Project.MSG_INFO;
 199   
 
 200   
     // Ant Project created from build file
 201   
     private transient Project project;
 202   
 
 203   
     // is true if Project initialization was successful
 204   
     private transient boolean projectInitialized = false;
 205   
 
 206   
     // Support for bound properties
 207   
     protected transient PropertyChangeSupport propertyChange;
 208   
 
 209   
     // thread for Ant build execution
 210   
     private Thread buildThread;
 211   
 
 212   
     // the listener used to log output.
 213   
     private BuildListener projectLogger;
 214   
 
 215   
 
 216   
     /**
 217   
      * The addPropertyChangeListener method was generated to support the
 218   
      * propertyChange field.
 219   
      */
 220  0
     public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
 221  0
         getPropertyChange().addPropertyChangeListener(listener);
 222   
     }
 223   
 
 224   
     /**
 225   
      * Returns the BuildInfo information as String. The BuildInfo can
 226   
      * be rebuilt from that String by calling parse().
 227   
      * @return java.lang.String
 228   
      */
 229  0
     public String asDataString() {
 230  0
         String result = getOutputMessageLevel() + "|" + getBuildFileName()
 231   
             + "|" + getTarget();
 232  0
         for (Enumeration e = getProjectTargets().elements();
 233  0
               e.hasMoreElements();) {
 234  0
             result = result + "|" + e.nextElement();
 235   
         }
 236   
 
 237  0
         return result;
 238   
     }
 239   
 
 240   
     /**
 241   
      * Search for the insert position to keep names a sorted list of Strings
 242   
      * This method has been copied from org.apache.tools.ant.Main
 243   
      */
 244  0
     private static int findTargetPosition(Vector names, String name) {
 245  0
         int res = names.size();
 246  0
         for (int i = 0; i < names.size() && res == names.size(); i++) {
 247  0
             if (name.compareTo((String) names.elementAt(i)) < 0) {
 248  0
                 res = i;
 249   
             }
 250   
         }
 251  0
         return res;
 252   
     }
 253   
 
 254   
     /**
 255   
      * The firePropertyChange method was generated to support the propertyChange field.
 256   
      */
 257  0
     public void firePropertyChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue) {
 258  0
         getPropertyChange().firePropertyChange(propertyName, oldValue, newValue);
 259   
     }
 260   
 
 261   
     /**
 262   
      * Returns the build file name.
 263   
      * @return build file name.
 264   
      */
 265  0
     public String getBuildFileName() {
 266  0
         return buildFileName;
 267   
     }
 268   
 
 269   
     /**
 270   
      * Returns the log level
 271   
      * @return log level.
 272   
      */
 273  0
     public int getOutputMessageLevel() {
 274  0
         return outputMessageLevel;
 275   
     }
 276   
 
 277   
     /**
 278   
      * Returns the Ant project
 279   
      * @return org.apache.tools.ant.Project
 280   
      */
 281  0
     private Project getProject() {
 282  0
         if (project == null) {
 283  0
             project = new Project();
 284   
         }
 285  0
         return project;
 286   
     }
 287   
 
 288   
     /**
 289   
      * return a list of all targets in the current buildfile
 290   
      */
 291  0
     public Vector getProjectTargets() {
 292  0
         return projectTargets;
 293   
     }
 294   
 
 295   
     /**
 296   
      * Accessor for the propertyChange field.
 297   
      */
 298  0
     protected PropertyChangeSupport getPropertyChange() {
 299  0
         if (propertyChange == null) {
 300  0
             propertyChange = new PropertyChangeSupport(this);
 301   
         }
 302  0
         return propertyChange;
 303   
     }
 304   
 
 305   
     /**
 306   
      * returns the selected target.
 307   
      */
 308  0
     public java.lang.String getTarget() {
 309  0
         return target;
 310   
     }
 311   
 
 312   
     /**
 313   
      * returns the VA project name
 314   
      */
 315  0
     public String getVAJProjectName() {
 316  0
         return vajProjectName;
 317   
     }
 318   
 
 319   
     /**
 320   
      * Initializes the Ant project. Assumes that the
 321   
      * project attribute is already set.
 322   
      */
 323  0
     private void initProject() {
 324  0
         try {
 325  0
             project.init();
 326  0
             File buildFile = new File(getBuildFileName());
 327  0
             project.setUserProperty("ant.file", buildFile.getAbsolutePath());
 328  0
             ProjectHelper.configureProject(project, buildFile);
 329  0
             setProjectInitialized(true);
 330   
         } catch (RuntimeException exc) {
 331  0
             setProjectInitialized(false);
 332  0
             throw exc;
 333   
         } catch (Error err) {
 334  0
             setProjectInitialized(false);
 335  0
             throw err;
 336   
         }
 337   
     }
 338   
 
 339   
     /**
 340   
      * Returns true, if the Ant project is initialized.
 341   
      * (i.e., if the buildfile loaded).
 342   
      */
 343  0
     public boolean isProjectInitialized() {
 344  0
         return projectInitialized;
 345   
     }
 346   
 
 347   
     /**
 348   
      * Creates a BuildInfo object from a String
 349   
      * The String must be in the format
 350   
      * outputMessageLevel'|'buildFileName'|'defaultTarget'|'(project target'|')*
 351   
      *
 352   
      * @return org.apache.tools.ant.taskdefs.optional.vaj.BuildInfo
 353   
      * @param data java.lang.String
 354   
      */
 355  0
     public static VAJBuildInfo parse(String data) {
 356  0
         VAJBuildInfo result = new VAJBuildInfo();
 357   
 
 358  0
         try {
 359  0
             StringTokenizer tok = new StringTokenizer(data, "|");
 360  0
             result.setOutputMessageLevel(tok.nextToken());
 361  0
             result.setBuildFileName(tok.nextToken());
 362  0
             result.setTarget(tok.nextToken());
 363  0
             while (tok.hasMoreTokens()) {
 364  0
                 result.projectTargets.addElement(tok.nextToken());
 365   
             }
 366   
         } catch (Throwable t) {
 367   
             // if parsing the info fails, just return
 368   
             // an empty VAJBuildInfo
 369   
         }
 370  0
         return result;
 371   
     }
 372   
 
 373   
     /**
 374   
      * The removePropertyChangeListener method was generated
 375   
      * to support the propertyChange field.
 376   
      */
 377  0
     public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
 378  0
         getPropertyChange().removePropertyChangeListener(listener);
 379   
     }
 380   
 
 381   
     /**
 382   
      * Sets the build file name
 383   
      * @param buildFileName build file name
 384   
      */
 385  0
     public void setBuildFileName(String newBuildFileName) {
 386  0
         String oldValue = buildFileName;
 387  0
         buildFileName = newBuildFileName;
 388  0
         setProjectInitialized(false);
 389  0
         firePropertyChange("buildFileName", oldValue, buildFileName);
 390   
     }
 391   
 
 392   
     /**
 393   
      * Sets the log level (value must be one of the constants in Project)
 394   
      * @param outputMessageLevel log level.
 395   
      */
 396  0
     public void setOutputMessageLevel(int newOutputMessageLevel) {
 397  0
         int oldValue = outputMessageLevel;
 398  0
         outputMessageLevel = newOutputMessageLevel;
 399  0
         firePropertyChange("outputMessageLevel",
 400   
                            new Integer(oldValue), new Integer(outputMessageLevel));
 401   
     }
 402   
 
 403   
     /**
 404   
      * Sets the log level (value must be one of the constants in Project)
 405   
      * @param outputMessageLevel log level as String.
 406   
      */
 407  0
     private void setOutputMessageLevel(String outputMessageLevel) {
 408  0
         int level = Integer.parseInt(outputMessageLevel);
 409  0
         setOutputMessageLevel(level);
 410   
     }
 411   
 
 412   
     /**
 413   
      * sets the initialized flag
 414   
      */
 415  0
     private void setProjectInitialized(boolean initialized) {
 416  0
         Boolean oldValue = new Boolean(projectInitialized);
 417  0
         projectInitialized = initialized;
 418  0
         firePropertyChange("projectInitialized", oldValue, new Boolean(projectInitialized));
 419   
     }
 420   
 
 421   
     /**
 422   
      * Sets the target to execute when executeBuild is called
 423   
      * @param newTarget build target
 424   
      */
 425  0
     public void setTarget(String newTarget) {
 426  0
         String oldValue = target;
 427  0
         target = newTarget;
 428  0
         firePropertyChange("target", oldValue, target);
 429   
     }
 430   
 
 431   
     /**
 432   
      * Sets the name of the Visual Age for Java project where
 433   
      * this BuildInfo belongs to
 434   
      * @param newProjectName VAJ project
 435   
      */
 436  0
     public void setVAJProjectName(String newVAJProjectName) {
 437  0
         String oldValue = vajProjectName;
 438  0
         vajProjectName = newVAJProjectName;
 439  0
         firePropertyChange("VAJProjectName", oldValue, vajProjectName);
 440   
     }
 441   
 
 442   
     /**
 443   
      * reloads the build file and updates the target list
 444   
      */
 445  0
     public void updateTargetList() {
 446  0
         project = new Project();
 447  0
         initProject();
 448  0
         projectTargets.removeAllElements();
 449  0
         Enumeration ptargets = project.getTargets().elements();
 450  0
         while (ptargets.hasMoreElements()) {
 451  0
             Target currentTarget = (Target) ptargets.nextElement();
 452  0
             if (currentTarget.getDescription() != null) {
 453  0
                 String targetName = currentTarget.getName();
 454  0
                 int pos = findTargetPosition(projectTargets, targetName);
 455  0
                 projectTargets.insertElementAt(targetName, pos);
 456   
             }
 457   
         }
 458   
     }
 459   
 
 460   
 
 461   
     /**
 462   
      * cancels a build.
 463   
      */
 464  0
     public void cancelBuild() {
 465  0
         buildThread.interrupt();
 466   
     }
 467   
 
 468   
     /**
 469   
      * Executes the target set by setTarget().
 470   
      * @param listener  BuildListener for the output of the build
 471   
      */
 472  0
     public void executeProject(BuildListener logger) {
 473  0
         Throwable error;
 474  0
         projectLogger = logger;
 475  0
         try {
 476  0
             buildThread = new Thread(this);
 477  0
             buildThread.setPriority(Thread.MIN_PRIORITY);
 478  0
             buildThread.start();
 479   
         } catch (RuntimeException exc) {
 480  0
             error = exc;
 481  0
             throw exc;
 482   
         } catch (Error err) {
 483  0
             error = err;
 484  0
             throw err;
 485   
         }
 486   
     }
 487   
 
 488   
     /**
 489   
      * Executes a build. This method is executed by
 490   
      * the Ant execution thread
 491   
      */
 492  0
     public void run() {
 493  0
         try {
 494  0
             InterruptedChecker ic = new InterruptedChecker(projectLogger);
 495  0
             BuildEvent e = new BuildEvent(getProject());
 496  0
             try {
 497  0
                 ic.buildStarted(e);
 498   
 
 499  0
                 if (!isProjectInitialized()) {
 500  0
                     initProject();
 501   
                 }
 502   
 
 503  0
                 project.addBuildListener(ic);
 504  0
                 project.executeTarget(target);
 505   
 
 506  0
                 ic.buildFinished(e);
 507   
             } catch (Throwable t) {
 508  0
                 e.setException(t);
 509  0
                 ic.buildFinished(e);
 510   
             } finally {
 511  0
                 project.removeBuildListener(ic);
 512   
             }
 513   
         } catch (Throwable t2) {
 514  0
             System.out.println("unexpected exception!");
 515  0
             t2.printStackTrace();
 516   
         }
 517   
     }
 518   
 }
 519