KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > services > reflect > JarFileJava2


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

21
22 package org.apache.derby.impl.services.reflect;
23
24 import org.apache.derby.iapi.reference.MessageId;
25 import org.apache.derby.iapi.error.StandardException;
26 import org.apache.derby.iapi.services.i18n.MessageService;
27
28 import java.util.zip.ZipEntry JavaDoc;
29 import java.util.zip.ZipInputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.InputStream JavaDoc;
33
34 // below are all Java2 imports.
35
import java.security.cert.Certificate JavaDoc;
36 import java.security.cert.X509Certificate JavaDoc;
37 import java.security.GeneralSecurityException JavaDoc;
38
39
40
41 /**
42     Sub-class of JarFile for a Java2 environment that uses the
43     java.util.jar.* classes to be signature aware.
44 */

45
46 final class JarFileJava2 extends JarFile {
47
48     JarFileJava2() {
49         super();
50     }
51
52     JarFileJava2(String JavaDoc[] name) {
53         super(name);
54     }
55
56     JarFile newJarFile(String JavaDoc[] name) {
57         return new JarFileJava2(name);
58     }
59
60     void initialize(File JavaDoc jarFile) throws IOException JavaDoc {
61
62         java.util.jar.JarFile JavaDoc jf = new java.util.jar.JarFile JavaDoc(jarFile);
63
64         // determine if it is signed.
65
zip = jf;
66     }
67
68     ZipEntry JavaDoc getEntry(String JavaDoc entryName) {
69         return ((java.util.jar.JarFile JavaDoc) zip).getJarEntry(entryName);
70     }
71     ZipInputStream JavaDoc getZipOnStream(InputStream JavaDoc in) throws IOException JavaDoc {
72         return new java.util.jar.JarInputStream JavaDoc(in);
73     }
74     ZipEntry JavaDoc getNextEntry(ZipInputStream JavaDoc in) throws IOException JavaDoc {
75         return ((java.util.jar.JarInputStream JavaDoc) in).getNextJarEntry();
76     }
77
78     byte[] readData(ZipEntry JavaDoc ze, InputStream JavaDoc in, String JavaDoc className) throws IOException JavaDoc {
79         try {
80             return super.readData(ze, in, className);
81         } catch (SecurityException JavaDoc se) {
82             throw handleException(se, className);
83         }
84     }
85
86     Object JavaDoc[] getSigners(String JavaDoc className, ZipEntry JavaDoc ze) throws IOException JavaDoc {
87         Exception JavaDoc e;
88
89         try {
90             Certificate JavaDoc[] list = ((java.util.jar.JarEntry JavaDoc) ze).getCertificates();
91             if ((list == null) || (list.length == 0)) {
92                 return null;
93             }
94
95             for (int i = 0; i < list.length; i++) {
96                 if (!(list[i] instanceof X509Certificate JavaDoc)) {
97                     String JavaDoc msg = MessageService.getTextMessage(MessageId.CM_UNKNOWN_CERTIFICATE, className, getJarName());
98
99                     throw new SecurityException JavaDoc(msg);
100                 }
101
102                 X509Certificate JavaDoc cert = (X509Certificate JavaDoc) list[i];
103
104                 cert.checkValidity();
105             }
106
107             return list;
108
109         } catch (GeneralSecurityException JavaDoc gse) {
110             // convert this into an unchecked security
111
// exception. Unchecked as eventually it has
112
// to pass through a method that's only throwing
113
// ClassNotFoundException
114
e = gse;
115         }
116         throw handleException(e, className);
117     }
118
119     private SecurityException JavaDoc handleException(Exception JavaDoc e, String JavaDoc className) {
120         String JavaDoc msg = MessageService.getTextMessage(MessageId.CM_SECURITY_EXCEPTION, className, getJarName(), e.getLocalizedMessage());
121         return new SecurityException JavaDoc(msg);
122     }
123 }
124
Popular Tags