1 package dolda.jsvc.store;
4 import dolda.jsvc.util.Misc;
8 public abstract class Store implements Iterable<File> {
9 private static Map<String, Factory> kinds = new TreeMap<String, Factory>();
10 private static Map<Package, Store> interned = new WeakHashMap<Package, Store>();
11 protected final Package pkg;
13 protected Store(Package pkg) {
17 public abstract File get(String name);
19 public static interface Factory {
20 public Store create(String root, Package pkg);
23 private static String getstoreroot() {
24 ThreadContext ctx = ThreadContext.current();
26 throw(new RuntimeException("Not running in jsvc context"));
27 String bn = ctx.server().sysconfig("jsvc.storage", null);
29 throw(new RuntimeException("No storage root has been configured"));
33 public static Store forclass(final Class<?> cl) {
34 Package pkg = cl.getPackage();
36 synchronized(interned) {
37 s = interned.get(pkg);
39 String root = getstoreroot();
40 int p = root.indexOf(':');
42 throw(new RuntimeException("Invalid store specification: " + root));
43 String kind = root.substring(0, p);
44 root = root.substring(p + 1);
47 fac = kinds.get(kind);
49 throw(new RuntimeException("No such store kind: " + kind));
51 s = fac.create(root, pkg);
58 public static void register(String kind, Factory fac) {
60 if(!kinds.containsKey(kind))
63 throw(new RuntimeException("Store of type " + kind + " already exists"));