1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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.2 $
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 lc.addFile(list[i]);
119 }
120 return new RootLoader(lc);
121 }
122
123 /***
124 * Set the classpath to be used for this compilation.
125 *
126 * @param classpath an Ant Path object containing the compilation classpath.
127 */
128 public void setClasspath(Path classpath) {
129 if (taskClasspath == null) {
130 taskClasspath = classpath;
131 }
132 else {
133 taskClasspath.append(classpath);
134 }
135 }
136
137 /***
138 * Adds a reference to a classpath defined elsewhere.
139 * @param r a reference to a classpath
140 */
141 public void setClasspathRef(Reference r) {
142 createClasspath().setRefid(r);
143 }
144
145 /***
146 * Adds a path to the classpath.
147 * @return a class path to be configured
148 */
149 public Path createClasspath() {
150 if (taskClasspath == null) {
151 taskClasspath = new Path(getProject());
152 }
153 return taskClasspath.createPath();
154 }
155 }