1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.Iterator ; 24 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.Project; 27 import org.apache.tools.ant.taskdefs.condition.IsSigned; 28 import org.apache.tools.ant.types.Path; 29 import org.apache.tools.ant.types.resources.FileResource; 30 import org.apache.tools.ant.util.FileUtils; 31 import org.apache.tools.ant.util.IdentityMapper; 32 import org.apache.tools.ant.util.FileNameMapper; 33 34 48 public class SignJar extends AbstractJarSignerTask { 49 51 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 52 53 56 protected String sigfile; 57 58 61 protected File signedjar; 62 63 66 protected boolean internalsf; 67 68 71 protected boolean sectionsonly; 72 73 76 private boolean preserveLastModified; 77 78 82 protected boolean lazy; 83 84 87 protected File destDir; 88 89 92 private FileNameMapper mapper; 93 94 97 protected String tsaurl; 98 99 102 protected String tsacert; 103 104 107 public static final String ERROR_TODIR_AND_SIGNEDJAR 108 = "'destdir' and 'signedjar' cannot both be set"; 109 112 public static final String ERROR_TOO_MANY_MAPPERS = "Too many mappers"; 113 116 public static final String ERROR_SIGNEDJAR_AND_PATHS 117 = "You cannot specify the signed JAR when using paths or filesets"; 118 121 public static final String ERROR_BAD_MAP = "Cannot map source file to anything sensible: "; 122 125 public static final String ERROR_MAPPER_WITHOUT_DEST 126 = "The destDir attribute is required if a mapper is set"; 127 130 public static final String ERROR_NO_ALIAS = "alias attribute must be set"; 131 134 public static final String ERROR_NO_STOREPASS = "storepass attribute must be set"; 135 137 142 public void setSigfile(final String sigfile) { 143 this.sigfile = sigfile; 144 } 145 146 151 public void setSignedjar(final File signedjar) { 152 this.signedjar = signedjar; 153 } 154 155 161 public void setInternalsf(final boolean internalsf) { 162 this.internalsf = internalsf; 163 } 164 165 170 public void setSectionsonly(final boolean sectionsonly) { 171 this.sectionsonly = sectionsonly; 172 } 173 174 180 public void setLazy(final boolean lazy) { 181 this.lazy = lazy; 182 } 183 184 190 public void setDestDir(File destDir) { 191 this.destDir = destDir; 192 } 193 194 195 202 public void add(FileNameMapper newMapper) { 203 if (mapper != null) { 204 throw new BuildException(ERROR_TOO_MANY_MAPPERS); 205 } 206 mapper = newMapper; 207 } 208 209 214 public FileNameMapper getMapper() { 215 return mapper; 216 } 217 218 223 public String getTsaurl() { 224 return tsaurl; 225 } 226 227 232 public void setTsaurl(String tsaurl) { 233 this.tsaurl = tsaurl; 234 } 235 236 241 public String getTsacert() { 242 return tsacert; 243 } 244 245 249 public void setTsacert(String tsacert) { 250 this.tsacert = tsacert; 251 } 252 253 258 public void execute() throws BuildException { 259 final boolean hasJar = jar != null; 261 final boolean hasSignedJar = signedjar != null; 262 final boolean hasDestDir = destDir != null; 263 final boolean hasMapper = mapper != null; 264 265 if (!hasJar && !hasResources()) { 266 throw new BuildException(ERROR_NO_SOURCE); 267 } 268 if (null == alias) { 269 throw new BuildException(ERROR_NO_ALIAS); 270 } 271 272 if (null == storepass) { 273 throw new BuildException(ERROR_NO_STOREPASS); 274 } 275 276 if (hasDestDir && hasSignedJar) { 277 throw new BuildException(ERROR_TODIR_AND_SIGNEDJAR); 278 } 279 280 281 if (hasResources() && hasSignedJar) { 282 throw new BuildException(ERROR_SIGNEDJAR_AND_PATHS); 283 } 284 285 if (!hasDestDir && hasMapper) { 288 throw new BuildException(ERROR_MAPPER_WITHOUT_DEST); 289 } 290 291 beginExecution(); 292 293 294 try { 295 if (hasJar && hasSignedJar) { 297 signOneJar(jar, signedjar); 299 return; 301 } 302 303 306 Path sources = createUnifiedSourcePath(); 307 FileNameMapper destMapper; 309 if (hasMapper) { 310 destMapper = mapper; 311 } else { 312 destMapper = new IdentityMapper(); 314 } 315 316 317 Iterator iter = sources.iterator(); 322 while (iter.hasNext()) { 323 FileResource fr = (FileResource) iter.next(); 324 325 File toDir = hasDestDir ? destDir : fr.getBaseDir(); 328 329 String [] destFilenames = destMapper.mapFileName(fr.getName()); 331 if (destFilenames == null || destFilenames.length != 1) { 332 throw new BuildException(ERROR_BAD_MAP + fr.getFile()); 334 } 335 File destFile = new File (toDir, destFilenames[0]); 336 signOneJar(fr.getFile(), destFile); 337 } 338 } finally { 339 endExecution(); 340 } 341 } 342 343 353 private void signOneJar(File jarSource, File jarTarget) 354 throws BuildException { 355 356 357 File targetFile = jarTarget; 358 if (targetFile == null) { 359 targetFile = jarSource; 360 } 361 if (isUpToDate(jarSource, targetFile)) { 362 return; 363 } 364 365 long lastModified = jarSource.lastModified(); 366 final ExecTask cmd = createJarSigner(); 367 368 setCommonOptions(cmd); 369 370 bindToKeystore(cmd); 371 if (null != sigfile) { 372 addValue(cmd, "-sigfile"); 373 String value = this.sigfile; 374 addValue(cmd, value); 375 } 376 377 if (null != targetFile && !jarSource.equals(targetFile)) { 380 addValue(cmd, "-signedjar"); 381 addValue(cmd, targetFile.getPath()); 382 } 383 384 if (internalsf) { 385 addValue(cmd, "-internalsf"); 386 } 387 388 if (sectionsonly) { 389 addValue(cmd, "-sectionsonly"); 390 } 391 392 addTimestampAuthorityCommands(cmd); 394 395 addValue(cmd, jarSource.getPath()); 397 398 addValue(cmd, alias); 400 401 log("Signing JAR: " 402 + jarSource.getAbsolutePath() 403 + " to " 404 + targetFile.getAbsolutePath() 405 + " as " + alias); 406 407 cmd.execute(); 408 409 if (preserveLastModified) { 411 targetFile.setLastModified(lastModified); 412 } 413 } 414 415 421 private void addTimestampAuthorityCommands(final ExecTask cmd) { 422 if (tsaurl != null) { 423 addValue(cmd, "-tsa"); 424 addValue(cmd, tsaurl); 425 } 426 if (tsacert != null) { 427 addValue(cmd, "-tsacert"); 428 addValue(cmd, tsacert); 429 } 430 } 431 432 445 protected boolean isUpToDate(File jarFile, File signedjarFile) { 446 if (null == jarFile || !jarFile.exists()) { 447 return false; 450 } 451 452 File destFile = signedjarFile; 454 if (destFile == null) { 455 destFile = jarFile; 457 } 458 459 if (jarFile.equals(destFile)) { 461 if (lazy) { 462 return isSigned(jarFile); 464 } 465 return false; 467 } 468 469 return FILE_UTILS.isUpToDate(jarFile, destFile); 471 } 472 473 481 protected boolean isSigned(File file) { 482 try { 483 return IsSigned.isSigned(file, alias); 484 } catch (IOException e) { 485 log(e.toString(), Project.MSG_VERBOSE); 487 return false; 488 } 489 } 490 491 497 public void setPreserveLastModified(boolean preserveLastModified) { 498 this.preserveLastModified = preserveLastModified; 499 } 500 } 501 | Popular Tags |