001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.condition;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.util.Enumeration;
023:
024: import org.apache.tools.ant.BuildException;
025: import org.apache.tools.ant.Project;
026: import org.apache.tools.ant.types.DataType;
027: import org.apache.tools.zip.ZipEntry;
028: import org.apache.tools.zip.ZipFile;
029:
030: /**
031: * Checks whether a jarfile is signed: if the name of the
032: * signature is passed, the file is checked for presence of that
033: * particular signature; otherwise the file is checked for the
034: * existence of any signature.
035: */
036: public class IsSigned extends DataType implements Condition {
037:
038: private static final String SIG_START = "META-INF/";
039: private static final String SIG_END = ".SF";
040: private static final int SHORT_SIG_LIMIT = 8;
041:
042: private String name;
043: private File file;
044:
045: /**
046: * The jarfile that is to be tested for the presence
047: * of a signature.
048: * @param file jarfile to be tested.
049: */
050: public void setFile(File file) {
051: this .file = file;
052: }
053:
054: /**
055: * The signature name to check jarfile for.
056: * @param name signature to look for.
057: */
058: public void setName(String name) {
059: this .name = name;
060: }
061:
062: /**
063: * Returns <code>true</code> if the file exists and is signed with
064: * the signature specified, or, if <code>name</code> wasn't
065: * specified, if the file contains a signature.
066: * @param zipFile the zipfile to check
067: * @param name the signature to check (may be killed)
068: * @return true if the file is signed.
069: * @throws IOException on error
070: */
071: public static boolean isSigned(File zipFile, String name)
072: throws IOException {
073: ZipFile jarFile = null;
074: try {
075: jarFile = new ZipFile(zipFile);
076: if (null == name) {
077: Enumeration entries = jarFile.getEntries();
078: while (entries.hasMoreElements()) {
079: String eName = ((ZipEntry) entries.nextElement())
080: .getName();
081: if (eName.startsWith(SIG_START)
082: && eName.endsWith(SIG_END)) {
083: return true;
084: }
085: }
086: return false;
087: }
088: boolean shortSig = jarFile.getEntry(SIG_START
089: + name.toUpperCase() + SIG_END) != null;
090: boolean longSig = false;
091: if (name.length() > SHORT_SIG_LIMIT) {
092: longSig = jarFile.getEntry(SIG_START
093: + name.substring(0, SHORT_SIG_LIMIT)
094: .toUpperCase() + SIG_END) != null;
095: }
096:
097: return shortSig || longSig;
098: } finally {
099: ZipFile.closeQuietly(jarFile);
100: }
101: }
102:
103: /**
104: * Returns <code>true</code> if the file exists and is signed with
105: * the signature specified, or, if <code>name</code> wasn't
106: * specified, if the file contains a signature.
107: * @return true if the file is signed.
108: */
109: public boolean eval() {
110: if (file == null) {
111: throw new BuildException("The file attribute must be set.");
112: }
113: if (file != null && !file.exists()) {
114: log("The file \"" + file.getAbsolutePath()
115: + "\" does not exist.", Project.MSG_VERBOSE);
116: return false;
117: }
118:
119: boolean r = false;
120: try {
121: r = isSigned(file, name);
122: } catch (IOException e) {
123: log("Got IOException reading file \""
124: + file.getAbsolutePath() + "\"" + e,
125: 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: }
|