Mercurial > dive4elements > framework
changeset 71:c99da6837be0
Load service factories and services during bootstrap.
artifacts/trunk@598 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 22 Jan 2010 11:27:57 +0000 |
parents | ce488c1d3fc4 |
children | b01e47317a78 |
files | ChangeLog artifact-database/doc/example-conf/conf.xml artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java |
diffstat | 3 files changed, 75 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Fri Jan 22 11:08:40 2010 +0000 +++ b/ChangeLog Fri Jan 22 11:27:57 2010 +0000 @@ -1,3 +1,10 @@ +2010-01-22 Sascha L. Teichmann <sascha.teichmann@intevation.de> + + * artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java: + Load service factories while bootstrapping the server. + + * artifact-database/doc/example-conf/conf.xml: Added examples for trivial services. + 2010-01-22 Sascha L. Teichmann <sascha.teichmann@intevation.de> * artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java:
--- a/artifact-database/doc/example-conf/conf.xml Fri Jan 22 11:08:40 2010 +0000 +++ b/artifact-database/doc/example-conf/conf.xml Fri Jan 22 11:27:57 2010 +0000 @@ -13,6 +13,16 @@ name="dummy-3" description="dummy description 3" artifact="de.intevation.artifactdatabase.DefaultArtifact">de.intevation.artifactdatabase.DefaultArtifactFactory</artifact-factory> </artifact-factories> + <service-factories> + <service-factory + name="first service" + service="de.intevation.artifactdatabase.DefaultService" + description="This is the first service">de.intevation.artifactdatabase.DefaultServiceFactory</service-factory> + <service-factory + name="second service" + service="de.intevation.artifactdatabase.DefaultService" + description="This is the second service">de.intevation.artifactdatabase.DefaultServiceFactory</service-factory> + </service-factories> </factories> <!-- configuration of the rest server --> <rest-server>
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java Fri Jan 22 11:08:40 2010 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java Fri Jan 22 11:27:57 2010 +0000 @@ -28,10 +28,15 @@ public static final String ARTIFACT_FACTORIES = "/artifact-database/factories/artifact-factories/artifact-factory"; + public static final String SERVICE_FACTORIES = + "/artifact-database/factories/service-factories/service-factory"; + protected Object context; protected ArtifactFactory [] artifactFactories; + protected ServiceFactory [] serviceFactories; + public FactoryBootstrap() { } @@ -69,6 +74,9 @@ } protected void loadArtifactFactories() { + + logger.info("loading artifact factories"); + ArrayList loadedFactories = new ArrayList(); NodeList nodes = Config.getNodeSetXPath(ARTIFACT_FACTORIES); @@ -80,7 +88,7 @@ Document config = Config.getConfig(); for (int i = 0, N = nodes != null ? nodes.getLength() : 0; i < N; ++i) { - String className = nodes.item(i).getTextContent(); + String className = nodes.item(i).getTextContent().trim(); ArtifactFactory factory = null; @@ -112,9 +120,57 @@ new ArtifactFactory[loadedFactories.size()]); } + protected void loadServiceFactories() { + + logger.info("loading service factories"); + + ArrayList loadedFactories = new ArrayList(); + + NodeList nodes = Config.getNodeSetXPath(SERVICE_FACTORIES); + + if (nodes == null) { + logger.warn("No factories found"); + } + + Document config = Config.getConfig(); + + for (int i = 0, N = nodes != null ? nodes.getLength() : 0; i < N; ++i) { + String className = nodes.item(i).getTextContent().trim(); + + ServiceFactory factory = null; + + try { + Class clazz = Class.forName(className); + factory = (ServiceFactory)clazz.newInstance(); + } + catch (ClassNotFoundException cnfe) { + logger.error(cnfe.getLocalizedMessage(), cnfe); + } + catch (InstantiationException ie) { + logger.error(ie.getLocalizedMessage(), ie); + } + catch (ClassCastException cce) { + logger.error(cce.getLocalizedMessage(), cce); + } + catch (IllegalAccessException iae) { + logger.error(iae.getLocalizedMessage(), iae); + } + + if (factory != null) { + factory.setup(config, nodes.item(i)); + loadedFactories.add(factory); + logger.info("Registering '" + factory.getName() + "' as service factory."); + } + } + + serviceFactories = (ServiceFactory [])loadedFactories.toArray( + new ServiceFactory[loadedFactories.size()]); + } + public void boot() { buildContext(); loadArtifactFactories(); + loadServiceFactories(); } public ArtifactFactory [] getArtifactFactories() { @@ -122,8 +178,7 @@ } public ServiceFactory [] getServiceFactories() { - logger.warn("Service factories are not supported yet"); - return new ServiceFactory[0]; + return serviceFactories; } public Object getContext() {