[14627] | 1 | /*
|
---|
| 2 | * Copyright 2002-2005 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 | */
|
---|
| 17 | package org.apache.tools.ant.taskdefs.optional.extension;
|
---|
| 18 |
|
---|
| 19 | import java.io.File;
|
---|
| 20 | import java.io.IOException;
|
---|
| 21 | import java.util.ArrayList;
|
---|
| 22 | import java.util.Iterator;
|
---|
| 23 | import java.util.jar.JarFile;
|
---|
| 24 | import java.util.jar.Manifest;
|
---|
| 25 | import org.apache.tools.ant.BuildException;
|
---|
| 26 | import org.apache.tools.ant.DirectoryScanner;
|
---|
| 27 | import org.apache.tools.ant.Project;
|
---|
| 28 | import org.apache.tools.ant.types.FileSet;
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * A set of useful methods relating to extensions.
|
---|
| 32 | *
|
---|
| 33 | */
|
---|
| 34 | public class ExtensionUtil {
|
---|
| 35 | /**
|
---|
| 36 | * Class is not meant to be instantiated.
|
---|
| 37 | */
|
---|
| 38 | private ExtensionUtil() {
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * Convert a list of extensionAdapter objects to extensions.
|
---|
| 43 | *
|
---|
| 44 | * @param adapters the list of ExtensionAdapterss to add to convert
|
---|
| 45 | * @throws BuildException if an error occurs
|
---|
| 46 | */
|
---|
| 47 | static ArrayList toExtensions(final ArrayList adapters)
|
---|
| 48 | throws BuildException {
|
---|
| 49 | final ArrayList results = new ArrayList();
|
---|
| 50 |
|
---|
| 51 | final int size = adapters.size();
|
---|
| 52 | for (int i = 0; i < size; i++) {
|
---|
| 53 | final ExtensionAdapter adapter =
|
---|
| 54 | (ExtensionAdapter) adapters.get(i);
|
---|
| 55 | final Extension extension = adapter.toExtension();
|
---|
| 56 | results.add(extension);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | return results;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * Generate a list of extensions from a specified fileset.
|
---|
| 64 | *
|
---|
| 65 | * @param librarys the list to add extensions to
|
---|
| 66 | * @param fileset the filesets containing librarys
|
---|
| 67 | * @throws BuildException if an error occurs
|
---|
| 68 | */
|
---|
| 69 | static void extractExtensions(final Project project,
|
---|
| 70 | final ArrayList librarys,
|
---|
| 71 | final ArrayList fileset)
|
---|
| 72 | throws BuildException {
|
---|
| 73 | if (!fileset.isEmpty()) {
|
---|
| 74 | final Extension[] extensions = getExtensions(project,
|
---|
| 75 | fileset);
|
---|
| 76 | for (int i = 0; i < extensions.length; i++) {
|
---|
| 77 | librarys.add(extensions[ i ]);
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
| 83 | * Retrieve extensions from the specified librarys.
|
---|
| 84 | *
|
---|
| 85 | * @param librarys the filesets for librarys
|
---|
| 86 | * @return the extensions contained in librarys
|
---|
| 87 | * @throws BuildException if failing to scan librarys
|
---|
| 88 | */
|
---|
| 89 | private static Extension[] getExtensions(final Project project,
|
---|
| 90 | final ArrayList librarys)
|
---|
| 91 | throws BuildException {
|
---|
| 92 | final ArrayList extensions = new ArrayList();
|
---|
| 93 | final Iterator iterator = librarys.iterator();
|
---|
| 94 | while (iterator.hasNext()) {
|
---|
| 95 | final FileSet fileSet = (FileSet) iterator.next();
|
---|
| 96 |
|
---|
| 97 | boolean includeImpl = true;
|
---|
| 98 | boolean includeURL = true;
|
---|
| 99 |
|
---|
| 100 | if (fileSet instanceof LibFileSet) {
|
---|
| 101 | LibFileSet libFileSet = (LibFileSet) fileSet;
|
---|
| 102 | includeImpl = libFileSet.isIncludeImpl();
|
---|
| 103 | includeURL = libFileSet.isIncludeURL();
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | final DirectoryScanner scanner = fileSet.getDirectoryScanner(project);
|
---|
| 107 | final File basedir = scanner.getBasedir();
|
---|
| 108 | final String[] files = scanner.getIncludedFiles();
|
---|
| 109 | for (int i = 0; i < files.length; i++) {
|
---|
| 110 | final File file = new File(basedir, files[ i ]);
|
---|
| 111 | loadExtensions(file, extensions, includeImpl, includeURL);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | return (Extension[]) extensions.toArray(new Extension[extensions.size()]);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /**
|
---|
| 118 | * Load list of available extensions from specified file.
|
---|
| 119 | *
|
---|
| 120 | * @param file the file
|
---|
| 121 | * @param extensionList the list to add available extensions to
|
---|
| 122 | * @throws BuildException if there is an error
|
---|
| 123 | */
|
---|
| 124 | private static void loadExtensions(final File file,
|
---|
| 125 | final ArrayList extensionList,
|
---|
| 126 | final boolean includeImpl,
|
---|
| 127 | final boolean includeURL)
|
---|
| 128 | throws BuildException {
|
---|
| 129 | try {
|
---|
| 130 | final JarFile jarFile = new JarFile(file);
|
---|
| 131 | final Extension[] extensions =
|
---|
| 132 | Extension.getAvailable(jarFile.getManifest());
|
---|
| 133 | for (int i = 0; i < extensions.length; i++) {
|
---|
| 134 | final Extension extension = extensions[ i ];
|
---|
| 135 | addExtension(extensionList, extension, includeImpl, includeURL);
|
---|
| 136 | }
|
---|
| 137 | } catch (final Exception e) {
|
---|
| 138 | throw new BuildException(e.getMessage(), e);
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /**
|
---|
| 143 | * Add extension to list.
|
---|
| 144 | * If extension should not have implementation details but
|
---|
| 145 | * does strip them. If extension should not have url but does
|
---|
| 146 | * then strip it.
|
---|
| 147 | *
|
---|
| 148 | * @param extensionList the list of extensions to add to
|
---|
| 149 | * @param originalExtension the extension
|
---|
| 150 | * @param includeImpl false to exclude implementation details
|
---|
| 151 | * @param includeURL false to exclude implementation URL
|
---|
| 152 | */
|
---|
| 153 | private static void addExtension(final ArrayList extensionList,
|
---|
| 154 | final Extension originalExtension,
|
---|
| 155 | final boolean includeImpl,
|
---|
| 156 | final boolean includeURL) {
|
---|
| 157 | Extension extension = originalExtension;
|
---|
| 158 | if (!includeURL
|
---|
| 159 | && null != extension.getImplementationURL()) {
|
---|
| 160 | extension =
|
---|
| 161 | new Extension(extension.getExtensionName(),
|
---|
| 162 | extension.getSpecificationVersion().toString(),
|
---|
| 163 | extension.getSpecificationVendor(),
|
---|
| 164 | extension.getImplementationVersion().toString(),
|
---|
| 165 | extension.getImplementationVendor(),
|
---|
| 166 | extension.getImplementationVendorID(),
|
---|
| 167 | null);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | final boolean hasImplAttributes =
|
---|
| 171 | null != extension.getImplementationURL()
|
---|
| 172 | || null != extension.getImplementationVersion()
|
---|
| 173 | || null != extension.getImplementationVendorID()
|
---|
| 174 | || null != extension.getImplementationVendor();
|
---|
| 175 |
|
---|
| 176 | if (!includeImpl && hasImplAttributes) {
|
---|
| 177 | extension =
|
---|
| 178 | new Extension(extension.getExtensionName(),
|
---|
| 179 | extension.getSpecificationVersion().toString(),
|
---|
| 180 | extension.getSpecificationVendor(),
|
---|
| 181 | null,
|
---|
| 182 | null,
|
---|
| 183 | null,
|
---|
| 184 | extension.getImplementationURL());
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | extensionList.add(extension);
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | /**
|
---|
| 191 | * retrieve manifest for specified file.
|
---|
| 192 | *
|
---|
| 193 | * @param file the file
|
---|
| 194 | * @return the manifest
|
---|
| 195 | * @throws BuildException if errror occurs (file not exist,
|
---|
| 196 | * file not a jar, manifest not exist in file)
|
---|
| 197 | */
|
---|
| 198 | static Manifest getManifest(final File file)
|
---|
| 199 | throws BuildException {
|
---|
| 200 | try {
|
---|
| 201 | final JarFile jarFile = new JarFile(file);
|
---|
| 202 | Manifest m = jarFile.getManifest();
|
---|
| 203 | if (m == null) {
|
---|
| 204 | throw new BuildException(file + " doesn't have a MANIFEST");
|
---|
| 205 | }
|
---|
| 206 | return m;
|
---|
| 207 | } catch (final IOException ioe) {
|
---|
| 208 | throw new BuildException(ioe.getMessage(), ioe);
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 | }
|
---|