KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > jarprocessor > Unsigner


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.build.jarprocessor;
12 import java.io.*;
13 import java.util.Enumeration JavaDoc;
14 import java.util.jar.JarFile JavaDoc;
15 import java.util.jar.Manifest JavaDoc;
16 import java.util.zip.*;
17
18 /**
19  * This class removes the signature files from a jar and clean up the manifest.
20  */

21 public class Unsigner {
22     private static final String JavaDoc META_INF = "META-INF"; //$NON-NLS-1$
23
private static final String JavaDoc DSA_EXT = ".DSA"; //$NON-NLS-1$
24
private static final String JavaDoc RSA_EXT = ".RSA"; //$NON-NLS-1$
25
private static final String JavaDoc SF_EXT = ".SF"; //$NON-NLS-1$
26
private static final String JavaDoc META_INF_PREFIX = META_INF + '/';
27     
28     private String JavaDoc[] signers;
29     private File JavaDoc jarFile;
30     private boolean keepManifestContent = false;
31     
32     private boolean isSigned(File JavaDoc file) {
33         ZipFile jar = null;
34         try {
35             jar = new ZipFile(file);
36
37             if (signers != null) {
38                 for (int i = 0; i < signers.length; i++) {
39                     if (jar.getEntry((META_INF_PREFIX + signers[i] + SF_EXT).toUpperCase()) != null)
40                         return true;
41                 }
42             } else {
43                 Enumeration JavaDoc entries = jar.entries();
44                 while (entries.hasMoreElements()) {
45                     ZipEntry entry = (ZipEntry) entries.nextElement();
46                     String JavaDoc entryName = entry.getName();
47                     if(entryName.endsWith(SF_EXT) && entryName.startsWith(META_INF))
48                         return true;
49                 }
50             }
51             return false;
52         } catch (ZipException e) {
53             return false;
54         } catch (IOException e) {
55             return false;
56         } finally {
57             if (jar != null)
58                 try {
59                     jar.close();
60                 } catch (IOException e) {
61                     //Ignore
62
}
63         }
64     }
65     
66     public void execute() {
67         processJar(jarFile);
68     }
69     
70     private void processJar(File JavaDoc inputFile) {
71         if (!isSigned(inputFile))
72             return;
73
74         try {
75             ZipInputStream input = new ZipInputStream(new BufferedInputStream(new FileInputStream(inputFile)));
76             File JavaDoc outputFile = File.createTempFile("removing.", ".signature", inputFile.getParentFile()); //$NON-NLS-1$ //$NON-NLS-2$
77
ZipOutputStream output = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
78
79             ZipEntry inputZe = input.getNextEntry();
80             byte[] b = new byte[8192];
81             while (inputZe != null) {
82                 byte remove = shouldRemove(inputZe);
83                 if (remove == 2) {
84                     inputZe = input.getNextEntry();
85                     continue;
86                 }
87
88                 //copy the file or modify the manifest.mf
89
if (remove == 1) {
90                     output.putNextEntry(new ZipEntry(inputZe.getName()));
91                     Manifest JavaDoc m = new Manifest JavaDoc();
92                     m.read(input);
93                     m.getEntries().clear(); //This is probably not subtle enough
94
m.write(output);
95                 } else {
96                     output.putNextEntry(inputZe);
97                     while (input.available() != 0) {
98                         int read = input.read(b);
99                         if (read != -1)
100                             output.write(b, 0, read);
101                     }
102                 }
103                 output.closeEntry();
104                 input.closeEntry();
105
106                 inputZe = input.getNextEntry();
107             }
108             output.close();
109             input.close();
110             inputFile.delete();
111             outputFile.renameTo(inputFile);
112         } catch (FileNotFoundException e) {
113             //this can't happen we have checked before
114
} catch (IOException e) {
115             e.printStackTrace();
116         }
117     }
118     
119     private byte shouldRemove(ZipEntry entry) {
120         String JavaDoc entryName = entry.getName();
121         if(keepManifestContent == false && entryName.equalsIgnoreCase(JarFile.MANIFEST_NAME)) {
122             return 1;
123         }
124         if (signers != null) {
125             for (int i = 0; i < signers.length; i++) {
126                 if (entryName.equalsIgnoreCase(META_INF_PREFIX + signers[i] + SF_EXT) || entryName.equalsIgnoreCase(META_INF_PREFIX + signers[i] + RSA_EXT) || entryName.equalsIgnoreCase(META_INF_PREFIX + signers[i] + DSA_EXT))
127                     return 2;
128             }
129         } else {
130             if (entryName.startsWith(META_INF) && (entryName.endsWith(SF_EXT) || entryName.endsWith(RSA_EXT) || entryName.endsWith(DSA_EXT)))
131                 return 2;
132         }
133         return 0;
134     }
135     
136     public void setRemoveSigners(String JavaDoc[] fileName) {
137         signers = fileName;
138     }
139     
140     public void setJar(File JavaDoc file) {
141         jarFile = file;
142     }
143     
144     public void setKeepManifestEntries(boolean keep) {
145         keepManifestContent = keep;
146     }
147 }
148
Popular Tags