BUG-994: improve object cache
[yangtools.git] / common / object-cache-api / src / main / java / org / opendaylight / yangtools / objcache / ObjectCacheFactory.java
index 58ca11c4d2335c489cedffd79634d05a14b9aa6d..dd3e53bbd0225119116a4ed0180620d3a2a11352 100644 (file)
@@ -11,6 +11,7 @@ import javax.annotation.Nonnull;
 
 import org.opendaylight.yangtools.objcache.impl.StaticObjectCacheBinder;
 import org.opendaylight.yangtools.objcache.spi.IObjectCacheFactory;
+import org.opendaylight.yangtools.objcache.spi.NoopObjectCacheBinder;
 
 import com.google.common.base.Preconditions;
 
@@ -18,36 +19,42 @@ import com.google.common.base.Preconditions;
  * Point of entry for acquiring an {@link ObjectCache} instance.
  */
 public final class ObjectCacheFactory {
-       private static IObjectCacheFactory FACTORY;
-
-       private static synchronized IObjectCacheFactory initialize() {
-               // Double-check under lock
-               if (FACTORY != null) {
-                       return FACTORY;
-               }
-
-               final IObjectCacheFactory f = StaticObjectCacheBinder.getInstance().getProductCacheFactory();
-               FACTORY = f;
-               return f;
-       }
-
-       public static synchronized void reset() {
-               FACTORY = null;
-       }
-
-       /**
-        * Get an ObjectCache for caching a particular object class. Note
-        * that it may be shared for multiple classes.
-        * 
-        * @param objClass Class of objects which are to be cached
-        * @return Object cache instance.
-        */
-       public static ObjectCache getObjectCache(@Nonnull final Class<?> objClass) {
-               IObjectCacheFactory f = FACTORY;
-               if (f == null) {
-                       f = initialize();
-               }
-
-               return f.getObjectCache(Preconditions.checkNotNull(objClass));
-       }
+    private static IObjectCacheFactory FACTORY;
+
+    private static synchronized IObjectCacheFactory initialize() {
+        // Double-check under lock
+        if (FACTORY != null) {
+            return FACTORY;
+        }
+
+        IObjectCacheFactory f;
+        try {
+            f = StaticObjectCacheBinder.getInstance().getProductCacheFactory();
+            FACTORY = f;
+        } catch (NoClassDefFoundError e) {
+            f = NoopObjectCacheBinder.INSTANCE.getProductCacheFactory();
+        }
+
+        return f;
+    }
+
+    public static synchronized void reset() {
+        FACTORY = null;
+    }
+
+    /**
+     * Get an ObjectCache for caching a particular object class. Note
+     * that it may be shared for multiple classes.
+     *
+     * @param objClass Class of objects which are to be cached
+     * @return Object cache instance.
+     */
+    public static ObjectCache getObjectCache(@Nonnull final Class<?> objClass) {
+        IObjectCacheFactory f = FACTORY;
+        if (f == null) {
+            f = initialize();
+        }
+
+        return f.getObjectCache(Preconditions.checkNotNull(objClass));
+    }
 }