comparison 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
comparison
equal deleted inserted replaced
561:1c2ce0501305 562:05caf2e731d0
18 import org.dive4elements.artifacts.Hook; 18 import org.dive4elements.artifacts.Hook;
19 import org.dive4elements.artifacts.ServiceFactory; 19 import org.dive4elements.artifacts.ServiceFactory;
20 import org.dive4elements.artifacts.UserFactory; 20 import org.dive4elements.artifacts.UserFactory;
21 21
22 import org.dive4elements.artifacts.common.utils.StringUtils; 22 import org.dive4elements.artifacts.common.utils.StringUtils;
23
24 import org.dive4elements.artifactdatabase.rest.HTTPServer; 23 import org.dive4elements.artifactdatabase.rest.HTTPServer;
25 24
26 import java.util.ArrayList; 25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List; 27 import java.util.List;
28 import java.util.Map;
28 29
29 import org.apache.log4j.Logger; 30 import org.apache.log4j.Logger;
30 31
31 import org.w3c.dom.Document; 32 import org.w3c.dom.Document;
32 import org.w3c.dom.Node; 33 import org.w3c.dom.Node;
68 */ 69 */
69 public static final String SERVICE_FACTORIES = 70 public static final String SERVICE_FACTORIES =
70 "/artifact-database/factories/service-factories/service-factory"; 71 "/artifact-database/factories/service-factories/service-factory";
71 72
72 /** 73 /**
74 * XPath to figure out the names of the addtional restful services from
75 * the global configuration.
76 */
77 public static final String RESTFUL_SERVICES =
78 "/artifact-database/factories/restful-services/restful-service";
79
80 /**
73 * XPath to figure out the class name of the user factory from global 81 * XPath to figure out the class name of the user factory from global
74 * configuration. 82 * configuration.
75 */ 83 */
76 public static final String USER_FACTORY = 84 public static final String USER_FACTORY =
77 "/artifact-database/factories/user-factory"; 85 "/artifact-database/factories/user-factory";
142 /** 150 /**
143 * List of the artifact factories to be exposed by the 151 * List of the artifact factories to be exposed by the
144 * artifact database. 152 * artifact database.
145 */ 153 */
146 protected ArtifactFactory [] artifactFactories; 154 protected ArtifactFactory [] artifactFactories;
155
156 private Map<String, Class<?>> restServices;
147 157
148 /** 158 /**
149 * List of service factories which creates services that are 159 * List of service factories which creates services that are
150 * exposed by the artifact database. 160 * exposed by the artifact database.
151 */ 161 */
359 369
360 serviceFactories = (ServiceFactory [])loadedFactories.toArray( 370 serviceFactories = (ServiceFactory [])loadedFactories.toArray(
361 new ServiceFactory[loadedFactories.size()]); 371 new ServiceFactory[loadedFactories.size()]);
362 } 372 }
363 373
374 /**
375 * Scans the global configuration for the configured service factories
376 * and sets them up.
377 */
378 private void loadRestfulServices() {
379
380 logger.info("loading additional restful services");
381
382 restServices = new HashMap<String, Class<?>>();
383
384 final NodeList nodes = Config.getNodeSetXPath(RESTFUL_SERVICES);
385
386 if (nodes == null)
387 return;
388
389 for (int i = 0; i < nodes.getLength(); ++i) {
390
391 final Node node = nodes.item(i);
392
393 final String className = Config.getStringXPath( node, "@class");
394 final String path = Config.getStringXPath( node, "@path");
395
396 try {
397 final Class<?> clazz = Class.forName(className);
398 restServices.put(path, clazz);
399 }
400 catch (final ClassNotFoundException cnfe) {
401 logger.error(cnfe.getLocalizedMessage(), cnfe);
402 }
403 catch (final ClassCastException cce) {
404 logger.error(cce.getLocalizedMessage(), cce);
405 }
406 }
407 }
364 408
365 /** 409 /**
366 * Scans the global configuration for the configured user factory. 410 * Scans the global configuration for the configured user factory.
367 */ 411 */
368 protected void loadUserFactory() { 412 protected void loadUserFactory() {
636 setupExportSecret(); 680 setupExportSecret();
637 buildContext(); 681 buildContext();
638 loadCollectionFactory(); 682 loadCollectionFactory();
639 loadArtifactFactories(); 683 loadArtifactFactories();
640 loadServiceFactories(); 684 loadServiceFactories();
685 loadRestfulServices();
641 loadUserFactory(); 686 loadUserFactory();
642 loadCallContextListener(); 687 loadCallContextListener();
643 loadHTTPServer(); 688 loadHTTPServer();
644 loadHooks(); 689 loadHooks();
645 loadLifetimeListeners(); 690 loadLifetimeListeners();
727 } 772 }
728 773
729 public List<BackendListener> getBackendListeners() { 774 public List<BackendListener> getBackendListeners() {
730 return backendListeners; 775 return backendListeners;
731 } 776 }
777
778 public Map<String,Class<?>> getRestServiceNames() {
779 return restServices;
780 }
732 } 781 }
733 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 782 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org