Commit | Line | Data |
---|---|---|
36537ce8 FT |
1 | package dolda.jsvc.store; |
2 | ||
3 | import dolda.jsvc.*; | |
4 | import dolda.jsvc.util.Misc; | |
5 | import java.io.*; | |
36537ce8 FT |
6 | import java.util.*; |
7 | ||
5d99f865 FT |
8 | public abstract class Store implements Iterable<File> { |
9 | private static Map<String, Factory> kinds = new TreeMap<String, Factory>(); | |
36537ce8 | 10 | private static Map<Package, Store> interned = new WeakHashMap<Package, Store>(); |
5d99f865 | 11 | protected final Package pkg; |
36537ce8 | 12 | |
5d99f865 | 13 | protected Store(Package pkg) { |
36537ce8 | 14 | this.pkg = pkg; |
36537ce8 FT |
15 | } |
16 | ||
5d99f865 FT |
17 | public abstract File get(String name); |
18 | ||
19 | public static interface Factory { | |
20 | public Store create(String root, Package pkg); | |
21 | } | |
22 | ||
23 | private static String getstoreroot() { | |
36537ce8 FT |
24 | ThreadContext ctx = ThreadContext.current(); |
25 | if(ctx == null) | |
26 | throw(new RuntimeException("Not running in jsvc context")); | |
6a0cb6cb | 27 | String bn = ctx.server().sysconfig("jsvc.storage", null); |
36537ce8 FT |
28 | if(bn == null) |
29 | throw(new RuntimeException("No storage root has been configured")); | |
5d99f865 | 30 | return(bn); |
36537ce8 | 31 | } |
5d99f865 | 32 | |
36537ce8 FT |
33 | public static Store forclass(final Class<?> cl) { |
34 | Package pkg = cl.getPackage(); | |
36537ce8 FT |
35 | Store s; |
36 | synchronized(interned) { | |
37 | s = interned.get(pkg); | |
38 | if(s == null) { | |
5d99f865 FT |
39 | String root = getstoreroot(); |
40 | int p = root.indexOf(':'); | |
41 | if(p < 0) | |
42 | throw(new RuntimeException("Invalid store specification: " + root)); | |
43 | String kind = root.substring(0, p); | |
44 | root = root.substring(p + 1); | |
45 | Factory fac; | |
46 | synchronized(kinds) { | |
47 | fac = kinds.get(kind); | |
48 | if(fac == null) | |
49 | throw(new RuntimeException("No such store kind: " + kind)); | |
50 | } | |
51 | s = fac.create(root, pkg); | |
36537ce8 FT |
52 | interned.put(pkg, s); |
53 | } | |
54 | } | |
55 | return(s); | |
56 | } | |
5d99f865 FT |
57 | |
58 | public static void register(String kind, Factory fac) { | |
59 | synchronized(kinds) { | |
60 | if(!kinds.containsKey(kind)) | |
61 | kinds.put(kind, fac); | |
62 | else | |
63 | throw(new RuntimeException("Store of type " + kind + " already exists")); | |
64 | } | |
65 | } | |
66 | ||
67 | static { | |
68 | FileStore.register(); | |
69 | } | |
36537ce8 | 70 | } |