001: package javax.cache;
002:
003: import ri.cache.ReferenceProvider;
004:
005: import javax.cache.spi.Provider;
006: import java.io.*;
007: import java.util.Properties;
008: import java.util.Set;
009: import java.util.concurrent.CopyOnWriteArraySet;
010:
011: /**
012: * The static methods in the JCache class are used in J2SE environments for finding a cache manager,
013: * and used by providers to register themselves.
014: * <p/>
015: * On startup, the system property "jcache.providers" is treated as a path-separator separated list of provider classes
016: * to be preloaded using the system class loader.
017: */
018: public class JCache {
019:
020: private static final Set<Provider> providers = new CopyOnWriteArraySet<Provider>();
021:
022: static {
023: loadDefaultProviders();
024: }
025:
026: // Prevent instantiation
027: private JCache() {
028:
029: }
030:
031: private static void loadDefaultProviders() {
032: loadProvider(new ReferenceProvider());
033:
034: try {
035: String providers = (String) java.security.AccessController
036: .doPrivileged(new sun.security.action.GetPropertyAction(
037: "jcache.providers"));
038: if (providers == null)
039: return;
040: for (String p : providers.split(System
041: .getProperty("path.separator"))) {
042: try {
043: Class.forName(p, true, ClassLoader
044: .getSystemClassLoader());
045: } catch (Exception ex) {
046: // @@@ log something?
047: }
048: }
049: } catch (Exception ex) {
050: // @@@ log something?
051: }
052: }
053:
054: /**
055: * Add a provider to the list of known providers. Providers will typically call this at static initialization
056: * time to add an instance of that provider class to the list.
057: * @param provider
058: */
059: public static void loadProvider(Provider provider) {
060: if (provider == null)
061: throw new NullPointerException();
062: providers.add(provider);
063: }
064:
065: public static void unloadProvider(Provider provider) {
066: if (provider == null)
067: throw new NullPointerException();
068: providers.remove(provider);
069: // @@@ Shut down caches managed by that provider?
070: }
071:
072: public static CacheManager getCacheManager(String uri)
073: throws CacheException {
074: if (uri == null || uri.equals("") || !uri.contains(":"))
075: throw new IllegalArgumentException(
076: "Invalid cache manager URI: " + uri);
077: for (Provider p : providers) {
078: if (!p.acceptsUri(uri))
079: continue;
080: CacheManager cm = p.getCacheManager(uri);
081: if (cm != null)
082: return cm;
083: }
084:
085: // @@@ Try service providers (using ServiceLoader or sun.misc.Service)
086:
087: throw new CacheException("Cannot find cache provider for URI: "
088: + uri);
089: }
090:
091: // private ClassLoader findClassLoader() {
092: // ClassLoader cl = Thread.currentThread().getContextClassLoader();
093: // if (cl == null) cl = ClassLoader.getSystemClassLoader();
094: // return cl;
095: // }
096: //
097: // private boolean isEmptyString(String s) {
098: // return s == null || "".equals(s);
099: // }
100: //
101: // String findFactory(String factoryId) {
102: //
103: // // Use the system property first
104: // try {
105: // String factoryClass = System.getProperty(factoryId);
106: // if (!isEmptyString(factoryClass)) return factoryClass;
107: // } catch (SecurityException ignore) {
108: // }
109: //
110: // // try to read from $java.home/lib/jcache.properties
111: // try {
112: // String configFile = System.getProperty("java.home") +
113: // File.separator + "lib" + File.separator + "jcache.properties";
114: // File f = new File(configFile);
115: // if (f.exists()) {
116: // InputStream in = new FileInputStream(f);
117: // try {
118: // Properties props = new Properties();
119: // props.load(in);
120: // String factoryClass = props.getProperty(factoryId);
121: // if (!isEmptyString(factoryClass)) return factoryClass;
122: // } finally {
123: // in.close();
124: // }
125: // }
126: // } catch (SecurityException ignore) {
127: // } catch (IOException ignore) {
128: // }
129: //
130: // // try to find services in CLASSPATH
131: // try {
132: // ClassLoader cl = findClassLoader();
133: // InputStream is = cl.getResourceAsStream("META-INF/services/" + factoryId);
134: // if (is != null) {
135: // BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));
136: // try {
137: // String factoryName = r.readLine();
138: // if (!isEmptyString(factoryName)) return factoryName;
139: // } finally {
140: // r.close();
141: // }
142: // }
143: // } catch (IOException ignore) {
144: // }
145: //
146: // return DEFAULT_FACTORY_NAME;
147: // }
148: }
|