1 | /*
|
---|
2 | * Copyright 2002-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 | package org.apache.tools.ant.taskdefs.optional.extension;
|
---|
18 |
|
---|
19 | import java.io.File;
|
---|
20 | import java.util.ArrayList;
|
---|
21 | import java.util.jar.Manifest;
|
---|
22 | import org.apache.tools.ant.BuildException;
|
---|
23 | import org.apache.tools.ant.Project;
|
---|
24 | import org.apache.tools.ant.Task;
|
---|
25 | import org.apache.tools.ant.taskdefs.optional.extension.resolvers.AntResolver;
|
---|
26 | import org.apache.tools.ant.taskdefs.optional.extension.resolvers.LocationResolver;
|
---|
27 | import org.apache.tools.ant.taskdefs.optional.extension.resolvers.URLResolver;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Tries to locate a JAR to satisfy an extension and place
|
---|
31 | * location of JAR into property.
|
---|
32 | *
|
---|
33 | * @ant.task name="jarlib-resolve"
|
---|
34 | */
|
---|
35 | public class JarLibResolveTask extends Task {
|
---|
36 | /**
|
---|
37 | * The name of the property in which the location of
|
---|
38 | * library is stored.
|
---|
39 | */
|
---|
40 | private String propertyName;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * The extension that is required.
|
---|
44 | */
|
---|
45 | private Extension requiredExtension;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * The set of resolvers to use to attempt to locate library.
|
---|
49 | */
|
---|
50 | private final ArrayList resolvers = new ArrayList();
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Flag to indicate that you should check that
|
---|
54 | * the librarys resolved actually contain
|
---|
55 | * extension and if they don't then raise
|
---|
56 | * an exception.
|
---|
57 | */
|
---|
58 | private boolean checkExtension = true;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Flag indicating whether or not you should
|
---|
62 | * throw a BuildException if you cannot resolve
|
---|
63 | * library.
|
---|
64 | */
|
---|
65 | private boolean failOnError = true;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * The name of the property in which the location of
|
---|
69 | * library is stored.
|
---|
70 | *
|
---|
71 | * @param property The name of the property in which the location of
|
---|
72 | * library is stored.
|
---|
73 | */
|
---|
74 | public void setProperty(final String property) {
|
---|
75 | this.propertyName = property;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Check nested libraries for extensions
|
---|
80 | *
|
---|
81 | * @param checkExtension if true, libraries returned by nested
|
---|
82 | * resolvers should be checked to see if they supply extension.
|
---|
83 | */
|
---|
84 | public void setCheckExtension(final boolean checkExtension) {
|
---|
85 | this.checkExtension = checkExtension;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Set whether to fail if error.
|
---|
90 | *
|
---|
91 | * @param failOnError if true, failure to locate library should fail build.
|
---|
92 | */
|
---|
93 | public void setFailOnError(final boolean failOnError) {
|
---|
94 | this.failOnError = failOnError;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Adds location resolver to look for a library in a location
|
---|
99 | * relative to project directory.
|
---|
100 | *
|
---|
101 | * @param location the resolver location to search.
|
---|
102 | */
|
---|
103 | public void addConfiguredLocation(final LocationResolver location) {
|
---|
104 | resolvers.add(location);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Adds a URL resolver to download a library from a URL
|
---|
109 | * to a local file.
|
---|
110 | *
|
---|
111 | * @param url the URL resolver from which to download the library
|
---|
112 | */
|
---|
113 | public void addConfiguredUrl(final URLResolver url) {
|
---|
114 | resolvers.add(url);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Adds Ant resolver to run an Ant build file to generate a library.
|
---|
119 | *
|
---|
120 | * @param ant the AntResolver to generate the library.
|
---|
121 | */
|
---|
122 | public void addConfiguredAnt(final AntResolver ant) {
|
---|
123 | resolvers.add(ant);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Set the Extension looking for.
|
---|
128 | *
|
---|
129 | * @param extension Set the Extension looking for.
|
---|
130 | */
|
---|
131 | public void addConfiguredExtension(final ExtensionAdapter extension) {
|
---|
132 | if (null != requiredExtension) {
|
---|
133 | final String message = "Can not specify extension to "
|
---|
134 | + "resolve multiple times.";
|
---|
135 | throw new BuildException(message);
|
---|
136 | }
|
---|
137 | requiredExtension = extension.toExtension();
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Execute the task.
|
---|
142 | *
|
---|
143 | * @throws BuildException if the task fails.
|
---|
144 | */
|
---|
145 | public void execute() throws BuildException {
|
---|
146 | validate();
|
---|
147 |
|
---|
148 | getProject().log("Resolving extension: " + requiredExtension,
|
---|
149 | Project.MSG_VERBOSE);
|
---|
150 |
|
---|
151 | String candidate =
|
---|
152 | getProject().getProperty(propertyName);
|
---|
153 |
|
---|
154 | if (null != candidate) {
|
---|
155 | final String message = "Property Already set to: " + candidate;
|
---|
156 | if (failOnError) {
|
---|
157 | throw new BuildException(message);
|
---|
158 | } else {
|
---|
159 | getProject().log(message, Project.MSG_ERR);
|
---|
160 | return;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | final int size = resolvers.size();
|
---|
165 | for (int i = 0; i < size; i++) {
|
---|
166 | final ExtensionResolver resolver =
|
---|
167 | (ExtensionResolver) resolvers.get(i);
|
---|
168 |
|
---|
169 | getProject().log("Searching for extension using Resolver:" + resolver,
|
---|
170 | Project.MSG_VERBOSE);
|
---|
171 |
|
---|
172 | try {
|
---|
173 | final File file =
|
---|
174 | resolver.resolve(requiredExtension, getProject());
|
---|
175 | try {
|
---|
176 | checkExtension(file);
|
---|
177 | return;
|
---|
178 | } catch (final BuildException be) {
|
---|
179 | final String message = "File " + file + " returned by "
|
---|
180 | + "resolver failed to satisfy extension due to: "
|
---|
181 | + be.getMessage();
|
---|
182 | getProject().log(message, Project.MSG_WARN);
|
---|
183 | }
|
---|
184 | } catch (final BuildException be) {
|
---|
185 | final String message = "Failed to resolve extension to file "
|
---|
186 | + "using resolver " + resolver + " due to: " + be;
|
---|
187 | getProject().log(message, Project.MSG_WARN);
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | missingExtension();
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Utility method that will throw a {@link BuildException}
|
---|
196 | * if {@link #failOnError} is true else it just displays
|
---|
197 | * a warning.
|
---|
198 | */
|
---|
199 | private void missingExtension() {
|
---|
200 | final String message =
|
---|
201 | "Unable to resolve extension to a file";
|
---|
202 | if (failOnError) {
|
---|
203 | throw new BuildException(message);
|
---|
204 | } else {
|
---|
205 | getProject().log(message, Project.MSG_ERR);
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Check if specified file satisfies extension.
|
---|
211 | * If it does then set the relevent property
|
---|
212 | * else throw a BuildException.
|
---|
213 | *
|
---|
214 | * @param file the candidate library
|
---|
215 | * @throws BuildException if library does not satisfy extension
|
---|
216 | */
|
---|
217 | private void checkExtension(final File file) {
|
---|
218 | if (!file.exists()) {
|
---|
219 | final String message =
|
---|
220 | "File " + file + " does not exist";
|
---|
221 | throw new BuildException(message);
|
---|
222 | }
|
---|
223 | if (!file.isFile()) {
|
---|
224 | final String message =
|
---|
225 | "File " + file + " is not a file";
|
---|
226 | throw new BuildException(message);
|
---|
227 | }
|
---|
228 |
|
---|
229 | if (!checkExtension) {
|
---|
230 | final String message = "Setting property to " + file
|
---|
231 | + " without verifying library satisfies extension";
|
---|
232 | getProject().log(message, Project.MSG_VERBOSE);
|
---|
233 | setLibraryProperty(file);
|
---|
234 | } else {
|
---|
235 | getProject().log("Checking file " + file
|
---|
236 | + " to see if it satisfies extension", Project.MSG_VERBOSE);
|
---|
237 | final Manifest manifest =
|
---|
238 | ExtensionUtil.getManifest(file);
|
---|
239 | final Extension[] extensions =
|
---|
240 | Extension.getAvailable(manifest);
|
---|
241 | for (int i = 0; i < extensions.length; i++) {
|
---|
242 | final Extension extension = extensions[ i ];
|
---|
243 | if (extension.isCompatibleWith(requiredExtension)) {
|
---|
244 | setLibraryProperty(file);
|
---|
245 | return;
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | getProject().log("File " + file + " skipped as it "
|
---|
250 | + "does not satisfy extension", Project.MSG_VERBOSE);
|
---|
251 |
|
---|
252 | final String message =
|
---|
253 | "File " + file + " does not satisfy extension";
|
---|
254 | throw new BuildException(message);
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Utility method to set the appropriate property
|
---|
260 | * to indicate that specified file satisfies library
|
---|
261 | * requirements.
|
---|
262 | *
|
---|
263 | * @param file the library
|
---|
264 | */
|
---|
265 | private void setLibraryProperty(final File file) {
|
---|
266 | getProject().setNewProperty(propertyName,
|
---|
267 | file.getAbsolutePath());
|
---|
268 | }
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Validate the tasks parameters.
|
---|
272 | *
|
---|
273 | * @throws BuildException if invalid parameters found
|
---|
274 | */
|
---|
275 | private void validate() throws BuildException {
|
---|
276 | if (null == propertyName) {
|
---|
277 | final String message = "Property attribute must be specified.";
|
---|
278 | throw new BuildException(message);
|
---|
279 | }
|
---|
280 |
|
---|
281 | if (null == requiredExtension) {
|
---|
282 | final String message = "Extension element must be specified.";
|
---|
283 | throw new BuildException(message);
|
---|
284 | }
|
---|
285 | }
|
---|
286 | }
|
---|