KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
22 import java.util.Vector JavaDoc;
23 import java.util.jar.Manifest JavaDoc;
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.Task;
26
27 /**
28  * Checks whether an extension is present in a fileset or an extensionSet.
29  *
30  * @ant.task name="jarlib-available"
31  */

32 public class JarLibAvailableTask extends Task {
33     /**
34      * The library to display information about.
35      */

36     private File JavaDoc libraryFile;
37
38     /**
39      * Filesets specifying all the librarys
40      * to display information about.
41      */

42     private final Vector JavaDoc extensionFileSets = new Vector JavaDoc();
43
44     /**
45      * The name of the property to set if extension is available.
46      */

47     private String JavaDoc propertyName;
48
49     /**
50      * The extension that is required.
51      */

52     private ExtensionAdapter requiredExtension;
53
54     /**
55      * The name of property to set if extensions are available.
56      *
57      * @param property The name of property to set if extensions is available.
58      */

59     public void setProperty(final String JavaDoc property) {
60         this.propertyName = property;
61     }
62
63     /**
64      * The JAR library to check.
65      *
66      * @param file The jar library to check.
67      */

68     public void setFile(final File JavaDoc file) {
69         this.libraryFile = file;
70     }
71
72     /**
73      * Set the Extension looking for.
74      *
75      * @param extension Set the Extension looking for.
76      */

77     public void addConfiguredExtension(final ExtensionAdapter extension) {
78         if (null != requiredExtension) {
79             final String JavaDoc message = "Can not specify extension to "
80                 + "search for multiple times.";
81             throw new BuildException(message);
82         }
83         requiredExtension = extension;
84     }
85
86     /**
87      * Adds a set of extensions to search in.
88      *
89      * @param extensionSet a set of extensions to search in.
90      */

91     public void addConfiguredExtensionSet(final ExtensionSet extensionSet) {
92         extensionFileSets.addElement(extensionSet);
93     }
94
95     /**
96      * Execute the task.
97      *
98      * @throws BuildException if somethign goes wrong.
99      */

100     public void execute() throws BuildException {
101         validate();
102
103         final Extension test = requiredExtension.toExtension();
104
105         // Check if list of files to check has been specified
106
if (!extensionFileSets.isEmpty()) {
107             final Iterator JavaDoc iterator = extensionFileSets.iterator();
108             while (iterator.hasNext()) {
109                 final ExtensionSet extensionSet
110                     = (ExtensionSet) iterator.next();
111                 final Extension[] extensions =
112                     extensionSet.toExtensions(getProject());
113                 for (int i = 0; i < extensions.length; i++) {
114                     final Extension extension = extensions[ i ];
115                     if (extension.isCompatibleWith(test)) {
116                         getProject().setNewProperty(propertyName, "true");
117                     }
118                 }
119             }
120         } else {
121             final Manifest JavaDoc manifest = ExtensionUtil.getManifest(libraryFile);
122             final Extension[] extensions = Extension.getAvailable(manifest);
123             for (int i = 0; i < extensions.length; i++) {
124                 final Extension extension = extensions[ i ];
125                 if (extension.isCompatibleWith(test)) {
126                     getProject().setNewProperty(propertyName, "true");
127                 }
128             }
129         }
130     }
131
132     /**
133      * Validate the tasks parameters.
134      *
135      * @throws BuildException if invalid parameters found
136      */

137     private void validate() throws BuildException {
138         if (null == requiredExtension) {
139             final String JavaDoc message = "Extension element must be specified.";
140             throw new BuildException(message);
141         }
142
143         if (null == libraryFile && extensionFileSets.isEmpty()) {
144             final String JavaDoc message = "File attribute not specified.";
145             throw new BuildException(message);
146         }
147         if (null != libraryFile && !libraryFile.exists()) {
148             final String JavaDoc message = "File '" + libraryFile + "' does not exist.";
149             throw new BuildException(message);
150         }
151         if (null != libraryFile && !libraryFile.isFile()) {
152             final String JavaDoc message = "\'" + libraryFile + "\' is not a file.";
153             throw new BuildException(message);
154         }
155     }
156 }
157
Popular Tags