diff --git a/src/main/org/apache/tools/ant/taskdefs/SignJar.java b/src/main/org/apache/tools/ant/taskdefs/SignJar.java index f368ddf8f..cd19b03f0 100644 --- a/src/main/org/apache/tools/ant/taskdefs/SignJar.java +++ b/src/main/org/apache/tools/ant/taskdefs/SignJar.java @@ -26,6 +26,7 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; +import org.apache.tools.ant.taskdefs.condition.IsSigned; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.util.JavaEnvUtils; import org.apache.tools.ant.util.FileUtils; @@ -338,37 +339,10 @@ public class SignJar extends Task { * @return true if the file is signed */ protected boolean isSigned(File file) { - final String SIG_START = "META-INF/"; - final String SIG_END = ".SF"; - - 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; - } + return IsSigned.isSigned(file, alias); } catch (IOException e) { return false; - } finally { - if (jarFile != null) { - try { - jarFile.close(); - } catch (IOException e) { - } - } } } } diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/IsSigned.java b/src/main/org/apache/tools/ant/taskdefs/condition/IsSigned.java index 9f9a2bade..3e57b682a 100644 --- a/src/main/org/apache/tools/ant/taskdefs/condition/IsSigned.java +++ b/src/main/org/apache/tools/ant/taskdefs/condition/IsSigned.java @@ -64,49 +64,35 @@ public class IsSigned extends DataType implements Condition { * specified, if the file contains a signature. * @return true if the file is signed. */ - public boolean eval() { - if (file == null) { - throw new BuildException("The file attribute must be set."); - } - if (file != null && !file.exists()) { - log("The file \"" + file.getAbsolutePath() - + "\" does not exist.", Project.MSG_VERBOSE); - return false; - } - + public static boolean isSigned(File zipFile, String name) + throws IOException { ZipFile jarFile = null; try { - jarFile = new ZipFile(file); + jarFile = new ZipFile(zipFile); if (null == name) { Enumeration entries = jarFile.entries(); while (entries.hasMoreElements()) { - String name = ((ZipEntry) entries.nextElement()).getName(); - if (name.startsWith(SIG_START) && name.endsWith(SIG_END)) { - log("File \"" + file.getAbsolutePath() - + "\" is signed.", Project.MSG_VERBOSE); + String eName = ((ZipEntry) entries.nextElement()).getName(); + if (eName.startsWith(SIG_START) + && eName.endsWith(SIG_END)) { return true; } } return false; } else { boolean shortSig = jarFile.getEntry(SIG_START - + name.toUpperCase() - + SIG_END) != null; - boolean longSig = jarFile.getEntry(SIG_START - + name.substring(0, 8).toUpperCase() - + SIG_END) != null; - if (shortSig || longSig) { - log("File \"" + file.getAbsolutePath() - + "\" is signed.", Project.MSG_VERBOSE); - return true; - } else { - return false; + + name.toUpperCase() + + SIG_END) != null; + boolean longSig = false; + if (name.length() > 8) { + longSig = + jarFile.getEntry(SIG_START + + name.substring(0, 8).toUpperCase() + + SIG_END) != null; } + + return shortSig || longSig; } - } catch (IOException e) { - log("Got IOException reading file \"" + file.getAbsolutePath() - + "\"" + e, Project.MSG_VERBOSE); - return false; } finally { if (jarFile != null) { try { @@ -117,4 +103,35 @@ public class IsSigned extends DataType implements Condition { } } } + + /** + * Returns true if the file exists and is signed with + * the signature specified, or, if name wasn't + * specified, if the file contains a signature. + * @return true if the file is signed. + */ + public boolean eval() { + if (file == null) { + throw new BuildException("The file attribute must be set."); + } + if (file != null && !file.exists()) { + log("The file \"" + file.getAbsolutePath() + + "\" does not exist.", Project.MSG_VERBOSE); + return false; + } + + boolean r = false; + try { + r = isSigned(file, name); + } catch (IOException e) { + log("Got IOException reading file \"" + file.getAbsolutePath() + + "\"" + e, Project.MSG_WARN); + } + + if (r) { + log("File \"" + file.getAbsolutePath() + "\" is signed.", + Project.MSG_VERBOSE); + } + return r; + } }