1 18 package org.apache.tools.ant.taskdefs.optional.extension; 19 20 import java.io.File ; 21 import java.util.Iterator ; 22 import java.util.Vector ; 23 import java.util.jar.Manifest ; 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.Task; 26 27 32 public class JarLibAvailableTask extends Task { 33 36 private File libraryFile; 37 38 42 private final Vector extensionFileSets = new Vector (); 43 44 47 private String propertyName; 48 49 52 private ExtensionAdapter requiredExtension; 53 54 59 public void setProperty(final String property) { 60 this.propertyName = property; 61 } 62 63 68 public void setFile(final File file) { 69 this.libraryFile = file; 70 } 71 72 77 public void addConfiguredExtension(final ExtensionAdapter extension) { 78 if (null != requiredExtension) { 79 final String message = "Can not specify extension to " 80 + "search for multiple times."; 81 throw new BuildException(message); 82 } 83 requiredExtension = extension; 84 } 85 86 91 public void addConfiguredExtensionSet(final ExtensionSet extensionSet) { 92 extensionFileSets.addElement(extensionSet); 93 } 94 95 100 public void execute() throws BuildException { 101 validate(); 102 103 final Extension test = requiredExtension.toExtension(); 104 105 if (!extensionFileSets.isEmpty()) { 107 final Iterator iterator = extensionFileSets.iterator(); 108 while (iterator.hasNext()) { 109 final ExtensionSet extensionSet 110 = (ExtensionSet) iterator.next(); 111 final Extension[] extensions = 112 extensionSet.toExtensions(getProject()); 113 for (int i = 0; i < extensions.length; i++) { 114 final Extension extension = extensions[ i ]; 115 if (extension.isCompatibleWith(test)) { 116 getProject().setNewProperty(propertyName, "true"); 117 } 118 } 119 } 120 } else { 121 final Manifest manifest = ExtensionUtil.getManifest(libraryFile); 122 final Extension[] extensions = Extension.getAvailable(manifest); 123 for (int i = 0; i < extensions.length; i++) { 124 final Extension extension = extensions[ i ]; 125 if (extension.isCompatibleWith(test)) { 126 getProject().setNewProperty(propertyName, "true"); 127 } 128 } 129 } 130 } 131 132 137 private void validate() throws BuildException { 138 if (null == requiredExtension) { 139 final String message = "Extension element must be specified."; 140 throw new BuildException(message); 141 } 142 143 if (null == libraryFile && extensionFileSets.isEmpty()) { 144 final String message = "File attribute not specified."; 145 throw new BuildException(message); 146 } 147 if (null != libraryFile && !libraryFile.exists()) { 148 final String message = "File '" + libraryFile + "' does not exist."; 149 throw new BuildException(message); 150 } 151 if (null != libraryFile && !libraryFile.isFile()) { 152 final String message = "\'" + libraryFile + "\' is not a file."; 153 throw new BuildException(message); 154 } 155 } 156 } 157 | Popular Tags |