1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 import java.util.Enumeration ; 23 import java.util.Vector ; 24 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.Task; 27 import org.apache.tools.ant.types.Environment; 28 import org.apache.tools.ant.types.FileSet; 29 import org.apache.tools.ant.types.Path; 30 import org.apache.tools.ant.types.RedirectorElement; 31 import org.apache.tools.ant.util.JavaEnvUtils; 32 33 37 38 public abstract class AbstractJarSignerTask extends Task { 39 43 protected File jar; 44 47 protected String alias; 48 51 protected String keystore; 52 55 protected String storepass; 56 59 protected String storetype; 60 63 protected String keypass; 64 67 protected boolean verbose; 68 71 protected String maxMemory; 72 75 protected Vector filesets = new Vector (); 76 79 protected static final String JARSIGNER_COMMAND = "jarsigner"; 80 81 83 86 private RedirectorElement redirector; 87 88 91 private Environment sysProperties = new Environment(); 92 93 96 public static final String ERROR_NO_SOURCE = "jar must be set through jar attribute " 97 + "or nested filesets"; 98 99 104 private Path path = null; 105 106 112 public void setMaxmemory(String max) { 113 maxMemory = max; 114 } 115 116 121 public void setJar(final File jar) { 122 this.jar = jar; 123 } 124 125 130 public void setAlias(final String alias) { 131 this.alias = alias; 132 } 133 134 139 public void setKeystore(final String keystore) { 140 this.keystore = keystore; 141 } 142 143 148 public void setStorepass(final String storepass) { 149 this.storepass = storepass; 150 } 151 152 157 public void setStoretype(final String storetype) { 158 this.storetype = storetype; 159 } 160 161 166 public void setKeypass(final String keypass) { 167 this.keypass = keypass; 168 } 169 170 175 public void setVerbose(final boolean verbose) { 176 this.verbose = verbose; 177 } 178 179 185 public void addFileset(final FileSet set) { 186 filesets.addElement(set); 187 } 188 189 194 public void addSysproperty(Environment.Variable sysp) { 195 sysProperties.addVariable(sysp); 196 } 197 198 204 public Path createPath() { 205 if (path == null) { 206 path = new Path(getProject()); 207 } 208 return path.createPath(); 209 } 210 211 214 protected void beginExecution() { 215 216 redirector = createRedirector(); 217 } 218 219 222 protected void endExecution() { 223 redirector = null; 224 } 225 226 231 private RedirectorElement createRedirector() { 232 RedirectorElement result = new RedirectorElement(); 233 if (storepass != null) { 234 StringBuffer input = new StringBuffer (storepass).append('\n'); 235 if (keypass != null) { 236 input.append(keypass).append('\n'); 237 } 238 result.setInputString(input.toString()); 239 result.setLogInputString(false); 240 } 241 return result; 242 } 243 244 249 public RedirectorElement getRedirector() { 250 return redirector; 251 } 252 253 257 protected void setCommonOptions(final ExecTask cmd) { 258 if (maxMemory != null) { 259 addValue(cmd, "-J-Xmx" + maxMemory); 260 } 261 262 if (verbose) { 263 addValue(cmd, "-verbose"); 264 } 265 266 Vector props = sysProperties.getVariablesVector(); 268 Enumeration e = props.elements(); 269 while (e.hasMoreElements()) { 270 Environment.Variable variable = (Environment.Variable) e.nextElement(); 271 declareSysProperty(cmd, variable); 272 } 273 } 274 275 281 protected void declareSysProperty( 282 ExecTask cmd, Environment.Variable property) throws BuildException { 283 addValue(cmd, "-J-D" + property.getContent()); 284 } 285 286 287 291 protected void bindToKeystore(final ExecTask cmd) { 292 if (null != keystore) { 293 addValue(cmd, "-keystore"); 295 String loc; 296 File keystoreFile = getProject().resolveFile(keystore); 297 if (keystoreFile.exists()) { 298 loc = keystoreFile.getPath(); 299 } else { 300 loc = keystore; 302 } 303 addValue(cmd, loc); 304 } 305 if (null != storetype) { 306 addValue(cmd, "-storetype"); 307 addValue(cmd, storetype); 308 } 309 } 310 311 316 protected ExecTask createJarSigner() { 317 final ExecTask cmd = new ExecTask(this); 318 cmd.setExecutable(JavaEnvUtils.getJdkExecutable(JARSIGNER_COMMAND)); 319 cmd.setTaskType(JARSIGNER_COMMAND); 320 cmd.setFailonerror(true); 321 cmd.addConfiguredRedirector(redirector); 322 return cmd; 323 } 324 325 330 protected Vector createUnifiedSources() { 331 Vector sources = (Vector ) filesets.clone(); 332 if (jar != null) { 333 FileSet sourceJar = new FileSet(); 337 sourceJar.setProject(getProject()); 338 sourceJar.setFile(jar); 339 sourceJar.setDir(jar.getParentFile()); 340 sources.add(sourceJar); 341 } 342 return sources; 343 } 344 345 352 protected Path createUnifiedSourcePath() { 353 Path p = path == null ? new Path(getProject()) : (Path) path.clone(); 354 Vector s = createUnifiedSources(); 355 Enumeration e = s.elements(); 356 while (e.hasMoreElements()) { 357 p.add((FileSet) e.nextElement()); 358 } 359 return p; 360 } 361 362 367 protected boolean hasResources() { 368 return path != null || filesets.size() > 0; 369 } 370 371 376 protected void addValue(final ExecTask cmd, String value) { 377 cmd.createArg().setValue(value); 378 } 379 } 380 | Popular Tags |