1 18 package org.apache.tools.ant.taskdefs.condition; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.util.Enumeration ; 23 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.Project; 26 import org.apache.tools.ant.types.DataType; 27 import org.apache.tools.zip.ZipEntry; 28 import org.apache.tools.zip.ZipFile; 29 30 36 public class IsSigned extends DataType implements Condition { 37 38 private static final String SIG_START = "META-INF/"; 39 private static final String SIG_END = ".SF"; 40 private static final int SHORT_SIG_LIMIT = 8; 41 42 private String name; 43 private File file; 44 45 50 public void setFile(File file) { 51 this.file = file; 52 } 53 54 58 public void setName(String name) { 59 this.name = name; 60 } 61 62 71 public static boolean isSigned(File zipFile, String name) 72 throws IOException { 73 ZipFile jarFile = null; 74 try { 75 jarFile = new ZipFile(zipFile); 76 if (null == name) { 77 Enumeration entries = jarFile.getEntries(); 78 while (entries.hasMoreElements()) { 79 String eName = ((ZipEntry) entries.nextElement()).getName(); 80 if (eName.startsWith(SIG_START) 81 && eName.endsWith(SIG_END)) { 82 return true; 83 } 84 } 85 return false; 86 } 87 boolean shortSig = jarFile.getEntry(SIG_START 88 + name.toUpperCase() 89 + SIG_END) != null; 90 boolean longSig = false; 91 if (name.length() > SHORT_SIG_LIMIT) { 92 longSig = jarFile.getEntry( 93 SIG_START 94 + name.substring(0, SHORT_SIG_LIMIT).toUpperCase() 95 + SIG_END) != null; 96 } 97 98 return shortSig || longSig; 99 } finally { 100 ZipFile.closeQuietly(jarFile); 101 } 102 } 103 104 110 public boolean eval() { 111 if (file == null) { 112 throw new BuildException("The file attribute must be set."); 113 } 114 if (file != null && !file.exists()) { 115 log("The file \"" + file.getAbsolutePath() 116 + "\" does not exist.", Project.MSG_VERBOSE); 117 return false; 118 } 119 120 boolean r = false; 121 try { 122 r = isSigned(file, name); 123 } catch (IOException e) { 124 log("Got IOException reading file \"" + file.getAbsolutePath() 125 + "\"" + e, Project.MSG_WARN); 126 } 127 128 if (r) { 129 log("File \"" + file.getAbsolutePath() + "\" is signed.", 130 Project.MSG_VERBOSE); 131 } 132 return r; 133 } 134 } 135 | Popular Tags |