KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > loader > JarEntry


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.loader;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.util.L10N;
33 import com.caucho.vfs.JarPath;
34 import com.caucho.vfs.Path;
35
36 import java.io.IOException JavaDoc;
37 import java.net.URL JavaDoc;
38 import java.security.CodeSource JavaDoc;
39 import java.security.cert.Certificate JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.Map JavaDoc;
43 import java.util.jar.Attributes JavaDoc;
44 import java.util.jar.Manifest JavaDoc;
45 import java.util.logging.Level JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47 import java.util.regex.Pattern JavaDoc;
48
49 /**
50  * JarEntry.
51  */

52 class JarEntry {
53   private static final L10N L = new L10N(JarEntry.class);
54   private static final Logger JavaDoc log
55     = Logger.getLogger(JarEntry.class.getName());
56
57   private Manifest JavaDoc _manifest;
58   private JarPath _jarPath;
59   private ArrayList JavaDoc<ClassPackage> _packages = new ArrayList JavaDoc<ClassPackage>();
60
61   private CodeSource JavaDoc _codeSource;
62
63   /**
64    * Creates a JarEntry.
65    */

66   JarEntry(JarPath jarPath)
67   {
68     _jarPath = jarPath;
69
70     try {
71       _codeSource = new CodeSource JavaDoc(new URL JavaDoc(jarPath.getURL()),
72                    (Certificate JavaDoc []) jarPath.getCertificates());
73     } catch (Exception JavaDoc e) {
74       log.log(Level.WARNING, e.toString(), e);
75     }
76
77     readManifest();
78   }
79
80   /**
81    * Reads the jar's manifest.
82    */

83   private void readManifest()
84   {
85     try {
86       _manifest = _jarPath.getManifest();
87       if (_manifest == null)
88         return;
89
90       Attributes JavaDoc attr = _manifest.getMainAttributes();
91       if (attr != null)
92         addManifestPackage("", attr);
93
94       Map JavaDoc entries = _manifest.getEntries();
95       
96       Iterator JavaDoc iter = entries.keySet().iterator();
97       while (iter.hasNext()) {
98         String JavaDoc pkg = (String JavaDoc) iter.next();
99         
100         attr = _manifest.getAttributes(pkg);
101         if (attr == null)
102           continue;
103
104         addManifestPackage(pkg, attr);
105       }
106     } catch (IOException JavaDoc e) {
107       log.log(Level.WARNING, e.toString(), e);
108     }
109   }
110
111   /**
112    * Adds package information from the manifest.
113    */

114   private void addManifestPackage(String JavaDoc name, Attributes JavaDoc attr)
115   {
116     // only add packages
117
if (! name.endsWith("/") && ! name.equals(""))
118       return;
119
120     String JavaDoc specTitle = attr.getValue("Specification-Title");
121     String JavaDoc specVersion = attr.getValue("Specification-Version");
122     String JavaDoc specVendor = attr.getValue("Specification-Vendor");
123     String JavaDoc implTitle = attr.getValue("Implementation-Title");
124     String JavaDoc implVersion = attr.getValue("Implementation-Version");
125     String JavaDoc implVendor = attr.getValue("Implementation-Vendor");
126
127     // If all none, then it isn't a package entry
128
if (specTitle == null && specVersion == null && specVendor != null &&
129         implTitle == null && implVersion == null && implVendor != null)
130       return;
131
132     ClassPackage pkg = new ClassPackage(name);
133     pkg.setSpecificationTitle(specTitle);
134     pkg.setSpecificationVersion(specVersion);
135     pkg.setSpecificationVendor(specVendor);
136     pkg.setImplementationTitle(implTitle);
137     pkg.setImplementationVersion(implVersion);
138     pkg.setImplementationVendor(implVendor);
139
140     _packages.add(pkg);
141   }
142
143   /**
144    * Validates the jar.
145    */

146   public void validate()
147     throws ConfigException
148   {
149     if (_manifest != null)
150       validateManifest(_jarPath.getContainer().getURL(), _manifest);
151   }
152
153   /**
154    * Validates the manifest.
155    */

156   public static void validateManifest(String JavaDoc manifestName, Manifest JavaDoc manifest)
157     throws ConfigException
158   {
159     Attributes JavaDoc attr = manifest.getMainAttributes();
160     if (attr == null)
161       return;
162
163     String JavaDoc extList = attr.getValue("Extension-List");
164     if (extList == null)
165       return;
166
167     Pattern JavaDoc pattern = Pattern.compile("[, \t]+");
168     String JavaDoc []split = pattern.split(extList);
169
170     for (int i = 0; i < split.length; i++) {
171       String JavaDoc ext = split[i];
172
173       String JavaDoc name = attr.getValue(ext + "-Extension-Name");
174       if (name == null)
175     continue;
176
177       Package JavaDoc pkg = Package.getPackage(name);
178
179       if (pkg == null) {
180     log.warning(L.l("package {0} is missing. {1} requires package {0}.",
181             name, manifestName));
182     continue;
183       }
184
185       String JavaDoc version = attr.getValue(ext + "-Specification-Version");
186
187       if (version == null)
188     continue;
189
190       if (pkg.getSpecificationVersion() == null ||
191       pkg.getSpecificationVersion().equals("")) {
192     log.warning(L.l("installed {0} is not compatible with version `{1}'. {2} requires version {1}.",
193              name, version, manifestName));
194       }
195       else if (! pkg.isCompatibleWith(version)) {
196     log.warning(L.l("installed {0} is not compatible with version `{1}'. {2} requires version {1}.",
197              name, version, manifestName));
198       }
199     }
200   }
201
202   public ClassPackage getPackage(String JavaDoc name)
203   {
204     ClassPackage bestPackage = null;
205     int bestLength = -1;
206
207     for (int i = 0; i < _packages.size(); i++) {
208       ClassPackage pkg = _packages.get(i);
209
210       String JavaDoc prefix = pkg.getPrefix();
211
212       if (name.startsWith(prefix) && bestLength < prefix.length()) {
213         bestPackage = pkg;
214         bestLength = prefix.length();
215       }
216     }
217
218     return bestPackage;
219   }
220
221   public JarPath getJarPath()
222   {
223     return _jarPath;
224   }
225
226   /**
227    * Returns the code source.
228    */

229   public CodeSource JavaDoc getCodeSource(String JavaDoc path)
230   {
231     try {
232       Path jarPath = _jarPath.lookup(path);
233       
234       Certificate JavaDoc []certificates = jarPath.getCertificates();
235     
236       return new CodeSource JavaDoc(new URL JavaDoc(jarPath.getURL()), certificates);
237     } catch (Exception JavaDoc e) {
238       log.log(Level.WARNING, e.toString(), e);
239
240       return null;
241     }
242   }
243
244   /**
245    * Tests for equality.
246    */

247   public boolean equals(Object JavaDoc o)
248   {
249     if (! (o instanceof JarEntry))
250       return false;
251
252     JarEntry entry = (JarEntry) o;
253
254     return _jarPath.equals(entry._jarPath);
255   }
256 }
257
Popular Tags