Cache reflection operations in AbstractSchemaAwareTest
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / test / AbstractSchemaAwareTest.java
index dc72e8bc592e1018e0d5a766f9f5923fe3d8504d..1c558b33b7a3d34674f6c2b9dbc4057881ff3941 100644 (file)
@@ -7,30 +7,47 @@
  */
 package org.opendaylight.mdsal.binding.dom.adapter.test;
 
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.collect.ImmutableSet;
+import java.util.Set;
 import org.junit.Before;
 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
-public abstract class AbstractSchemaAwareTest  {
+public abstract class AbstractSchemaAwareTest {
+    private static final LoadingCache<ClassLoader, Set<YangModuleInfo>> MODULE_INFO_CACHE = CacheBuilder.newBuilder()
+            .weakKeys().weakValues().build(new CacheLoader<ClassLoader, Set<YangModuleInfo>>() {
+                @Override
+                public Set<YangModuleInfo> load(final ClassLoader key) {
+                    return BindingReflections.loadModuleInfos(key);
+                }
+            });
+    private static final LoadingCache<Set<YangModuleInfo>, SchemaContext> SCHEMA_CONTEXT_CACHE =
+            CacheBuilder.newBuilder().weakValues().build(new CacheLoader<Set<YangModuleInfo>, SchemaContext>() {
+                @Override
+                public SchemaContext load(final Set<YangModuleInfo> key) {
+                    final ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
+                    moduleContext.addModuleInfos(key);
+                    return moduleContext.tryToCreateSchemaContext().get();
+                }
+            });
 
-    private Iterable<YangModuleInfo> moduleInfos;
-    private SchemaContext schemaContext;
-
-
-    protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
-        return BindingReflections.loadModuleInfos();
+    @Before
+    public final void setup() throws Exception {
+        setupWithSchema(getSchemaContext());
     }
 
+    protected Set<YangModuleInfo> getModuleInfos() throws Exception {
+        return MODULE_INFO_CACHE.getUnchecked(Thread.currentThread().getContextClassLoader());
+    }
 
-    @Before
-    public final void setup() throws Exception {
-        moduleInfos = getModuleInfos();
-        ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
-        moduleContext.addModuleInfos(moduleInfos);
-        schemaContext = moduleContext.tryToCreateSchemaContext().get();
-        setupWithSchema(schemaContext);
+    protected SchemaContext getSchemaContext() throws Exception {
+        // ImmutableSet guarantees non-null
+        return SCHEMA_CONTEXT_CACHE.getUnchecked(ImmutableSet.copyOf(getModuleInfos()));
     }
 
     /**
@@ -39,5 +56,4 @@ public abstract class AbstractSchemaAwareTest  {
      * @param context schema context
      */
     protected abstract void setupWithSchema(SchemaContext context);
-
 }