587
|
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 |
/* * Portion Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
|
|
20 |
|
|
21 |
package com.nokia.ant;
|
|
22 |
|
|
23 |
import java.io.IOException;
|
|
24 |
import java.net.URL;
|
|
25 |
import java.util.ArrayList;
|
|
26 |
import java.util.List;
|
|
27 |
|
|
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;
|
628
|
32 |
import org.apache.tools.ant.TaskContainer;
|
587
|
33 |
import org.apache.tools.ant.UnknownElement;
|
628
|
34 |
import org.apache.tools.ant.helper.ProjectHelper2;
|
|
35 |
import org.apache.tools.ant.taskdefs.AntlibDefinition;
|
587
|
36 |
|
|
37 |
/**
|
|
38 |
* Antlib task. It does not
|
|
39 |
* occur in an ant build file. It is the root element
|
|
40 |
* an antlib xml file.
|
|
41 |
* @ant.task ignore="true"
|
|
42 |
* @since Ant 1.6
|
|
43 |
*/
|
|
44 |
public class Antlib extends Task implements TaskContainer {
|
|
45 |
//
|
|
46 |
// Static
|
|
47 |
//
|
|
48 |
|
|
49 |
/** The name of this task */
|
|
50 |
public static final String TAG = "antlib";
|
|
51 |
|
|
52 |
//
|
|
53 |
// Instance
|
|
54 |
//
|
|
55 |
private ClassLoader classLoader;
|
|
56 |
private String uri = "";
|
|
57 |
private List<Task> tasks = new ArrayList<Task>();
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Static method to read an ant lib definition from
|
|
61 |
* a url.
|
|
62 |
*
|
|
63 |
* @param project the current project
|
|
64 |
* @param antlibUrl the url to read the definitions from
|
|
65 |
* @param uri the uri that the antlib is to be placed in
|
|
66 |
* @return the ant lib task
|
|
67 |
*/
|
|
68 |
public static Antlib createAntlib(final Project project, final URL antlibUrl,
|
|
69 |
final String uri) {
|
|
70 |
// Check if we can contact the URL
|
|
71 |
try {
|
|
72 |
antlibUrl.openConnection().connect();
|
|
73 |
} catch (IOException ex) {
|
|
74 |
throw new BuildException(
|
|
75 |
"Unable to find " + antlibUrl, ex);
|
|
76 |
}
|
|
77 |
ComponentHelper helper =
|
|
78 |
ComponentHelper.getComponentHelper(project);
|
|
79 |
helper.enterAntLib(uri);
|
|
80 |
try {
|
|
81 |
// Should be safe to parse
|
|
82 |
ProjectHelper2 parser = new ProjectHelper2();
|
|
83 |
UnknownElement ue =
|
|
84 |
parser.parseUnknownElement(project, antlibUrl);
|
|
85 |
// Check name is "antlib"
|
|
86 |
if (!(ue.getTag().equals(TAG))) {
|
|
87 |
throw new BuildException(
|
|
88 |
"Unexpected tag " + ue.getTag() + " expecting "
|
|
89 |
+ TAG, ue.getLocation());
|
|
90 |
}
|
|
91 |
Antlib antlib = new Antlib();
|
|
92 |
antlib.setProject(project);
|
|
93 |
antlib.setLocation(ue.getLocation());
|
|
94 |
antlib.setTaskName("antlib");
|
|
95 |
antlib.init();
|
|
96 |
ue.configure(antlib);
|
|
97 |
return antlib;
|
|
98 |
} finally {
|
|
99 |
helper.exitAntLib();
|
|
100 |
}
|
|
101 |
}
|
|
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 final void setClassLoader(final ClassLoader classLoader)
|
|
111 |
{
|
|
112 |
this.classLoader = classLoader;
|
|
113 |
}
|
|
114 |
|
|
115 |
/**
|
|
116 |
* Set the URI for this antlib.
|
|
117 |
* @param uri the namespace uri
|
|
118 |
*/
|
|
119 |
protected final void setURI(final String uri)
|
|
120 |
{
|
|
121 |
this.uri = uri;
|
|
122 |
}
|
|
123 |
|
|
124 |
private ClassLoader getClassLoader() {
|
|
125 |
if (classLoader == null) {
|
|
126 |
classLoader = Antlib.class.getClassLoader();
|
|
127 |
}
|
|
128 |
return classLoader;
|
|
129 |
}
|
|
130 |
|
|
131 |
/**
|
|
132 |
* add a task to the list of tasks
|
|
133 |
*
|
|
134 |
* @param nestedTask Nested task to execute in antlib
|
|
135 |
*/
|
|
136 |
public final void addTask(final Task nestedTask) {
|
|
137 |
tasks.add(nestedTask);
|
|
138 |
}
|
|
139 |
|
|
140 |
/**
|
|
141 |
* Execute the nested tasks, setting the classloader for
|
|
142 |
* any tasks that derive from Definer.
|
|
143 |
*/
|
|
144 |
public final void execute() {
|
588
|
145 |
for (Task task : tasks) {
|
|
146 |
if (task instanceof UnknownElement) {
|
|
147 |
UnknownElement ue = (UnknownElement)task;
|
587
|
148 |
setLocation(ue.getLocation());
|
|
149 |
ue.maybeConfigure();
|
|
150 |
Object configuredObject = ue.getRealThing();
|
|
151 |
if (configuredObject == null) {
|
|
152 |
continue;
|
|
153 |
}
|
|
154 |
if (!(configuredObject instanceof AntlibDefinition)) {
|
|
155 |
throw new BuildException(
|
|
156 |
"Invalid task in antlib " + ue.getTag()
|
|
157 |
+ " " + configuredObject.getClass() + " does not "
|
|
158 |
+ "extend org.apache.tools.ant.taskdefs.AntlibDefinition");
|
|
159 |
}
|
|
160 |
AntlibDefinition def = (AntlibDefinition) configuredObject;
|
|
161 |
def.setURI(uri);
|
|
162 |
def.setAntlibClassLoader(getClassLoader());
|
|
163 |
def.init();
|
|
164 |
def.execute();
|
|
165 |
}
|
|
166 |
}
|
|
167 |
}
|
|
168 |
|
|
169 |
}
|