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.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 }