001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.extension;
019:
020: import java.io.File;
021: import java.util.Iterator;
022: import java.util.Vector;
023: import java.util.jar.Manifest;
024: import org.apache.tools.ant.BuildException;
025: import org.apache.tools.ant.Task;
026:
027: /**
028: * Checks whether an extension is present in a fileset or an extensionSet.
029: *
030: * @ant.task name="jarlib-available"
031: */
032: public class JarLibAvailableTask extends Task {
033: /**
034: * The library to display information about.
035: */
036: private File libraryFile;
037:
038: /**
039: * Filesets specifying all the librarys
040: * to display information about.
041: */
042: private final Vector extensionFileSets = new Vector();
043:
044: /**
045: * The name of the property to set if extension is available.
046: */
047: private String propertyName;
048:
049: /**
050: * The extension that is required.
051: */
052: private ExtensionAdapter requiredExtension;
053:
054: /**
055: * The name of property to set if extensions are available.
056: *
057: * @param property The name of property to set if extensions is available.
058: */
059: public void setProperty(final String property) {
060: this .propertyName = property;
061: }
062:
063: /**
064: * The JAR library to check.
065: *
066: * @param file The jar library to check.
067: */
068: public void setFile(final File file) {
069: this .libraryFile = file;
070: }
071:
072: /**
073: * Set the Extension looking for.
074: *
075: * @param extension Set the Extension looking for.
076: */
077: public void addConfiguredExtension(final ExtensionAdapter extension) {
078: if (null != requiredExtension) {
079: final String message = "Can not specify extension to "
080: + "search for multiple times.";
081: throw new BuildException(message);
082: }
083: requiredExtension = extension;
084: }
085:
086: /**
087: * Adds a set of extensions to search in.
088: *
089: * @param extensionSet a set of extensions to search in.
090: */
091: public void addConfiguredExtensionSet(
092: final ExtensionSet extensionSet) {
093: extensionFileSets.addElement(extensionSet);
094: }
095:
096: /**
097: * Execute the task.
098: *
099: * @throws BuildException if somethign goes wrong.
100: */
101: public void execute() throws BuildException {
102: validate();
103:
104: final Extension test = requiredExtension.toExtension();
105:
106: // Check if list of files to check has been specified
107: if (!extensionFileSets.isEmpty()) {
108: final Iterator iterator = extensionFileSets.iterator();
109: while (iterator.hasNext()) {
110: final ExtensionSet extensionSet = (ExtensionSet) iterator
111: .next();
112: final Extension[] extensions = extensionSet
113: .toExtensions(getProject());
114: for (int i = 0; i < extensions.length; i++) {
115: final Extension extension = extensions[i];
116: if (extension.isCompatibleWith(test)) {
117: getProject().setNewProperty(propertyName,
118: "true");
119: }
120: }
121: }
122: } else {
123: final Manifest manifest = ExtensionUtil
124: .getManifest(libraryFile);
125: final Extension[] extensions = Extension
126: .getAvailable(manifest);
127: for (int i = 0; i < extensions.length; i++) {
128: final Extension extension = extensions[i];
129: if (extension.isCompatibleWith(test)) {
130: getProject().setNewProperty(propertyName, "true");
131: }
132: }
133: }
134: }
135:
136: /**
137: * Validate the tasks parameters.
138: *
139: * @throws BuildException if invalid parameters found
140: */
141: private void validate() throws BuildException {
142: if (null == requiredExtension) {
143: final String message = "Extension element must be specified.";
144: throw new BuildException(message);
145: }
146:
147: if (null == libraryFile && extensionFileSets.isEmpty()) {
148: final String message = "File attribute not specified.";
149: throw new BuildException(message);
150: }
151: if (null != libraryFile && !libraryFile.exists()) {
152: final String message = "File '" + libraryFile
153: + "' does not exist.";
154: throw new BuildException(message);
155: }
156: if (null != libraryFile && !libraryFile.isFile()) {
157: final String message = "\'" + libraryFile
158: + "\' is not a file.";
159: throw new BuildException(message);
160: }
161: }
162: }
|