KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > condition > IsSigned


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.condition;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Enumeration JavaDoc;
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 /**
31  * Checks whether a jarfile is signed: if the name of the
32  * signature is passed, the file is checked for presence of that
33  * particular signature; otherwise the file is checked for the
34  * existence of any signature.
35  */

36 public class IsSigned extends DataType implements Condition {
37
38     private static final String JavaDoc SIG_START = "META-INF/";
39     private static final String JavaDoc SIG_END = ".SF";
40     private static final int SHORT_SIG_LIMIT = 8;
41
42     private String JavaDoc name;
43     private File JavaDoc file;
44
45     /**
46      * The jarfile that is to be tested for the presence
47      * of a signature.
48      * @param file jarfile to be tested.
49      */

50     public void setFile(File JavaDoc file) {
51         this.file = file;
52     }
53
54    /**
55      * The signature name to check jarfile for.
56      * @param name signature to look for.
57      */

58     public void setName(String JavaDoc name) {
59         this.name = name;
60     }
61
62     /**
63      * Returns <code>true</code> if the file exists and is signed with
64      * the signature specified, or, if <code>name</code> wasn't
65      * specified, if the file contains a signature.
66      * @param zipFile the zipfile to check
67      * @param name the signature to check (may be killed)
68      * @return true if the file is signed.
69      * @throws IOException on error
70      */

71     public static boolean isSigned(File JavaDoc zipFile, String JavaDoc name)
72         throws IOException JavaDoc {
73         ZipFile jarFile = null;
74         try {
75             jarFile = new ZipFile(zipFile);
76             if (null == name) {
77                 Enumeration JavaDoc entries = jarFile.getEntries();
78                 while (entries.hasMoreElements()) {
79                     String JavaDoc 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     /**
105      * Returns <code>true</code> if the file exists and is signed with
106      * the signature specified, or, if <code>name</code> wasn't
107      * specified, if the file contains a signature.
108      * @return true if the file is signed.
109      */

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 JavaDoc 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