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.io.IOException;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.jar.JarFile;
026: import java.util.jar.Manifest;
027:
028: import org.apache.tools.ant.BuildException;
029: import org.apache.tools.ant.DirectoryScanner;
030: import org.apache.tools.ant.Project;
031: import org.apache.tools.ant.types.FileSet;
032:
033: /**
034: * A set of useful methods relating to extensions.
035: *
036: */
037: public class ExtensionUtil {
038: /**
039: * Class is not meant to be instantiated.
040: */
041: private ExtensionUtil() {
042: //all methods static
043: }
044:
045: /**
046: * Convert a list of extensionAdapter objects to extensions.
047: *
048: * @param adapters the list of ExtensionAdapterss to add to convert
049: * @throws BuildException if an error occurs
050: */
051: static ArrayList toExtensions(final List adapters)
052: throws BuildException {
053: final ArrayList results = new ArrayList();
054:
055: final int size = adapters.size();
056: for (int i = 0; i < size; i++) {
057: final ExtensionAdapter adapter = (ExtensionAdapter) adapters
058: .get(i);
059: final Extension extension = adapter.toExtension();
060: results.add(extension);
061: }
062:
063: return results;
064: }
065:
066: /**
067: * Generate a list of extensions from a specified fileset.
068: *
069: * @param libraries the list to add extensions to
070: * @param fileset the filesets containing librarys
071: * @throws BuildException if an error occurs
072: */
073: static void extractExtensions(final Project project,
074: final List libraries, final List fileset)
075: throws BuildException {
076: if (!fileset.isEmpty()) {
077: final Extension[] extensions = getExtensions(project,
078: fileset);
079: for (int i = 0; i < extensions.length; i++) {
080: libraries.add(extensions[i]);
081: }
082: }
083: }
084:
085: /**
086: * Retrieve extensions from the specified libraries.
087: *
088: * @param libraries the filesets for libraries
089: * @return the extensions contained in libraries
090: * @throws BuildException if failing to scan libraries
091: */
092: private static Extension[] getExtensions(final Project project,
093: final List libraries) throws BuildException {
094: final ArrayList extensions = new ArrayList();
095: final Iterator iterator = libraries.iterator();
096: while (iterator.hasNext()) {
097: final FileSet fileSet = (FileSet) iterator.next();
098:
099: boolean includeImpl = true;
100: boolean includeURL = true;
101:
102: if (fileSet instanceof LibFileSet) {
103: LibFileSet libFileSet = (LibFileSet) fileSet;
104: includeImpl = libFileSet.isIncludeImpl();
105: includeURL = libFileSet.isIncludeURL();
106: }
107:
108: final DirectoryScanner scanner = fileSet
109: .getDirectoryScanner(project);
110: final File basedir = scanner.getBasedir();
111: final String[] files = scanner.getIncludedFiles();
112: for (int i = 0; i < files.length; i++) {
113: final File file = new File(basedir, files[i]);
114: loadExtensions(file, extensions, includeImpl,
115: includeURL);
116: }
117: }
118: return (Extension[]) extensions
119: .toArray(new Extension[extensions.size()]);
120: }
121:
122: /**
123: * Load list of available extensions from specified file.
124: *
125: * @param file the file
126: * @param extensionList the list to add available extensions to
127: * @throws BuildException if there is an error
128: */
129: private static void loadExtensions(final File file,
130: final List extensionList, final boolean includeImpl,
131: final boolean includeURL) throws BuildException {
132: try {
133: final JarFile jarFile = new JarFile(file);
134: final Extension[] extensions = Extension
135: .getAvailable(jarFile.getManifest());
136: for (int i = 0; i < extensions.length; i++) {
137: final Extension extension = extensions[i];
138: addExtension(extensionList, extension, includeImpl,
139: includeURL);
140: }
141: } catch (final Exception e) {
142: throw new BuildException(e.getMessage(), e);
143: }
144: }
145:
146: /**
147: * Add extension to list.
148: * If extension should not have implementation details but
149: * does strip them. If extension should not have url but does
150: * then strip it.
151: *
152: * @param extensionList the list of extensions to add to
153: * @param originalExtension the extension
154: * @param includeImpl false to exclude implementation details
155: * @param includeURL false to exclude implementation URL
156: */
157: private static void addExtension(final List extensionList,
158: final Extension originalExtension,
159: final boolean includeImpl, final boolean includeURL) {
160: Extension extension = originalExtension;
161: if (!includeURL && null != extension.getImplementationURL()) {
162: extension = new Extension(extension.getExtensionName(),
163: extension.getSpecificationVersion().toString(),
164: extension.getSpecificationVendor(), extension
165: .getImplementationVersion().toString(),
166: extension.getImplementationVendor(), extension
167: .getImplementationVendorID(), null);
168: }
169:
170: final boolean hasImplAttributes = null != extension
171: .getImplementationURL()
172: || null != extension.getImplementationVersion()
173: || null != extension.getImplementationVendorID()
174: || null != extension.getImplementationVendor();
175:
176: if (!includeImpl && hasImplAttributes) {
177: extension = new Extension(extension.getExtensionName(),
178: extension.getSpecificationVersion().toString(),
179: extension.getSpecificationVendor(), null, null,
180: null, extension.getImplementationURL());
181: }
182:
183: extensionList.add(extension);
184: }
185:
186: /**
187: * Retrieve manifest for specified file.
188: *
189: * @param file the file
190: * @return the manifest
191: * @throws BuildException if errror occurs (file doesn't exist,
192: * file not a jar, manifest doesn't exist in file)
193: */
194: static Manifest getManifest(final File file) throws BuildException {
195: try {
196: final JarFile jarFile = new JarFile(file);
197: Manifest m = jarFile.getManifest();
198: if (m == null) {
199: throw new BuildException(file
200: + " doesn't have a MANIFEST");
201: }
202: return m;
203: } catch (final IOException ioe) {
204: throw new BuildException(ioe.getMessage(), ioe);
205: }
206: }
207: }
|