1 | /*
|
---|
2 | * Copyright 2003-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 | import java.io.IOException;
|
---|
21 | import java.net.URL;
|
---|
22 | import java.util.ArrayList;
|
---|
23 | import java.util.Iterator;
|
---|
24 | import java.util.List;
|
---|
25 |
|
---|
26 | import org.apache.tools.ant.TaskContainer;
|
---|
27 | import org.apache.tools.ant.BuildException;
|
---|
28 | import org.apache.tools.ant.ComponentHelper;
|
---|
29 | import org.apache.tools.ant.Project;
|
---|
30 | import org.apache.tools.ant.Task;
|
---|
31 | import org.apache.tools.ant.helper.ProjectHelper2;
|
---|
32 | import org.apache.tools.ant.UnknownElement;
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Antlib task. It does not
|
---|
37 | * occur in an ant build file. It is the root element
|
---|
38 | * an antlib xml file.
|
---|
39 | *
|
---|
40 | *
|
---|
41 | * @since Ant 1.6
|
---|
42 | */
|
---|
43 | public class Antlib extends Task implements TaskContainer {
|
---|
44 | //
|
---|
45 | // Static
|
---|
46 | //
|
---|
47 |
|
---|
48 | /** The name of this task */
|
---|
49 | public static final String TAG = "antlib";
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Static method to read an ant lib definition from
|
---|
53 | * a url.
|
---|
54 | *
|
---|
55 | * @param project the current project
|
---|
56 | * @param antlibUrl the url to read the definitions from
|
---|
57 | * @param uri the uri that the antlib is to be placed in
|
---|
58 | * @return the ant lib task
|
---|
59 | */
|
---|
60 | public static Antlib createAntlib(Project project, URL antlibUrl,
|
---|
61 | String uri) {
|
---|
62 | // Check if we can contact the URL
|
---|
63 | try {
|
---|
64 | antlibUrl.openConnection().connect();
|
---|
65 | } catch (IOException ex) {
|
---|
66 | throw new BuildException(
|
---|
67 | "Unable to find " + antlibUrl, ex);
|
---|
68 | }
|
---|
69 | ComponentHelper helper =
|
---|
70 | ComponentHelper.getComponentHelper(project);
|
---|
71 | helper.enterAntLib(uri);
|
---|
72 | try {
|
---|
73 | // Should be safe to parse
|
---|
74 | ProjectHelper2 parser = new ProjectHelper2();
|
---|
75 | UnknownElement ue =
|
---|
76 | parser.parseUnknownElement(project, antlibUrl);
|
---|
77 | // Check name is "antlib"
|
---|
78 | if (!(ue.getTag().equals(TAG))) {
|
---|
79 | throw new BuildException(
|
---|
80 | "Unexpected tag " + ue.getTag() + " expecting "
|
---|
81 | + TAG, ue.getLocation());
|
---|
82 | }
|
---|
83 | Antlib antlib = new Antlib();
|
---|
84 | antlib.setProject(project);
|
---|
85 | antlib.setLocation(ue.getLocation());
|
---|
86 | antlib.setTaskName("antlib");
|
---|
87 | antlib.init();
|
---|
88 | ue.configure(antlib);
|
---|
89 | return antlib;
|
---|
90 | } finally {
|
---|
91 | helper.exitAntLib();
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | //
|
---|
97 | // Instance
|
---|
98 | //
|
---|
99 | private ClassLoader classLoader;
|
---|
100 | private String uri = "";
|
---|
101 | private List tasks = new ArrayList();
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Set the class loader for this antlib.
|
---|
105 | * This class loader is used for any tasks that
|
---|
106 | * derive from Definer.
|
---|
107 | *
|
---|
108 | * @param classLoader the class loader
|
---|
109 | */
|
---|
110 | protected void setClassLoader(ClassLoader classLoader) {
|
---|
111 | this.classLoader = classLoader;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Set the URI for this antlib.
|
---|
116 | * @param uri the namespace uri
|
---|
117 | */
|
---|
118 | protected void setURI(String uri) {
|
---|
119 | this.uri = uri;
|
---|
120 | }
|
---|
121 |
|
---|
122 | private ClassLoader getClassLoader() {
|
---|
123 | if (classLoader == null) {
|
---|
124 | classLoader = Antlib.class.getClassLoader();
|
---|
125 | }
|
---|
126 | return classLoader;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * add a task to the list of tasks
|
---|
131 | *
|
---|
132 | * @param nestedTask Nested task to execute in antlib
|
---|
133 | */
|
---|
134 | public void addTask(Task nestedTask) {
|
---|
135 | tasks.add(nestedTask);
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Execute the nested tasks, setting the classloader for
|
---|
140 | * any tasks that derive from Definer.
|
---|
141 | */
|
---|
142 | public void execute() {
|
---|
143 | for (Iterator i = tasks.iterator(); i.hasNext();) {
|
---|
144 | UnknownElement ue = (UnknownElement) i.next();
|
---|
145 | setLocation(ue.getLocation());
|
---|
146 | ue.maybeConfigure();
|
---|
147 | Object configuredObject = ue.getRealThing();
|
---|
148 | if (configuredObject == null) {
|
---|
149 | continue;
|
---|
150 | }
|
---|
151 | if (!(configuredObject instanceof AntlibDefinition)) {
|
---|
152 | throw new BuildException(
|
---|
153 | "Invalid task in antlib " + ue.getTag()
|
---|
154 | + " " + configuredObject.getClass() + " does not "
|
---|
155 | + "extend org.apache.tools.ant.taskdefs.AntlibDefinition");
|
---|
156 | }
|
---|
157 | AntlibDefinition def = (AntlibDefinition) configuredObject;
|
---|
158 | def.setURI(uri);
|
---|
159 | def.setAntlibClassLoader(getClassLoader());
|
---|
160 | def.init();
|
---|
161 | def.execute();
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | }
|
---|