view artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java @ 37:3f03aee55c2f

Switched from Get to Post communication. artifacts/trunk@96 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Wed, 16 Sep 2009 12:43:03 +0000
parents 1259d192e3c3
children ce488c1d3fc4
line wrap: on
line source
package de.intevation.artifactdatabase;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import java.util.ArrayList;

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

import org.apache.log4j.Logger;

/**
 * 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";

    protected Object context;

    protected ArtifactFactory [] artifactFactories;

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

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

    public void boot() {
        buildContext();
        loadArtifactFactories();
    }

    public ArtifactFactory [] getArtifactFactories() {
        return artifactFactories;
    }

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

http://dive4elements.wald.intevation.org