diff artifact-database/src/main/java/org/dive4elements/artifactdatabase/FactoryBootstrap.java @ 562:05caf2e731d0

Allow to add additional restful services via configuration.
author gernotbelger
date Wed, 10 Oct 2018 20:07:58 +0200
parents 415df0fc4fa1
children
line wrap: on
line diff
--- a/artifact-database/src/main/java/org/dive4elements/artifactdatabase/FactoryBootstrap.java	Mon Sep 24 17:59:17 2018 +0200
+++ b/artifact-database/src/main/java/org/dive4elements/artifactdatabase/FactoryBootstrap.java	Wed Oct 10 20:07:58 2018 +0200
@@ -20,11 +20,12 @@
 import org.dive4elements.artifacts.UserFactory;
 
 import org.dive4elements.artifacts.common.utils.StringUtils;
-
 import org.dive4elements.artifactdatabase.rest.HTTPServer;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.log4j.Logger;
 
@@ -70,6 +71,13 @@
         "/artifact-database/factories/service-factories/service-factory";
 
     /**
+     * XPath to figure out the names of the addtional restful services from
+     * the global configuration.
+     */
+    public static final String RESTFUL_SERVICES =
+            "/artifact-database/factories/restful-services/restful-service";
+
+    /**
      * XPath to figure out the class name of the user factory from global
      * configuration.
      */
@@ -144,6 +152,8 @@
      * artifact database.
      */
     protected ArtifactFactory [] artifactFactories;
+    
+    private Map<String, Class<?>> restServices;
 
     /**
      * List of service factories which creates services that are
@@ -361,6 +371,40 @@
             new ServiceFactory[loadedFactories.size()]);
     }
 
+    /**
+     * Scans the global configuration for the configured service factories
+     * and sets them up.
+     */
+    private void loadRestfulServices() {
+        
+        logger.info("loading additional restful services");
+        
+        restServices = new HashMap<String, Class<?>>();
+        
+        final NodeList nodes = Config.getNodeSetXPath(RESTFUL_SERVICES);
+        
+        if (nodes == null)
+            return;
+        
+        for (int i = 0; i < nodes.getLength(); ++i) {
+            
+            final Node node = nodes.item(i);
+            
+            final String className = Config.getStringXPath( node, "@class");
+            final String path = Config.getStringXPath( node, "@path");
+
+            try {
+                final Class<?> clazz = Class.forName(className);
+                restServices.put(path, clazz);
+            }
+            catch (final ClassNotFoundException cnfe) {
+                logger.error(cnfe.getLocalizedMessage(), cnfe);
+            }
+            catch (final ClassCastException cce) {
+                logger.error(cce.getLocalizedMessage(), cce);
+            }
+        }
+    }
 
     /**
      * Scans the global configuration for the configured user factory.
@@ -638,6 +682,7 @@
         loadCollectionFactory();
         loadArtifactFactories();
         loadServiceFactories();
+        loadRestfulServices();
         loadUserFactory();
         loadCallContextListener();
         loadHTTPServer();
@@ -729,5 +774,9 @@
     public List<BackendListener> getBackendListeners() {
         return backendListeners;
     }
+
+    public Map<String,Class<?>> getRestServiceNames() {
+        return restServices;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org