1 18 package org.apache.tools.ant.taskdefs.optional.extension; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.jar.JarFile ; 26 import java.util.jar.Manifest ; 27 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.DirectoryScanner; 30 import org.apache.tools.ant.Project; 31 import org.apache.tools.ant.types.FileSet; 32 33 37 public class ExtensionUtil { 38 41 private ExtensionUtil() { 42 } 44 45 51 static ArrayList toExtensions(final List adapters) 52 throws BuildException { 53 final ArrayList results = new ArrayList (); 54 55 final int size = adapters.size(); 56 for (int i = 0; i < size; i++) { 57 final ExtensionAdapter adapter = 58 (ExtensionAdapter) adapters.get(i); 59 final Extension extension = adapter.toExtension(); 60 results.add(extension); 61 } 62 63 return results; 64 } 65 66 73 static void extractExtensions(final Project project, 74 final List libraries, 75 final List fileset) 76 throws BuildException { 77 if (!fileset.isEmpty()) { 78 final Extension[] extensions = getExtensions(project, 79 fileset); 80 for (int i = 0; i < extensions.length; i++) { 81 libraries.add(extensions[ i ]); 82 } 83 } 84 } 85 86 93 private static Extension[] getExtensions(final Project project, 94 final List libraries) 95 throws BuildException { 96 final ArrayList extensions = new ArrayList (); 97 final Iterator iterator = libraries.iterator(); 98 while (iterator.hasNext()) { 99 final FileSet fileSet = (FileSet) iterator.next(); 100 101 boolean includeImpl = true; 102 boolean includeURL = true; 103 104 if (fileSet instanceof LibFileSet) { 105 LibFileSet libFileSet = (LibFileSet) fileSet; 106 includeImpl = libFileSet.isIncludeImpl(); 107 includeURL = libFileSet.isIncludeURL(); 108 } 109 110 final DirectoryScanner scanner = fileSet.getDirectoryScanner(project); 111 final File basedir = scanner.getBasedir(); 112 final String [] files = scanner.getIncludedFiles(); 113 for (int i = 0; i < files.length; i++) { 114 final File file = new File (basedir, files[ i ]); 115 loadExtensions(file, extensions, includeImpl, includeURL); 116 } 117 } 118 return (Extension[]) extensions.toArray(new Extension[extensions.size()]); 119 } 120 121 128 private static void loadExtensions(final File file, 129 final List extensionList, 130 final boolean includeImpl, 131 final boolean includeURL) 132 throws BuildException { 133 try { 134 final JarFile jarFile = new JarFile (file); 135 final Extension[] extensions = 136 Extension.getAvailable(jarFile.getManifest()); 137 for (int i = 0; i < extensions.length; i++) { 138 final Extension extension = extensions[ i ]; 139 addExtension(extensionList, extension, includeImpl, includeURL); 140 } 141 } catch (final Exception e) { 142 throw new BuildException(e.getMessage(), e); 143 } 144 } 145 146 157 private static void addExtension(final List extensionList, 158 final Extension originalExtension, 159 final boolean includeImpl, 160 final boolean includeURL) { 161 Extension extension = originalExtension; 162 if (!includeURL 163 && null != extension.getImplementationURL()) { 164 extension = 165 new Extension(extension.getExtensionName(), 166 extension.getSpecificationVersion().toString(), 167 extension.getSpecificationVendor(), 168 extension.getImplementationVersion().toString(), 169 extension.getImplementationVendor(), 170 extension.getImplementationVendorID(), 171 null); 172 } 173 174 final boolean hasImplAttributes = 175 null != extension.getImplementationURL() 176 || null != extension.getImplementationVersion() 177 || null != extension.getImplementationVendorID() 178 || null != extension.getImplementationVendor(); 179 180 if (!includeImpl && hasImplAttributes) { 181 extension = 182 new Extension(extension.getExtensionName(), 183 extension.getSpecificationVersion().toString(), 184 extension.getSpecificationVendor(), 185 null, 186 null, 187 null, 188 extension.getImplementationURL()); 189 } 190 191 extensionList.add(extension); 192 } 193 194 202 static Manifest getManifest(final File file) 203 throws BuildException { 204 try { 205 final JarFile jarFile = new JarFile (file); 206 Manifest m = jarFile.getManifest(); 207 if (m == null) { 208 throw new BuildException(file + " doesn't have a MANIFEST"); 209 } 210 return m; 211 } catch (final IOException ioe) { 212 throw new BuildException(ioe.getMessage(), ioe); 213 } 214 } 215 } 216 | Popular Tags |