Added more flexible ways to create DataContextFactoryRegistry. 192/head
authorKasper Sørensen <i.am.kasper.sorensen@gmail.com>
Thu, 8 Nov 2018 05:48:07 +0000 (21:48 -0800)
committerKasper Sørensen <i.am.kasper.sorensen@gmail.com>
Thu, 8 Nov 2018 05:48:07 +0000 (21:48 -0800)
In the Membrane sub-project it will become relevant (for METAMODEL-1159)
to be able to make copies of the "default" registry, but with additional
tenant-specific factories, too. So this change makes it easier for
Membrane to copy a DataContextFactoryRegistry.

core/src/main/java/org/apache/metamodel/factory/DataContextFactoryRegistryImpl.java

index 0dfb897..91dc480 100644 (file)
@@ -26,15 +26,19 @@ import java.util.ServiceLoader;
 
 import org.apache.metamodel.DataContext;
 
-public class DataContextFactoryRegistryImpl implements DataContextFactoryRegistry {
+public class DataContextFactoryRegistryImpl implements DataContextFactoryRegistry, Cloneable {
 
     private static final DataContextFactoryRegistry DEFAULT_INSTANCE;
 
     static {
+        DEFAULT_INSTANCE = createFromClasspath();
+    }
+    
+    public static DataContextFactoryRegistry createFromClasspath() {
         final ResourceFactoryRegistry resourceFactoryRegistry = ResourceFactoryRegistryImpl.getDefaultInstance();
         final DataContextFactoryRegistryImpl registry = new DataContextFactoryRegistryImpl(resourceFactoryRegistry);
         registry.discoverFromClasspath();
-        DEFAULT_INSTANCE = registry;
+        return registry;
     }
 
     public static DataContextFactoryRegistry getDefaultInstance() {
@@ -49,6 +53,17 @@ public class DataContextFactoryRegistryImpl implements DataContextFactoryRegistr
         this.resourceFactoryRegistry = resourceFactoryRegistry;
     }
 
+    public DataContextFactoryRegistryImpl(Collection<DataContextFactory> factories,
+            ResourceFactoryRegistry resourceFactoryRegistry) {
+        this.factories = new ArrayList<>(factories);
+        this.resourceFactoryRegistry = resourceFactoryRegistry;
+    }
+
+    @Override
+    public DataContextFactoryRegistryImpl clone() {
+        return new DataContextFactoryRegistryImpl(factories, resourceFactoryRegistry);
+    }
+
     @Override
     public void addFactory(DataContextFactory factory) {
         factories.add(factory);