1 18 package org.apache.tools.ant.taskdefs.optional.extension; 19 20 import java.io.File ; 21 import java.util.ArrayList ; 22 import java.util.jar.Manifest ; 23 import org.apache.tools.ant.BuildException; 24 import org.apache.tools.ant.Project; 25 import org.apache.tools.ant.Task; 26 import org.apache.tools.ant.taskdefs.optional.extension.resolvers.AntResolver; 27 import org.apache.tools.ant.taskdefs.optional.extension.resolvers.LocationResolver; 28 import org.apache.tools.ant.taskdefs.optional.extension.resolvers.URLResolver; 29 30 36 public class JarLibResolveTask extends Task { 37 41 private String propertyName; 42 43 46 private Extension requiredExtension; 47 48 51 private final ArrayList resolvers = new ArrayList (); 52 53 59 private boolean checkExtension = true; 60 61 66 private boolean failOnError = true; 67 68 75 public void setProperty(final String property) { 76 this.propertyName = property; 77 } 78 79 85 public void setCheckExtension(final boolean checkExtension) { 86 this.checkExtension = checkExtension; 87 } 88 89 94 public void setFailOnError(final boolean failOnError) { 95 this.failOnError = failOnError; 96 } 97 98 104 public void addConfiguredLocation(final LocationResolver loc) { 105 resolvers.add(loc); 106 } 107 108 114 public void addConfiguredUrl(final URLResolver url) { 115 resolvers.add(url); 116 } 117 118 123 public void addConfiguredAnt(final AntResolver ant) { 124 resolvers.add(ant); 125 } 126 127 132 public void addConfiguredExtension(final ExtensionAdapter extension) { 133 if (null != requiredExtension) { 134 final String message = "Can not specify extension to " 135 + "resolve multiple times."; 136 throw new BuildException(message); 137 } 138 requiredExtension = extension.toExtension(); 139 } 140 141 146 public void execute() throws BuildException { 147 validate(); 148 149 getProject().log("Resolving extension: " + requiredExtension, 150 Project.MSG_VERBOSE); 151 152 String candidate = 153 getProject().getProperty(propertyName); 154 155 if (null != candidate) { 156 final String message = "Property Already set to: " + candidate; 157 if (failOnError) { 158 throw new BuildException(message); 159 } 160 getProject().log(message, Project.MSG_ERR); 161 return; 162 } 163 164 final int size = resolvers.size(); 165 for (int i = 0; i < size; i++) { 166 final ExtensionResolver resolver = 167 (ExtensionResolver) resolvers.get(i); 168 169 getProject().log("Searching for extension using Resolver:" + resolver, 170 Project.MSG_VERBOSE); 171 172 try { 173 final File file = 174 resolver.resolve(requiredExtension, getProject()); 175 try { 176 checkExtension(file); 177 return; 178 } catch (final BuildException be) { 179 final String message = "File " + file + " returned by " 180 + "resolver failed to satisfy extension due to: " 181 + be.getMessage(); 182 getProject().log(message, Project.MSG_WARN); 183 } 184 } catch (final BuildException be) { 185 final String message = "Failed to resolve extension to file " 186 + "using resolver " + resolver + " due to: " + be; 187 getProject().log(message, Project.MSG_WARN); 188 } 189 } 190 191 missingExtension(); 192 } 193 194 199 private void missingExtension() { 200 final String message = 201 "Unable to resolve extension to a file"; 202 if (failOnError) { 203 throw new BuildException(message); 204 } 205 getProject().log(message, Project.MSG_ERR); 206 } 207 208 216 private void checkExtension(final File file) { 217 if (!file.exists()) { 218 final String message = 219 "File " + file + " does not exist"; 220 throw new BuildException(message); 221 } 222 if (!file.isFile()) { 223 final String message = 224 "File " + file + " is not a file"; 225 throw new BuildException(message); 226 } 227 228 if (!checkExtension) { 229 final String message = "Setting property to " + file 230 + " without verifying library satisfies extension"; 231 getProject().log(message, Project.MSG_VERBOSE); 232 setLibraryProperty(file); 233 } else { 234 getProject().log("Checking file " + file 235 + " to see if it satisfies extension", Project.MSG_VERBOSE); 236 final Manifest manifest = 237 ExtensionUtil.getManifest(file); 238 final Extension[] extensions = 239 Extension.getAvailable(manifest); 240 for (int i = 0; i < extensions.length; i++) { 241 final Extension extension = extensions[ i ]; 242 if (extension.isCompatibleWith(requiredExtension)) { 243 setLibraryProperty(file); 244 return; 245 } 246 } 247 248 getProject().log("File " + file + " skipped as it " 249 + "does not satisfy extension", Project.MSG_VERBOSE); 250 251 final String message = 252 "File " + file + " does not satisfy extension"; 253 throw new BuildException(message); 254 } 255 } 256 257 264 private void setLibraryProperty(final File file) { 265 getProject().setNewProperty(propertyName, 266 file.getAbsolutePath()); 267 } 268 269 274 private void validate() throws BuildException { 275 if (null == propertyName) { 276 final String message = "Property attribute must be specified."; 277 throw new BuildException(message); 278 } 279 280 if (null == requiredExtension) { 281 final String message = "Extension element must be specified."; 282 throw new BuildException(message); 283 } 284 } 285 } 286 | Popular Tags |