Browse Source

* Use <issigned> from <signjar>

* Refactor <issigned> by moving the test into a static method so it is
  easier to use from a different task.

* Avoid IndexOutOfBoundsException if name is shorter than eight characters.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277066 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 20 years ago
parent
commit
0adae3872f
2 changed files with 49 additions and 58 deletions
  1. +2
    -28
      src/main/org/apache/tools/ant/taskdefs/SignJar.java
  2. +47
    -30
      src/main/org/apache/tools/ant/taskdefs/condition/IsSigned.java

+ 2
- 28
src/main/org/apache/tools/ant/taskdefs/SignJar.java View File

@@ -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) {
}
}
}
}
}


+ 47
- 30
src/main/org/apache/tools/ant/taskdefs/condition/IsSigned.java View File

@@ -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 <CODE>true</code> if the file exists and is signed with
* the signature specified, or, if <CODE>name</code> 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;
}
}

Loading…
Cancel
Save