source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/extension/JarLibAvailableTask.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 5.1 KB
Line 
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 */
17package org.apache.tools.ant.taskdefs.optional.extension;
18
19import java.io.File;
20import java.util.Iterator;
21import java.util.Vector;
22import java.util.jar.Manifest;
23import org.apache.tools.ant.BuildException;
24import org.apache.tools.ant.Task;
25
26/**
27 * Checks whether an extension is present in a fileset or an extensionSet.
28 *
29 * @ant.task name="jarlib-available"
30 */
31public class JarLibAvailableTask extends Task {
32 /**
33 * The library to display information about.
34 */
35 private File libraryFile;
36
37 /**
38 * Filesets specifying all the librarys
39 * to display information about.
40 */
41 private final Vector extensionFileSets = new Vector();
42
43 /**
44 * The name of the property to set if extension is available.
45 */
46 private String propertyName;
47
48 /**
49 * The extension that is required.
50 */
51 private ExtensionAdapter requiredExtension;
52
53 /**
54 * The name of property to set if extensions are available.
55 *
56 * @param property The name of property to set if extensions is available.
57 */
58 public void setProperty(final String property) {
59 this.propertyName = property;
60 }
61
62 /**
63 * The JAR library to check.
64 *
65 * @param file The jar library to check.
66 */
67 public void setFile(final File file) {
68 this.libraryFile = file;
69 }
70
71 /**
72 * Set the Extension looking for.
73 *
74 * @param extension Set the Extension looking for.
75 */
76 public void addConfiguredExtension(final ExtensionAdapter extension) {
77 if (null != requiredExtension) {
78 final String message = "Can not specify extension to "
79 + "search for multiple times.";
80 throw new BuildException(message);
81 }
82 requiredExtension = extension;
83 }
84
85 /**
86 * Adds a set of extensions to search in.
87 *
88 * @param extensionSet a set of extensions to search in.
89 */
90 public void addConfiguredExtensionSet(final ExtensionSet extensionSet) {
91 extensionFileSets.addElement(extensionSet);
92 }
93
94 /**
95 * Execute the task.
96 *
97 * @throws BuildException if somethign goes wrong.
98 */
99 public void execute() throws BuildException {
100 validate();
101
102 final Extension test = requiredExtension.toExtension();
103
104 // Check if list of files to check has been specified
105 if (!extensionFileSets.isEmpty()) {
106 final Iterator iterator = extensionFileSets.iterator();
107 while (iterator.hasNext()) {
108 final ExtensionSet extensionSet
109 = (ExtensionSet) iterator.next();
110 final Extension[] extensions =
111 extensionSet.toExtensions(getProject());
112 for (int i = 0; i < extensions.length; i++) {
113 final Extension extension = extensions[ i ];
114 if (extension.isCompatibleWith(test)) {
115 getProject().setNewProperty(propertyName, "true");
116 }
117 }
118 }
119 } else {
120 final Manifest manifest = ExtensionUtil.getManifest(libraryFile);
121 final Extension[] extensions = Extension.getAvailable(manifest);
122 for (int i = 0; i < extensions.length; i++) {
123 final Extension extension = extensions[ i ];
124 if (extension.isCompatibleWith(test)) {
125 getProject().setNewProperty(propertyName, "true");
126 }
127 }
128 }
129 }
130
131 /**
132 * Validate the tasks parameters.
133 *
134 * @throws BuildException if invalid parameters found
135 */
136 private void validate() throws BuildException {
137 if (null == requiredExtension) {
138 final String message = "Extension element must be specified.";
139 throw new BuildException(message);
140 }
141
142 if (null == libraryFile && extensionFileSets.isEmpty()) {
143 final String message = "File attribute not specified.";
144 throw new BuildException(message);
145 }
146 if (null != libraryFile && !libraryFile.exists()) {
147 final String message = "File '" + libraryFile + "' does not exist.";
148 throw new BuildException(message);
149 }
150 if (null != libraryFile && !libraryFile.isFile()) {
151 final String message = "\'" + libraryFile + "\' is not a file.";
152 throw new BuildException(message);
153 }
154 }
155}
Note: See TracBrowser for help on using the repository browser.