Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 269   Methods: 10
NCLOC: 118   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Classloader.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-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;
 56   
 
 57   
 import org.apache.tools.ant.*;
 58   
 import org.apache.tools.ant.types.*;
 59   
 
 60   
 import java.io.*;
 61   
 import java.util.*;
 62   
 import org.xml.sax.Locator;
 63   
 import org.xml.sax.InputSource;
 64   
 //import org.xml.sax.HandlerBase;
 65   
 import org.xml.sax.SAXParseException;
 66   
 import org.xml.sax.XMLReader;
 67   
 import org.xml.sax.SAXException;
 68   
 import org.xml.sax.DocumentHandler;
 69   
 import org.xml.sax.Attributes;
 70   
 import org.xml.sax.AttributeList;
 71   
 import org.xml.sax.helpers.XMLReaderAdapter;
 72   
 import org.xml.sax.helpers.DefaultHandler;
 73   
 import org.xml.sax.helpers.AttributeListImpl;
 74   
 
 75   
 import javax.xml.parsers.SAXParserFactory;
 76   
 import javax.xml.parsers.SAXParser;
 77   
 import javax.xml.parsers.ParserConfigurationException;
 78   
 
 79   
 /**
 80   
  * EXPERIMENTAL
 81   
  * Create or modifies ClassLoader. The required pathRef parameter
 82   
  * will be used to add classpath elements.
 83   
  *
 84   
  * The classpath is a regular path. Currently only file components are
 85   
  * supported ( future extensions may allow URLs ).
 86   
  *
 87   
  * You can modify the core loader by not specifying any name or using
 88   
  * "ant.coreLoader". ( the core loader is used to load system ant
 89   
  * tasks and for taskdefs that don't specify an explicit path ).
 90   
  *
 91   
  * Taskdef and typedef can use the loader you create if the name follows
 92   
  * the "ant.loader.NAME" pattern. NAME will be used as a pathref when
 93   
  * calling taskdef.
 94   
  *
 95   
  * This tasks will not modify the core loader if "build.sysclasspath=only"
 96   
  *
 97   
  * The typical use is:
 98   
  * <pre>
 99   
  *  &lt;path id="ant.deps" &gt;
 100   
  *     &lt;fileset dir="myDir" &gt;
 101   
  *        &lt;include name="junit.jar, bsf.jar, js.jar, etc"/&gt;
 102   
  *     &lt;/fileset&gt;
 103   
  *  &lt;/path&gt;
 104   
  *
 105   
  *  &lt;classloader pathRef="ant.deps" /&gt;
 106   
  *
 107   
  * </pre>
 108   
  *
 109   
  * @author Costin Manolache
 110   
  */
 111   
 public class Classloader extends Task {
 112   
     public static final String SYSTEM_LOADER_REF="ant.coreLoader";
 113   
 
 114   
     private String name=null;
 115   
     private Path classpath;
 116   
     private boolean reset=false;
 117   
     private boolean parentFirst=true;
 118   
     private String parentName=null;
 119   
 
 120  0
     public Classloader() {
 121   
     }
 122   
 
 123   
     /** Name of the loader. If none, the default loader will be modified
 124   
      *
 125   
      * @param name
 126   
      */
 127  0
     public void setName(String name) {
 128  0
         this.name=name;
 129   
     }
 130   
 
 131   
     /** Reset the classloader, if it already exists. A new loader will
 132   
      * be created and all the references to the old one will be removed.
 133   
      * ( it is not possible to remove paths from a loader ). The new
 134   
      * path will be used.
 135   
      *
 136   
      * @param b
 137   
      */
 138  0
     public void setReset(boolean b) {
 139  0
         this.reset=b;
 140   
     }
 141   
 
 142  0
     public void setReverse(boolean b ) {
 143  0
         this.parentFirst= ! b;
 144   
     }
 145   
 
 146  0
     public void setParentFirst(boolean b ) {
 147  0
         this.parentFirst= b;
 148   
     }
 149   
 
 150   
     // TODO: add exceptions for delegation or reverse
 151   
 
 152   
     // TODO
 153  0
     public void setParentName( String name ) {
 154  0
         this.parentName=name;
 155   
     }
 156   
 
 157   
 
 158   
     /** Specify which path will be used. If the loader already exists
 159   
      *  and is an AntClassLoader ( or any other loader we can extend ),
 160   
      *  the path will be added to the loader.
 161   
      */
 162  0
     public void setClasspathRef( Reference pathRef ) throws BuildException {
 163  0
         classpath=(Path)pathRef.getReferencedObject(project);
 164   
     }
 165   
 
 166   
     /**
 167   
      * Set the classpath to be used when searching for component being defined
 168   
      *
 169   
      * @param classpath an Ant Path object containing the classpath.
 170   
      */
 171  0
     public void setClasspath(Path classpath) {
 172  0
         if (this.classpath == null) {
 173  0
             this.classpath = classpath;
 174   
         } else {
 175  0
             this.classpath.append(classpath);
 176   
         }
 177   
     }
 178   
 
 179  0
     public Path createClasspath() {
 180  0
         if (this.classpath == null) {
 181  0
             this.classpath = new Path(null);
 182   
         }
 183  0
         return this.classpath.createPath();
 184   
     }
 185   
 
 186   
 
 187  0
     public void execute() {
 188  0
         try {
 189   
             // Gump friendly - don't mess with the core loader if only classpath
 190  0
             if( "only".equals( project.getProperty("build.sysclasspath")) &&
 191   
                     (name==null || SYSTEM_LOADER_REF.equals( name ))) {
 192  0
                 log( "Changing the system loader is disabled " +
 193   
                         "by build.sysclasspath=only",
 194   
                         Project.MSG_WARN);
 195  0
                 return;
 196   
             }
 197   
 
 198  0
             String loaderName=(name==null) ? SYSTEM_LOADER_REF : name;
 199   
 
 200  0
             Object obj=project.getReference(loaderName);
 201  0
             if( reset ) {
 202   
                 // Are any other references held ? Can we 'close' the loader
 203   
                 // so it removes the locks on jars ?
 204  0
                 obj=null; // a new one will be created.
 205   
             }
 206   
 
 207   
             // XXX maybe use reflection to addPathElement ( other patterns ?)
 208  0
             if( obj!=null && !(obj instanceof AntClassLoader )) {
 209  0
                 log( "Referenced object is not an AntClassLoader",
 210   
                         Project.MSG_ERR);
 211  0
                 return;
 212   
             }
 213   
 
 214  0
             AntClassLoader acl=(AntClassLoader)obj;
 215   
 
 216  0
             if( acl==null ) {
 217   
                 // Construct a new class loader
 218  0
                 Object parent=null;
 219  0
                 if( parentName != null ) {
 220  0
                     parent=project.getReference(parentName);
 221  0
                     if( !(parent instanceof ClassLoader) ) {
 222  0
                         parent=null;
 223   
                     }
 224   
                 }
 225   
                 // TODO: allow user to request the system or no parent
 226  0
                 if( parent==null ) {
 227  0
                     parent=this.getClass().getClassLoader();
 228   
                 }
 229   
 
 230  0
                 if( name==null ) {
 231   
                     // The core loader must be reverse
 232   
                     //reverse=true;
 233   
                 }
 234  0
                 project.log("Setting parent loader " + name + " " +
 235   
                         parent + " " + parentFirst, Project.MSG_DEBUG);
 236   
 
 237   
                 // The param is "parentFirst"
 238  0
                 acl=new AntClassLoader( (ClassLoader)parent,
 239   
                         project, classpath, parentFirst );
 240   
 
 241  0
                 project.addReference( loaderName, acl );
 242   
 
 243  0
                 if( name==null ) {
 244   
                     // This allows the core loader to load optional tasks
 245   
                     // without delegating
 246  0
                     acl.addLoaderPackageRoot( "org.apache.tools.ant.taskdefs.optional");
 247  0
                     project.setCoreLoader(acl);
 248   
                 }
 249   
             }
 250  0
             if( classpath != null ) {
 251  0
                 String list[]=classpath.list();
 252  0
                 for( int i=0; i<list.length; i++ ) {
 253  0
                     File f= new File( list[i] );
 254  0
                     if( f.exists() ) {
 255  0
                         acl.addPathElement(f.getAbsolutePath());
 256  0
                         log("Adding to class loader " +  acl + " " + f.getAbsolutePath(),
 257   
                                 Project.MSG_DEBUG);
 258   
                     }
 259   
                 }
 260   
             }
 261   
 
 262   
             // XXX add exceptions
 263   
 
 264   
         } catch( Exception ex ) {
 265  0
             ex.printStackTrace();
 266   
         }
 267   
     }
 268   
 }
 269