bodewig     2004/11/22 03:42:53

  Modified:    src/main/org/apache/tools/ant/taskdefs SignJar.java
               src/main/org/apache/tools/ant/taskdefs/condition
                        IsSigned.java
  Log:
  * 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.
  
  Revision  Changes    Path
  1.39      +2 -28     ant/src/main/org/apache/tools/ant/taskdefs/SignJar.java
  
  Index: SignJar.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/SignJar.java,v
  retrieving revision 1.38
  retrieving revision 1.39
  diff -u -r1.38 -r1.39
  --- SignJar.java      22 Nov 2004 09:23:28 -0000      1.38
  +++ SignJar.java      22 Nov 2004 11:42:53 -0000      1.39
  @@ -26,6 +26,7 @@
   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 @@
        * @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) {
  -                }
  -            }
           }
       }
   }
  
  
  
  1.2       +47 -30    
ant/src/main/org/apache/tools/ant/taskdefs/condition/IsSigned.java
  
  Index: IsSigned.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/condition/IsSigned.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- IsSigned.java     22 Nov 2004 10:48:57 -0000      1.1
  +++ IsSigned.java     22 Nov 2004 11:42:53 -0000      1.2
  @@ -64,49 +64,35 @@
        * 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 {
  @@ -116,5 +102,36 @@
                   }
               }
           }
  +    }
  +
  +    /**
  +     * 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;
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to