KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Antlib


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.IOException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.tools.ant.TaskContainer;
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.ComponentHelper;
30 import org.apache.tools.ant.Project;
31 import org.apache.tools.ant.Task;
32 import org.apache.tools.ant.helper.ProjectHelper2;
33 import org.apache.tools.ant.UnknownElement;
34
35
36 /**
37  * Antlib task. It does not
38  * occur in an ant build file. It is the root element
39  * an antlib xml file.
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 JavaDoc 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 JavaDoc antlibUrl,
61                                       String JavaDoc uri) {
62         // Check if we can contact the URL
63
try {
64             antlibUrl.openConnection().connect();
65         } catch (IOException JavaDoc 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 JavaDoc classLoader;
100     private String JavaDoc uri = "";
101     private List JavaDoc tasks = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc uri) {
119         this.uri = uri;
120     }
121
122     private ClassLoader JavaDoc 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 JavaDoc i = tasks.iterator(); i.hasNext();) {
144             UnknownElement ue = (UnknownElement) i.next();
145             setLocation(ue.getLocation());
146             ue.maybeConfigure();
147             Object JavaDoc 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 }
166
Popular Tags