sascha@5: package de.intevation.artifactdatabase; sascha@5: sascha@70: import de.intevation.artifacts.ArtifactContextFactory; sascha@70: import de.intevation.artifacts.ArtifactFactory; sascha@70: import de.intevation.artifacts.ServiceFactory; sascha@70: sascha@70: import java.util.ArrayList; sascha@70: sascha@70: import org.apache.log4j.Logger; sascha@70: sascha@5: import org.w3c.dom.Document; sascha@5: import org.w3c.dom.NodeList; ingo@79: sascha@5: /** sascha@5: * Bootstrap facility for the global context and the artifact factories. sascha@5: * sascha@77: * @author Sascha L. Teichmann sascha@5: */ sascha@5: public class FactoryBootstrap sascha@5: { sascha@17: private static Logger logger = Logger.getLogger(FactoryBootstrap.class); sascha@17: sascha@5: public static final String CONTEXT_FACTORY = sascha@5: "/artifact-database/factories/context-factory/text()"; sascha@5: sascha@5: public static final String DEFAULT_CONTEXT_FACTORY = sascha@5: "de.intevation.artifactdatabase.DefaultArtifactContextFactory"; sascha@5: sascha@5: public static final String ARTIFACT_FACTORIES = tim@16: "/artifact-database/factories/artifact-factories/artifact-factory"; sascha@5: sascha@71: public static final String SERVICE_FACTORIES = sascha@71: "/artifact-database/factories/service-factories/service-factory"; sascha@71: ingo@79: public static final String EXPORT_SECRET = ingo@79: "/artifact-database/export-secret/text()"; ingo@79: ingo@79: public static final String DEFAULT_EXORT_SECRET = ingo@79: "!!!CHANGE ME! I'M NO SECRET!!!"; ingo@79: sascha@5: protected Object context; sascha@5: sascha@5: protected ArtifactFactory [] artifactFactories; sascha@5: sascha@71: protected ServiceFactory [] serviceFactories; sascha@71: ingo@79: protected byte [] exportSecret; ingo@79: ingo@79: sascha@5: public FactoryBootstrap() { sascha@5: } sascha@5: sascha@5: void buildContext() { sascha@5: String className = Config.getStringXPath( sascha@5: CONTEXT_FACTORY, DEFAULT_CONTEXT_FACTORY); sascha@5: sascha@5: ArtifactContextFactory factory = null; sascha@5: sascha@5: try { sascha@5: Class clazz = Class.forName(className); sascha@5: factory = (ArtifactContextFactory)clazz.newInstance(); sascha@5: } sascha@5: catch (ClassNotFoundException cnfe) { sascha@17: logger.error(cnfe.getLocalizedMessage(), cnfe); sascha@5: } sascha@5: catch (InstantiationException ie) { sascha@17: logger.error(ie.getLocalizedMessage(), ie); sascha@5: } sascha@5: catch (ClassCastException cce) { sascha@17: logger.error(cce.getLocalizedMessage(), cce); sascha@5: } sascha@5: catch (IllegalAccessException iae) { sascha@17: logger.error(iae.getLocalizedMessage(), iae); sascha@5: } sascha@5: sascha@5: if (factory == null) { sascha@5: factory = new DefaultArtifactContextFactory(); sascha@5: } sascha@5: sascha@19: logger.info("Using class '" + factory.getClass().getName() sascha@19: + "' for context creation."); sascha@19: sascha@5: context = factory.createArtifactContext(Config.getConfig()); sascha@5: } sascha@5: sascha@5: protected void loadArtifactFactories() { sascha@71: sascha@71: logger.info("loading artifact factories"); sascha@71: sascha@5: ArrayList loadedFactories = new ArrayList(); sascha@5: sascha@5: NodeList nodes = Config.getNodeSetXPath(ARTIFACT_FACTORIES); sascha@5: sascha@10: if (nodes == null) { sascha@17: logger.warn("No factories found"); sascha@10: } sascha@10: sascha@5: Document config = Config.getConfig(); sascha@5: sascha@19: for (int i = 0, N = nodes != null ? nodes.getLength() : 0; i < N; ++i) { sascha@71: String className = nodes.item(i).getTextContent().trim(); sascha@5: sascha@5: ArtifactFactory factory = null; sascha@5: sascha@5: try { sascha@5: Class clazz = Class.forName(className); sascha@5: factory = (ArtifactFactory)clazz.newInstance(); sascha@5: } sascha@5: catch (ClassNotFoundException cnfe) { sascha@17: logger.error(cnfe.getLocalizedMessage(), cnfe); sascha@5: } sascha@5: catch (InstantiationException ie) { sascha@17: logger.error(ie.getLocalizedMessage(), ie); sascha@5: } sascha@5: catch (ClassCastException cce) { sascha@17: logger.error(cce.getLocalizedMessage(), cce); sascha@5: } sascha@5: catch (IllegalAccessException iae) { sascha@17: logger.error(iae.getLocalizedMessage(), iae); sascha@5: } sascha@5: sascha@5: if (factory != null) { sascha@19: factory.setup(config, nodes.item(i)); sascha@5: loadedFactories.add(factory); sascha@19: logger.info("Registering '" + factory.getName() + "' as artifact factory."); sascha@5: } sascha@5: } sascha@5: sascha@5: artifactFactories = (ArtifactFactory [])loadedFactories.toArray( sascha@5: new ArtifactFactory[loadedFactories.size()]); sascha@5: } sascha@5: sascha@71: protected void loadServiceFactories() { sascha@71: sascha@71: logger.info("loading service factories"); sascha@71: sascha@71: ArrayList loadedFactories = new ArrayList(); sascha@71: sascha@71: NodeList nodes = Config.getNodeSetXPath(SERVICE_FACTORIES); sascha@71: sascha@71: if (nodes == null) { sascha@71: logger.warn("No factories found"); sascha@71: } sascha@71: sascha@71: Document config = Config.getConfig(); sascha@71: sascha@71: for (int i = 0, N = nodes != null ? nodes.getLength() : 0; i < N; ++i) { sascha@71: String className = nodes.item(i).getTextContent().trim(); sascha@71: sascha@71: ServiceFactory factory = null; sascha@71: sascha@71: try { sascha@71: Class clazz = Class.forName(className); sascha@71: factory = (ServiceFactory)clazz.newInstance(); sascha@71: } sascha@71: catch (ClassNotFoundException cnfe) { sascha@71: logger.error(cnfe.getLocalizedMessage(), cnfe); sascha@71: } sascha@71: catch (InstantiationException ie) { sascha@71: logger.error(ie.getLocalizedMessage(), ie); sascha@71: } sascha@71: catch (ClassCastException cce) { sascha@71: logger.error(cce.getLocalizedMessage(), cce); sascha@71: } sascha@71: catch (IllegalAccessException iae) { sascha@71: logger.error(iae.getLocalizedMessage(), iae); sascha@71: } sascha@71: sascha@71: if (factory != null) { sascha@71: factory.setup(config, nodes.item(i)); sascha@71: loadedFactories.add(factory); sascha@71: logger.info("Registering '" + factory.getName() + "' as service factory."); sascha@71: } sascha@71: } sascha@71: sascha@71: serviceFactories = (ServiceFactory [])loadedFactories.toArray( sascha@71: new ServiceFactory[loadedFactories.size()]); sascha@71: } sascha@71: ingo@79: protected void setupExportSecret() { ingo@79: String secret = Config.getStringXPath(EXPORT_SECRET); ingo@79: ingo@79: if (secret == null) { ingo@79: logger.warn("NO EXPORT SECRET SET! USING INSECURE DEFAULT!"); ingo@79: secret = DEFAULT_EXORT_SECRET; ingo@79: } ingo@79: ingo@79: exportSecret = StringUtils.getUTF8Bytes(secret); ingo@79: } ingo@79: sascha@5: public void boot() { ingo@79: setupExportSecret(); sascha@5: buildContext(); sascha@5: loadArtifactFactories(); sascha@71: loadServiceFactories(); sascha@5: } sascha@5: sascha@5: public ArtifactFactory [] getArtifactFactories() { sascha@5: return artifactFactories; sascha@5: } sascha@5: sascha@70: public ServiceFactory [] getServiceFactories() { sascha@71: return serviceFactories; sascha@70: } sascha@70: sascha@5: public Object getContext() { sascha@5: return context; sascha@5: } ingo@79: ingo@79: public byte [] getExportSecret() { ingo@79: return exportSecret; ingo@79: } sascha@5: } ingo@79: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :