FileDocCategorySizeDatePackage
IsSigned.javaAPI DocApache Ant 1.704592Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs.condition

IsSigned

public class IsSigned extends org.apache.tools.ant.types.DataType implements Condition
Checks whether a jarfile is signed: if the name of the signature is passed, the file is checked for presence of that particular signature; otherwise the file is checked for the existence of any signature.

Fields Summary
private static final String
SIG_START
private static final String
SIG_END
private static final int
SHORT_SIG_LIMIT
private String
name
private File
file
Constructors Summary
Methods Summary
public booleaneval()
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.

        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;
    
public static booleanisSigned(java.io.File zipFile, java.lang.String name)
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.

param
zipFile the zipfile to check
param
name the signature to check (may be killed)
return
true if the file is signed.
throws
IOException on error

        ZipFile jarFile = null;
        try {
            jarFile = new ZipFile(zipFile);
            if (null == name) {
                Enumeration entries = jarFile.getEntries();
                while (entries.hasMoreElements()) {
                    String eName = ((ZipEntry) entries.nextElement()).getName();
                    if (eName.startsWith(SIG_START)
                        && eName.endsWith(SIG_END)) {
                        return true;
                    }
                }
                return false;
            }
            boolean shortSig = jarFile.getEntry(SIG_START
                        + name.toUpperCase()
                        + SIG_END) != null;
            boolean longSig = false;
            if (name.length() > SHORT_SIG_LIMIT) {
                longSig = jarFile.getEntry(
                    SIG_START
                    + name.substring(0, SHORT_SIG_LIMIT).toUpperCase()
                    + SIG_END) != null;
            }

            return shortSig || longSig;
        } finally {
            ZipFile.closeQuietly(jarFile);
        }
    
public voidsetFile(java.io.File file)
The jarfile that is to be tested for the presence of a signature.

param
file jarfile to be tested.


                            
        
        this.file = file;
    
public voidsetName(java.lang.String name)
The signature name to check jarfile for.

param
name signature to look for.

        this.name = name;