Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 242   Methods: 9
NCLOC: 136   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ExecuteJava.java 90% 93.8% 88.9% 92.6%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-2003 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   
 
 56   
 package org.apache.tools.ant.taskdefs;
 57   
 
 58   
 import java.io.PrintStream;
 59   
 import java.lang.reflect.InvocationTargetException;
 60   
 import java.lang.reflect.Method;
 61   
 import org.apache.tools.ant.AntClassLoader;
 62   
 import org.apache.tools.ant.BuildException;
 63   
 import org.apache.tools.ant.Project;
 64   
 import org.apache.tools.ant.Task;
 65   
 import org.apache.tools.ant.types.Commandline;
 66   
 import org.apache.tools.ant.types.CommandlineJava;
 67   
 import org.apache.tools.ant.types.Path;
 68   
 import org.apache.tools.ant.util.TimeoutObserver;
 69   
 import org.apache.tools.ant.util.Watchdog;
 70   
 
 71   
 /**
 72   
  *
 73   
  * @author thomas.haas@softwired-inc.com
 74   
  * @author Stefan Bodewig
 75   
  * @since Ant 1.2
 76   
  */
 77   
 public class ExecuteJava implements Runnable, TimeoutObserver {
 78   
 
 79   
     private Commandline javaCommand = null;
 80   
     private Path classpath = null;
 81   
     private CommandlineJava.SysProperties sysProperties = null;
 82   
     private Method main = null;
 83   
     private Long timeout = null;
 84   
     private Throwable caught = null;
 85   
     private boolean timedOut = false;
 86   
     private Thread thread = null;
 87   
 
 88  7
     public void setJavaCommand(Commandline javaCommand) {
 89  7
         this.javaCommand = javaCommand;
 90   
     }
 91   
 
 92   
     /**
 93   
      * Set the classpath to be used when running the Java class
 94   
      *
 95   
      * @param p an Ant Path object containing the classpath.
 96   
      */
 97  7
     public void setClasspath(Path p) {
 98  7
         classpath = p;
 99   
     }
 100   
 
 101  5
     public void setSystemProperties(CommandlineJava.SysProperties s) {
 102  5
         sysProperties = s;
 103   
     }
 104   
 
 105   
     /**
 106   
      * All output (System.out as well as System.err) will be written
 107   
      * to this Stream.
 108   
      *
 109   
      * @deprecated manage output at the task level
 110   
      */
 111  0
     public void setOutput(PrintStream out) {
 112   
     }
 113   
 
 114   
     /**
 115   
      * @since 1.19, Ant 1.5
 116   
      */
 117  7
     public void setTimeout(Long timeout) {
 118  7
         this.timeout = timeout;
 119   
     }
 120   
 
 121  7
     public void execute(Project project) throws BuildException{
 122  7
         final String classname = javaCommand.getExecutable();
 123   
 
 124  7
         AntClassLoader loader = null;
 125  7
         try {
 126  7
             if (sysProperties != null) {
 127  5
                 sysProperties.setSystem();
 128   
             }
 129   
 
 130  7
             final Class[] param = { Class.forName("[Ljava.lang.String;") };
 131  7
             Class target = null;
 132  7
             if (classpath == null) {
 133  1
                 target = Class.forName(classname);
 134   
             } else {
 135  6
                 loader = project.createClassLoader(classpath);
 136  6
                 loader.setParent(project.getCoreLoader());
 137  6
                 loader.setParentFirst(false);
 138  6
                 loader.addJavaLibraries();
 139  6
                 loader.setIsolated(true);
 140  6
                 loader.setThreadContextLoader();
 141  6
                 target = loader.forceLoadClass(classname);
 142  6
                 AntClassLoader.initializeClass(target);
 143   
             }
 144  7
             main = target.getMethod("main", param);
 145  7
             if (main == null) {
 146  0
                 throw new BuildException("Could not find main() method in "
 147   
                                          + classname);
 148   
             }
 149   
 
 150  7
             if (timeout == null) {
 151  5
                 run();
 152   
             } else {
 153  2
                 thread = new Thread(this, "ExecuteJava");
 154  2
                 Task currentThreadTask
 155   
                     = project.getThreadTask(Thread.currentThread());
 156  2
                 project.registerThreadTask(thread, currentThreadTask);
 157   
                 // if we run into a timout, the run-away thread shall not
 158   
                 // make the VM run forever - if no timeout occurs, Ant's
 159   
                 // main thread will still be there to let the new thread
 160   
                 // finish
 161  2
                 thread.setDaemon(true);
 162  2
                 Watchdog w = new Watchdog(timeout.longValue());
 163  2
                 w.addTimeoutObserver(this);
 164  2
                 synchronized (this) {
 165  2
                     thread.start();
 166  2
                     w.start();
 167  2
                     try {
 168  2
                         wait();
 169   
                     } catch (InterruptedException e) {}
 170  2
                     if (timedOut) {
 171  1
                         project.log("Timeout: sub-process interrupted",
 172   
                                     Project.MSG_WARN);
 173   
                     } else {
 174  1
                         thread = null;
 175  1
                         w.stop();
 176   
                     }
 177   
                 }
 178   
             }
 179   
 
 180  7
             if (caught != null) {
 181  2
                 throw caught;
 182   
             }
 183   
 
 184   
         } catch (ClassNotFoundException e) {
 185  0
             throw new BuildException("Could not find " + classname + "."
 186   
                                      + " Make sure you have it in your"
 187   
                                      + " classpath");
 188   
         } catch (SecurityException e) {
 189  0
             throw e;
 190   
         } catch (Throwable e) {
 191  2
             throw new BuildException(e);
 192   
         } finally {
 193  7
             if (loader != null) {
 194  6
                 loader.resetThreadContextLoader();
 195  6
                 loader.cleanup();
 196   
             }
 197  7
             if (sysProperties != null) {
 198  5
                 sysProperties.restoreSystem();
 199   
             }
 200   
         }
 201   
     }
 202   
 
 203   
     /**
 204   
      * @since 1.19, Ant 1.5
 205   
      */
 206  7
     public void run() {
 207  7
         final Object[] argument = { javaCommand.getArguments() };
 208  7
         try {
 209  7
             main.invoke(null, argument);
 210   
         } catch (InvocationTargetException e) {
 211  3
             Throwable t = e.getTargetException();
 212  3
             if (!(t instanceof InterruptedException)) {
 213  2
                 caught = t;
 214   
             } /* else { swallow, probably due to timeout } */
 215   
         } catch (Throwable t) {
 216  0
             caught = t;
 217   
         } finally {
 218  7
             synchronized (this) {
 219  7
                 notifyAll();
 220   
             }
 221   
         }
 222   
     }
 223   
 
 224   
     /**
 225   
      * @since 1.19, Ant 1.5
 226   
      */
 227  1
     public synchronized void timeoutOccured(Watchdog w) {
 228  1
         if (thread != null) {
 229  1
             timedOut = true;
 230  1
             thread.interrupt();
 231   
         }
 232  1
         notifyAll();
 233   
     }
 234   
 
 235   
     /**
 236   
      * @since 1.19, Ant 1.5
 237   
      */
 238  2
     public boolean killedProcess() {
 239  2
         return timedOut;
 240   
     }
 241   
 }
 242