view artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java @ 8:d9aab2df8543

Correct wrong Checkin artifacts/trunk@22 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Fri, 04 Sep 2009 15:20:28 +0000
parents f95d9a449215
children e8626caac353
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;

/**
 * Bootstrap facility for the global context and the artifact factories.
 *
 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
 */
public class FactoryBootstrap
{
    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/text()";

    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) {
            cnfe.printStackTrace(System.err);
        }
        catch (InstantiationException ie) {
            ie.printStackTrace(System.err);
        }
        catch (ClassCastException cce) {
            cce.printStackTrace(System.err);
        }
        catch (IllegalAccessException iae) {
            iae.printStackTrace(System.err);
        }

        if (factory == null) {
            factory = new DefaultArtifactContextFactory();
        }

        context = factory.createArtifactContext(Config.getConfig());
    }

    protected void loadArtifactFactories() {
        ArrayList loadedFactories = new ArrayList();

        NodeList nodes = Config.getNodeSetXPath(ARTIFACT_FACTORIES);

        Document config = Config.getConfig();

        for (int i = 0, N = nodes.getLength(); 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) {
                cnfe.printStackTrace(System.err);
            }
            catch (InstantiationException ie) {
                ie.printStackTrace(System.err);
            }
            catch (ClassCastException cce) {
                cce.printStackTrace(System.err);
            }
            catch (IllegalAccessException iae) {
                iae.printStackTrace(System.err);
            }

            if (factory != null) {
                factory.setup(config,nodes.item(i));
                loadedFactories.add(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