KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > extension > JarLibResolveTask


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 package org.apache.tools.ant.taskdefs.optional.extension;
19
20 import java.io.File JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.jar.Manifest JavaDoc;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.Task;
26 import org.apache.tools.ant.taskdefs.optional.extension.resolvers.AntResolver;
27 import org.apache.tools.ant.taskdefs.optional.extension.resolvers.LocationResolver;
28 import org.apache.tools.ant.taskdefs.optional.extension.resolvers.URLResolver;
29
30 /**
31  * Tries to locate a JAR to satisfy an extension and place
32  * location of JAR into property.
33  *
34  * @ant.task name="jarlib-resolve"
35  */

36 public class JarLibResolveTask extends Task {
37     /**
38      * The name of the property in which the location of
39      * library is stored.
40      */

41     private String JavaDoc propertyName;
42
43     /**
44      * The extension that is required.
45      */

46     private Extension requiredExtension;
47
48     /**
49      * The set of resolvers to use to attempt to locate library.
50      */

51     private final ArrayList JavaDoc resolvers = new ArrayList JavaDoc();
52
53     /**
54      * Flag to indicate that you should check that
55      * the librarys resolved actually contain
56      * extension and if they don't then raise
57      * an exception.
58      */

59     private boolean checkExtension = true;
60
61     /**
62      * Flag indicating whether or not you should
63      * throw a BuildException if you cannot resolve
64      * library.
65      */

66     private boolean failOnError = true;
67
68     /**
69      * The name of the property in which the location of
70      * library is stored.
71      *
72      * @param property The name of the property in which the location of
73      * library is stored.
74      */

75     public void setProperty(final String JavaDoc property) {
76         this.propertyName = property;
77     }
78
79     /**
80      * Check nested libraries for extensions
81      *
82      * @param checkExtension if true, libraries returned by nested
83      * resolvers should be checked to see if they supply extension.
84      */

85     public void setCheckExtension(final boolean checkExtension) {
86         this.checkExtension = checkExtension;
87     }
88
89     /**
90      * Set whether to fail if error.
91      *
92      * @param failOnError if true, failure to locate library should fail build.
93      */

94     public void setFailOnError(final boolean failOnError) {
95         this.failOnError = failOnError;
96     }
97
98     /**
99      * Adds location resolver to look for a library in a location
100      * relative to project directory.
101      *
102      * @param loc the resolver location to search.
103      */

104     public void addConfiguredLocation(final LocationResolver loc) {
105         resolvers.add(loc);
106     }
107
108     /**
109      * Adds a URL resolver to download a library from a URL
110      * to a local file.
111      *
112      * @param url the URL resolver from which to download the library
113      */

114     public void addConfiguredUrl(final URLResolver url) {
115         resolvers.add(url);
116     }
117
118     /**
119      * Adds Ant resolver to run an Ant build file to generate a library.
120      *
121      * @param ant the AntResolver to generate the library.
122      */

123     public void addConfiguredAnt(final AntResolver ant) {
124         resolvers.add(ant);
125     }
126
127     /**
128      * Set the Extension looking for.
129      *
130      * @param extension Set the Extension looking for.
131      */

132     public void addConfiguredExtension(final ExtensionAdapter extension) {
133         if (null != requiredExtension) {
134             final String JavaDoc message = "Can not specify extension to "
135                 + "resolve multiple times.";
136             throw new BuildException(message);
137         }
138         requiredExtension = extension.toExtension();
139     }
140
141     /**
142      * Execute the task.
143      *
144      * @throws BuildException if the task fails.
145      */

146     public void execute() throws BuildException {
147         validate();
148
149         getProject().log("Resolving extension: " + requiredExtension,
150                           Project.MSG_VERBOSE);
151
152         String JavaDoc candidate =
153             getProject().getProperty(propertyName);
154
155         if (null != candidate) {
156             final String JavaDoc message = "Property Already set to: " + candidate;
157             if (failOnError) {
158                 throw new BuildException(message);
159             }
160             getProject().log(message, Project.MSG_ERR);
161             return;
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 JavaDoc file =
174                     resolver.resolve(requiredExtension, getProject());
175                 try {
176                     checkExtension(file);
177                     return;
178                 } catch (final BuildException be) {
179                     final String JavaDoc 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 JavaDoc 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 JavaDoc message =
201             "Unable to resolve extension to a file";
202         if (failOnError) {
203             throw new BuildException(message);
204         }
205         getProject().log(message, Project.MSG_ERR);
206     }
207
208     /**
209      * Check if specified file satisfies extension.
210      * If it does then set the relevent property
211      * else throw a BuildException.
212      *
213      * @param file the candidate library
214      * @throws BuildException if library does not satisfy extension
215      */

216     private void checkExtension(final File JavaDoc file) {
217         if (!file.exists()) {
218             final String JavaDoc message =
219                 "File " + file + " does not exist";
220             throw new BuildException(message);
221         }
222         if (!file.isFile()) {
223             final String JavaDoc message =
224                 "File " + file + " is not a file";
225             throw new BuildException(message);
226         }
227
228         if (!checkExtension) {
229             final String JavaDoc message = "Setting property to " + file
230                 + " without verifying library satisfies extension";
231             getProject().log(message, Project.MSG_VERBOSE);
232             setLibraryProperty(file);
233         } else {
234             getProject().log("Checking file " + file
235                 + " to see if it satisfies extension", Project.MSG_VERBOSE);
236             final Manifest JavaDoc manifest =
237                 ExtensionUtil.getManifest(file);
238             final Extension[] extensions =
239                 Extension.getAvailable(manifest);
240             for (int i = 0; i < extensions.length; i++) {
241                 final Extension extension = extensions[ i ];
242                 if (extension.isCompatibleWith(requiredExtension)) {
243                     setLibraryProperty(file);
244                     return;
245                 }
246             }
247
248             getProject().log("File " + file + " skipped as it "
249                 + "does not satisfy extension", Project.MSG_VERBOSE);
250
251             final String JavaDoc message =
252                 "File " + file + " does not satisfy extension";
253             throw new BuildException(message);
254         }
255     }
256
257     /**
258      * Utility method to set the appropriate property
259      * to indicate that specified file satisfies library
260      * requirements.
261      *
262      * @param file the library
263      */

264     private void setLibraryProperty(final File JavaDoc file) {
265         getProject().setNewProperty(propertyName,
266                                      file.getAbsolutePath());
267     }
268
269     /**
270      * Validate the tasks parameters.
271      *
272      * @throws BuildException if invalid parameters found
273      */

274     private void validate() throws BuildException {
275         if (null == propertyName) {
276             final String JavaDoc message = "Property attribute must be specified.";
277             throw new BuildException(message);
278         }
279
280         if (null == requiredExtension) {
281             final String JavaDoc message = "Extension element must be specified.";
282             throw new BuildException(message);
283         }
284     }
285 }
286
Popular Tags