1 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 ; 37 import java.net.URL ; 38 import java.security.CodeSource ; 39 import java.security.cert.Certificate ; 40 import java.util.ArrayList ; 41 import java.util.Iterator ; 42 import java.util.Map ; 43 import java.util.jar.Attributes ; 44 import java.util.jar.Manifest ; 45 import java.util.logging.Level ; 46 import java.util.logging.Logger ; 47 import java.util.regex.Pattern ; 48 49 52 class JarEntry { 53 private static final L10N L = new L10N(JarEntry.class); 54 private static final Logger log 55 = Logger.getLogger(JarEntry.class.getName()); 56 57 private Manifest _manifest; 58 private JarPath _jarPath; 59 private ArrayList <ClassPackage> _packages = new ArrayList <ClassPackage>(); 60 61 private CodeSource _codeSource; 62 63 66 JarEntry(JarPath jarPath) 67 { 68 _jarPath = jarPath; 69 70 try { 71 _codeSource = new CodeSource (new URL (jarPath.getURL()), 72 (Certificate []) jarPath.getCertificates()); 73 } catch (Exception e) { 74 log.log(Level.WARNING, e.toString(), e); 75 } 76 77 readManifest(); 78 } 79 80 83 private void readManifest() 84 { 85 try { 86 _manifest = _jarPath.getManifest(); 87 if (_manifest == null) 88 return; 89 90 Attributes attr = _manifest.getMainAttributes(); 91 if (attr != null) 92 addManifestPackage("", attr); 93 94 Map entries = _manifest.getEntries(); 95 96 Iterator iter = entries.keySet().iterator(); 97 while (iter.hasNext()) { 98 String pkg = (String ) iter.next(); 99 100 attr = _manifest.getAttributes(pkg); 101 if (attr == null) 102 continue; 103 104 addManifestPackage(pkg, attr); 105 } 106 } catch (IOException e) { 107 log.log(Level.WARNING, e.toString(), e); 108 } 109 } 110 111 114 private void addManifestPackage(String name, Attributes attr) 115 { 116 if (! name.endsWith("/") && ! name.equals("")) 118 return; 119 120 String specTitle = attr.getValue("Specification-Title"); 121 String specVersion = attr.getValue("Specification-Version"); 122 String specVendor = attr.getValue("Specification-Vendor"); 123 String implTitle = attr.getValue("Implementation-Title"); 124 String implVersion = attr.getValue("Implementation-Version"); 125 String implVendor = attr.getValue("Implementation-Vendor"); 126 127 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 146 public void validate() 147 throws ConfigException 148 { 149 if (_manifest != null) 150 validateManifest(_jarPath.getContainer().getURL(), _manifest); 151 } 152 153 156 public static void validateManifest(String manifestName, Manifest manifest) 157 throws ConfigException 158 { 159 Attributes attr = manifest.getMainAttributes(); 160 if (attr == null) 161 return; 162 163 String extList = attr.getValue("Extension-List"); 164 if (extList == null) 165 return; 166 167 Pattern pattern = Pattern.compile("[, \t]+"); 168 String []split = pattern.split(extList); 169 170 for (int i = 0; i < split.length; i++) { 171 String ext = split[i]; 172 173 String name = attr.getValue(ext + "-Extension-Name"); 174 if (name == null) 175 continue; 176 177 Package 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 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 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 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 229 public CodeSource getCodeSource(String path) 230 { 231 try { 232 Path jarPath = _jarPath.lookup(path); 233 234 Certificate []certificates = jarPath.getCertificates(); 235 236 return new CodeSource (new URL (jarPath.getURL()), certificates); 237 } catch (Exception e) { 238 log.log(Level.WARNING, e.toString(), e); 239 240 return null; 241 } 242 } 243 244 247 public boolean equals(Object 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 |