view artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java @ 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 48d1a9a082c2
line wrap: on
line source
package de.intevation.artifactdatabase;

import de.intevation.artifacts.ArtifactContextFactory;
import de.intevation.artifacts.ArtifactFactory;
import de.intevation.artifacts.ServiceFactory;

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 Sascha L. Teichmann (sascha.teichmann@intevation.de)
 */
public class FactoryBootstrap
{
    private static Logger logger = Logger.getLogger(FactoryBootstrap.class);

    public static final String CONTEXT_FACTORY =
        "/artifact-database/factories/context-factory/text()";

    public static final String DEFAULT_CONTEXT_FACTORY =
        "de.intevation.artifactdatabase.DefaultArtifactContextFactory";

    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() {
    }

    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());
    }

    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()]);
    }

    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() {
        return artifactFactories;
    }

    public ServiceFactory [] getServiceFactories() {
        return serviceFactories;
    }

    public Object getContext() {
        return context;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org