View Javadoc

1   /*
2    $Id: RootLoaderRef.java,v 1.4 2006/06/15 13:31:42 blackdrag Exp $
3   
4    Copyright 2003 (C) Jochen Theodorou. All Rights Reserved.
5   
6    Redistribution and use of this software and associated documentation
7    ("Software"), with or without modification, are permitted provided
8    that the following conditions are met:
9   
10   1. Redistributions of source code must retain copyright
11      statements and notices.  Redistributions must also contain a
12      copy of this document.
13  
14   2. Redistributions in binary form must reproduce the
15      above copyright notice, this list of conditions and the
16      following disclaimer in the documentation and/or other
17      materials provided with the distribution.
18  
19   3. The name "groovy" must not be used to endorse or promote
20      products derived from this Software without prior written
21      permission of The Codehaus.  For written permission,
22      please contact info@codehaus.org.
23  
24   4. Products derived from this Software may not be called "groovy"
25      nor may "groovy" appear in their names without prior written
26      permission of The Codehaus. "groovy" is a registered
27      trademark of The Codehaus.
28  
29   5. Due credit should be given to The Codehaus -
30      http://groovy.codehaus.org/
31  
32   THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
33   ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
34   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
36   THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43   OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45   */
46   
47  package org.codehaus.groovy.ant;
48  
49  import org.apache.tools.ant.AntClassLoader;
50  import org.apache.tools.ant.BuildException;
51  import org.apache.tools.ant.Project;
52  import org.apache.tools.ant.taskdefs.MatchingTask;
53  import org.apache.tools.ant.types.Path;
54  import org.apache.tools.ant.types.Reference;
55  import org.codehaus.groovy.tools.LoaderConfiguration;
56  import org.codehaus.groovy.tools.RootLoader;
57  
58   
59  /***
60   * Sets the RootLoader as reference.
61   * Reexecution of this task will set a new instance of RootLoader for
62   * the reference. 
63   *
64   * arguments:
65   * <ul>
66   * <li>ref</li>
67   * <li>classpath</li>
68   * </ul>
69   * 
70   * all arguments are requiered. 
71   *
72   * As ant requieres an AntClassLoader as reference, this will create a RootLoader
73   * and set an AntClassLoader as child and stored in the reference. The AntClassLoader
74   * instance will not have a classpath nor will it have access to the classpath somehow,
75   * all loading is done by the RootLoader parent. To avoid problems with loading classes 
76   * multiple times and using them at the same time, this task will filter out the ant jars
77   * and the commons-logging jars. This only works if the ant jars are starting with "ant-" and
78   * the logging jar starts with "commons-logging-".
79   * 
80   * This was needed because if ant wants to access a task argument that uses for example a Path
81   * it look for a matching method which includes a matching class. But two classes of the same name
82   * with different classloaders are different, so ant would not be able to find the method.
83   *
84   * @see org.codehaus.groovy.tools.RootLoader
85   * @author Jochen Theodorou
86   * @version $Revision: 1.4 $ 
87   */
88  public class RootLoaderRef extends MatchingTask {
89      private String name;
90      private Path taskClasspath;
91      
92      /***
93       * sets the name of the reference which should store the Loader
94       */
95      public void setRef(String n){
96          name = n;
97      }
98      
99      public void execute() throws BuildException {
100         if (taskClasspath==null || taskClasspath.size()==0) {
101             throw new BuildException("no classpath given");
102         }
103         Project project = getProject();
104         AntClassLoader loader = new AntClassLoader(makeRoot(),true);
105         project.addReference(name,loader);
106     }
107     
108     private RootLoader makeRoot() {
109         String[] list = taskClasspath.list();
110         LoaderConfiguration lc = new LoaderConfiguration();
111         for (int i=0; i<list.length; i++) {
112             if (list[i].matches(".*ant-[^/]*jar$")) {
113                 continue;
114             }
115             if (list[i].matches(".*commons-logging-[^/]*jar$")) {
116                 continue;
117             }
118             if (list[i].matches(".*xerces-[^/]*jar$")) {
119                 continue;
120             }
121             lc.addFile(list[i]);
122         }
123         return new RootLoader(lc);
124     }
125     
126     /***
127      * Set the classpath to be used for this compilation.
128      *
129      * @param classpath an Ant Path object containing the compilation classpath.
130      */
131     public void setClasspath(Path classpath) {
132         if (taskClasspath == null) {
133             taskClasspath = classpath;
134         }
135         else {
136             taskClasspath.append(classpath);
137         }
138     }
139     
140     /***
141      * Adds a reference to a classpath defined elsewhere.
142      * @param r a reference to a classpath
143      */
144     public void setClasspathRef(Reference r) {
145         createClasspath().setRefid(r);
146     }
147     
148     /***
149      * Adds a path to the classpath.
150      * @return a class path to be configured
151      */
152     public Path createClasspath() {
153         if (taskClasspath == null) {
154             taskClasspath = new Path(getProject());
155         }
156         return taskClasspath.createPath();
157     }
158 }