Browse Source

Made SignJar support nested FileSets. It will also check if relevent signature file is present before signing if attribute lazy="true" is set

Submitted by:  "Nick Fortescue" <nick.fortescue@smartspread.com>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269205 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Donald 24 years ago
parent
commit
fcbf12d7b6
1 changed files with 128 additions and 40 deletions
  1. +128
    -40
      src/main/org/apache/tools/ant/taskdefs/SignJar.java

+ 128
- 40
src/main/org/apache/tools/ant/taskdefs/SignJar.java View File

@@ -54,13 +54,20 @@
package org.apache.tools.ant.taskdefs; package org.apache.tools.ant.taskdefs;


import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.tools.ant.*; import org.apache.tools.ant.*;
import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.FileSet;


/** /**
* Sign a archive. * Sign a archive.
*
*
* @author Peter Donald <a href="mailto:donaldp@apache.org">donaldp@apache.org</a> * @author Peter Donald <a href="mailto:donaldp@apache.org">donaldp@apache.org</a>
* @author Nick Fortescue <a href="mailto:nick@ox.compsoc.net">nick@ox.compsoc.net</a>
*/ */
public class SignJar extends Task { public class SignJar extends Task {


@@ -87,142 +94,223 @@ public class SignJar extends Task {
protected boolean internalsf; protected boolean internalsf;
protected boolean sectionsonly; protected boolean sectionsonly;


/**
* the filesets of the jars to sign
*/
protected Vector filesets = new Vector();
/**
* Whether to assume a jar which has an appropriate .SF file in is already
* signed.
*/
protected boolean lazy;

public void setJar(final String jar) { public void setJar(final String jar) {
this.jar = jar; this.jar = jar;
}
}


public void setAlias(final String alias) { public void setAlias(final String alias) {
this.alias = alias; this.alias = alias;
}
}


public void setKeystore(final String keystore) { public void setKeystore(final String keystore) {
this.keystore = keystore; this.keystore = keystore;
}
}


public void setStorepass(final String storepass) { public void setStorepass(final String storepass) {
this.storepass = storepass; this.storepass = storepass;
}
}


public void setStoretype(final String storetype) { public void setStoretype(final String storetype) {
this.storetype = storetype; this.storetype = storetype;
}
}


public void setKeypass(final String keypass) { public void setKeypass(final String keypass) {
this.keypass = keypass; this.keypass = keypass;
}
}


public void setSigfile(final String sigfile) { public void setSigfile(final String sigfile) {
this.sigfile = sigfile; this.sigfile = sigfile;
}
}


public void setSignedjar(final String signedjar) { public void setSignedjar(final String signedjar) {
this.signedjar = signedjar; this.signedjar = signedjar;
}
}


public void setVerbose(final boolean verbose) { public void setVerbose(final boolean verbose) {
this.verbose = verbose; this.verbose = verbose;
}
}


public void setInternalsf(final boolean internalsf) { public void setInternalsf(final boolean internalsf) {
this.internalsf = internalsf; this.internalsf = internalsf;
}
}


public void setSectionsonly(final boolean sectionsonly) { public void setSectionsonly(final boolean sectionsonly) {
this.sectionsonly = sectionsonly; this.sectionsonly = sectionsonly;
}
}

public void setLazy(final boolean lazy) {
this.lazy = lazy;
}

/**
* Adds a set of files (nested fileset attribute).
*/
public void addFileset(final FileSet set) {
filesets.addElement(set);
}



public void execute() throws BuildException { public void execute() throws BuildException {
if (null == jar && null == filesets) {
throw new BuildException("jar must be set through jar attribute or nested filesets");
}
if( null != jar ) {
doOneJar(jar, signedjar);
return;
} else {
//Assume null != filesets

// deal with the filesets
for (int i=0; i<filesets.size(); i++) {
FileSet fs = (FileSet) filesets.elementAt(i);
DirectoryScanner ds = fs.getDirectoryScanner(project);
String[] jarFiles = ds.getIncludedFiles();
for(int j=0; j<jarFiles.length; j++) {
doOneJar(jarFiles[j], null);
}
}
}
}

private void doOneJar(String jarSource, String jarTarget) throws BuildException {
if (project.getJavaVersion().equals(Project.JAVA_1_1)) { if (project.getJavaVersion().equals(Project.JAVA_1_1)) {
throw new BuildException("The signjar task is only available on JDK versions 1.2 or greater"); throw new BuildException("The signjar task is only available on JDK versions 1.2 or greater");
}

if (null == jar) {
throw new BuildException("jar attribute must be set");
}
}


if (null == alias) { if (null == alias) {
throw new BuildException("alias attribute must be set"); throw new BuildException("alias attribute must be set");
}
}


if (null == storepass) { if (null == storepass) {
throw new BuildException("storepass attribute must be set"); throw new BuildException("storepass attribute must be set");
}
}


if(isUpToDate()) return;
if(isUpToDate(jarSource, jarTarget)) return;


final StringBuffer sb = new StringBuffer(); final StringBuffer sb = new StringBuffer();


final ExecTask cmd = (ExecTask) project.createTask("exec"); final ExecTask cmd = (ExecTask) project.createTask("exec");
cmd.setExecutable("jarsigner"); cmd.setExecutable("jarsigner");
if (null != keystore) { if (null != keystore) {
cmd.createArg().setValue("-keystore"); cmd.createArg().setValue("-keystore");
cmd.createArg().setValue(keystore); cmd.createArg().setValue(keystore);
}
}


if (null != storepass) { if (null != storepass) {
cmd.createArg().setValue("-storepass"); cmd.createArg().setValue("-storepass");
cmd.createArg().setValue(storepass); cmd.createArg().setValue(storepass);
}
}


if (null != storetype) { if (null != storetype) {
cmd.createArg().setValue("-storetype"); cmd.createArg().setValue("-storetype");
cmd.createArg().setValue(storetype); cmd.createArg().setValue(storetype);
}
}


if (null != keypass) { if (null != keypass) {
cmd.createArg().setValue("-keypass"); cmd.createArg().setValue("-keypass");
cmd.createArg().setValue(keypass); cmd.createArg().setValue(keypass);
}
}


if (null != sigfile) { if (null != sigfile) {
cmd.createArg().setValue("-sigfile"); cmd.createArg().setValue("-sigfile");
cmd.createArg().setValue(sigfile); cmd.createArg().setValue(sigfile);
}
}


if (null != signedjar) {
if (null != jarTarget) {
cmd.createArg().setValue("-signedjar"); cmd.createArg().setValue("-signedjar");
cmd.createArg().setValue(signedjar);
}
cmd.createArg().setValue(jarTarget);
}


if (verbose) { if (verbose) {
cmd.createArg().setValue("-verbose"); cmd.createArg().setValue("-verbose");
}
}


if (internalsf) { if (internalsf) {
cmd.createArg().setValue("-internalsf"); cmd.createArg().setValue("-internalsf");
}
}


if (sectionsonly) { if (sectionsonly) {
cmd.createArg().setValue("-sectionsonly"); cmd.createArg().setValue("-sectionsonly");
}
}

cmd.createArg().setValue(jarSource);


cmd.createArg().setValue(jar);


cmd.createArg().setValue(alias); cmd.createArg().setValue(alias);


log("Signing Jar : " + (new File(jar)).getAbsolutePath());
log("Signing Jar : " + (new File(jarSource)).getAbsolutePath());
cmd.setFailonerror(true); cmd.setFailonerror(true);
cmd.setTaskName( getTaskName() ); cmd.setTaskName( getTaskName() );
cmd.execute(); cmd.execute();
} }


protected boolean isUpToDate() {
protected boolean isUpToDate(String jarSource, String jarTarget) {
if( null == jarSource ) {
return false;
}

if( null != jarTarget ) {


if( null != jar && null != signedjar ) {
final File jarFile = new File(jarSource);
final File signedjarFile = new File(jarTarget);


final File jarFile = new File(jar);
final File signedjarFile = new File(signedjar);
if(!jarFile.exists()) return false; if(!jarFile.exists()) return false;
if(!signedjarFile.exists()) return false; if(!signedjarFile.exists()) return false;
if(jarFile.equals(signedjarFile)) return false; if(jarFile.equals(signedjarFile)) return false;
if(signedjarFile.lastModified() > jarFile.lastModified()) if(signedjarFile.lastModified() > jarFile.lastModified())
return true; return true;
} else {
if( lazy ) {
return isSigned(jarSource);
}
} }


return false; return false;
} }

protected boolean isSigned(String jarFilename) {
final String SIG_START = "META-INF/";
final String SIG_END = ".SF";

File file = new File(jarFilename);
if( !file.exists() ) {
return false;
}
ZipFile jarFile = null;
try {
jarFile = new ZipFile(file);
if(null == alias) {
Enumeration entries = jarFile.entries();
while(entries.hasMoreElements()) {
String name = ((ZipEntry)entries.nextElement()).getName();
if(name.startsWith(SIG_START) && name.endsWith(SIG_END)) {
return true;
}
}
return false;
} else {
return jarFile.getEntry(SIG_START+alias.toUpperCase()+
SIG_END) != null;
}
} catch(IOException e) {
return false;
} finally {
if(jarFile != null) {
try {jarFile.close();} catch(IOException e) {}
}
}
}

} }



Loading…
Cancel
Save