1 package dolda.jsvc.store;
4 import dolda.jsvc.util.Misc;
6 import java.security.*;
7 import java.security.cert.Certificate;
11 private static Map<Package, Store> interned = new WeakHashMap<Package, Store>();
12 private final Package pkg;
13 private final File base;
15 private Store(Package pkg, CodeSource src, File root) {
17 String nm = pkg.getName();
21 MessageDigest fdig = MessageDigest.getInstance("MD5");
22 for(Certificate cert : src.getCertificates()) {
23 MessageDigest cdig = MessageDigest.getInstance("MD5");
24 cdig.update(cert.getEncoded());
25 fdig.update(cdig.digest());
27 byte[] fp = fdig.digest();
28 StringBuilder buf = new StringBuilder();
30 buf.append(Misc.int2hex((b & 0xf0) >> 4, true));
31 buf.append(Misc.int2hex(b & 0x0f, true));
33 base = new File(base, buf.toString());
34 } catch(NoSuchAlgorithmException e) {
36 } catch(java.security.cert.CertificateEncodingException e) {
42 while((p2 = nm.indexOf('.', p)) >= 0) {
43 base = new File(base, nm.substring(p, p2));
46 this.base = new File(base, nm.substring(p));
49 private static File getstoreroot() {
50 ThreadContext ctx = ThreadContext.current();
52 throw(new RuntimeException("Not running in jsvc context"));
53 String bn = ctx.server().config("jsvc.storage");
55 throw(new RuntimeException("No storage root has been configured"));
59 public static Store forclass(final Class<?> cl) {
60 Package pkg = cl.getPackage();
61 File root = getstoreroot();
63 synchronized(interned) {
64 s = interned.get(pkg);
67 dom = AccessController.doPrivileged(new PrivilegedAction<ProtectionDomain>() {
68 public ProtectionDomain run() {
70 return(cl.getProtectionDomain());
71 } catch(SecurityException e) {
77 s = new Store(pkg, dom.getCodeSource(), root);
79 s = new Store(pkg, null, root);