view artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java @ 15:9ad6ec2d09c3

Implemented restoring artifacts from database. artifacts/trunk@30 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 07 Sep 2009 10:06:23 +0000
parents e8626caac353
children 635310c6a20e
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);

        if (nodes == null) {
            System.err.println("ERROR: no factories found");
            return;
        }

        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