Mercurial > dive4elements > framework
view artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java @ 125:cee54e52000c
Added the registration of the UserFactory to the bootstrap process.
artifacts/trunk@1349 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 01 Mar 2011 17:45:06 +0000 |
parents | 933bbc9fc11f |
children | 0245a87df368 |
line wrap: on
line source
/* * Copyright (c) 2010 by Intevation GmbH * * This program is free software under the LGPL (>=v2.1) * Read the file LGPL.txt coming with the software for details * or visit http://www.gnu.org/licenses/ if it does not exist. */ package de.intevation.artifactdatabase; import de.intevation.artifacts.ArtifactContextFactory; import de.intevation.artifacts.ArtifactFactory; import de.intevation.artifacts.ServiceFactory; import de.intevation.artifacts.UserFactory; import java.util.ArrayList; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.NodeList; /** * Bootstrap facility for the global context and the artifact factories. * * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> */ public class FactoryBootstrap { private static Logger logger = Logger.getLogger(FactoryBootstrap.class); /** * XPath to figure out the class name of the context factory from * the global configuration. */ public static final String CONTEXT_FACTORY = "/artifact-database/factories/context-factory/text()"; /** * The name of the default context factory. */ public static final String DEFAULT_CONTEXT_FACTORY = "de.intevation.artifactdatabase.DefaultArtifactContextFactory"; /** * XPath to figure out the names of the artifact factories from * the global configuration to be exposed by the artifact database. */ public static final String ARTIFACT_FACTORIES = "/artifact-database/factories/artifact-factories/artifact-factory"; /** * XPath to figure out the names of the service factories from * the global configuration to build the services offered by the * artifact database. */ public static final String SERVICE_FACTORIES = "/artifact-database/factories/service-factories/service-factory"; /** * XPath to figure out the class name of the user factory from global * configuration. */ public static final String USER_FACTORY = "/artfifact-database/factories/user-factory/text()"; /** * The name of the default user factory. */ public static final String DEFAULT_USER_FACTORY = "de.intevation.artifactdatabase.DefaultUserFactory"; /** * XPath to figure out the secret used to sign the artifact exports * made by the artfifact database server. */ public static final String EXPORT_SECRET = "/artifact-database/export-secret/text()"; /** * Default export signing secret. * <strong>PLEASE CHANGE THE SECRET VIA THE XPATH EXPORT_SECRET * IN THE CONFIGURATION.</strong>. */ public static final String DEFAULT_EXPORT_SECRET = "!!!CHANGE ME! I'M NO SECRET!!!"; /** * Reference to the global context build by the global context factory. */ protected Object context; /** * List of the artifact factories to be exposed by the * artifact database. */ protected ArtifactFactory [] artifactFactories; /** * List of service factories which creates services that are * exposed by the artifact database. */ protected ServiceFactory [] serviceFactories; /** * The factory that is used to create and list users. */ protected UserFactory userFactory; /** * byte array holding the export signing secret. */ protected byte [] exportSecret; /** * Default constructor */ public FactoryBootstrap() { } void buildContext() { String className = Config.getStringXPath( CONTEXT_FACTORY, DEFAULT_CONTEXT_FACTORY); ArtifactContextFactory factory = null; try { Class clazz = Class.forName(className); factory = (ArtifactContextFactory)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 = new DefaultArtifactContextFactory(); } logger.info("Using class '" + factory.getClass().getName() + "' for context creation."); context = factory.createArtifactContext(Config.getConfig()); } /** * Scans the global configuration to load the configured * artifact factories and sets them up. */ protected void loadArtifactFactories() { logger.info("loading artifact factories"); ArrayList loadedFactories = new ArrayList(); NodeList nodes = Config.getNodeSetXPath(ARTIFACT_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(); ArtifactFactory factory = null; try { Class clazz = Class.forName(className); factory = (ArtifactFactory)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 artifact factory."); } } artifactFactories = (ArtifactFactory [])loadedFactories.toArray( new ArtifactFactory[loadedFactories.size()]); } /** * Scans the global configuration for the configured service factories * and sets them up. */ 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()]); } /** * Scans the global configuration for the configured user factory. */ protected void loadUserFactory() { logger.info("loading user factory"); String className = Config.getStringXPath( USER_FACTORY, DEFAULT_USER_FACTORY); try { Class clazz = Class.forName(className); userFactory = (UserFactory) 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); } } /** * Fetches the export signing secret from the global configuration. * If none is found if defaults to the DEFAULT_EXORT_SECRET which * is insecure. */ protected void setupExportSecret() { String secret = Config.getStringXPath(EXPORT_SECRET); if (secret == null) { logger.warn("NO EXPORT SECRET SET! USING INSECURE DEFAULT!"); secret = DEFAULT_EXPORT_SECRET; } exportSecret = StringUtils.getUTF8Bytes(secret); } /** * Loads all the dynamic classes configured by the global configuration. */ public void boot() { setupExportSecret(); buildContext(); loadArtifactFactories(); loadServiceFactories(); loadUserFactory(); } /** * Returns the list of ready to use artifact factories. * @return The list of artifact factories. */ public ArtifactFactory [] getArtifactFactories() { return artifactFactories; } /** * Returns the ready to use service factories. * @return The list of service factories. */ public ServiceFactory [] getServiceFactories() { return serviceFactories; } /** * Returns the global context created by the global context factory. * @return The global context. */ public Object getContext() { return context; } /** * Returns the signing secret to be used when ex- and importing * artifacts from and into the artifact database. * @return the byte array containg the signing secret. */ public byte [] getExportSecret() { return exportSecret; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :