view artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java @ 79:f69e5b87f05f

Implementation to export artifacts as xml (applied patch from issue208 by SLT). artifacts/trunk@792 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 16 Mar 2010 16:03:06 +0000
parents 48d1a9a082c2
children d348fe1fd822
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 <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
 */
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";

    public static final String EXPORT_SECRET =
        "/artifact-database/export-secret/text()";

    public static final String DEFAULT_EXORT_SECRET =
        "!!!CHANGE ME! I'M NO SECRET!!!";

    protected Object context;

    protected ArtifactFactory [] artifactFactories;

    protected ServiceFactory [] serviceFactories;

    protected byte [] exportSecret;


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

    protected void setupExportSecret() {
        String secret = Config.getStringXPath(EXPORT_SECRET);

        if (secret == null) {
            logger.warn("NO EXPORT SECRET SET! USING INSECURE DEFAULT!");
            secret = DEFAULT_EXORT_SECRET;
        }

        exportSecret = StringUtils.getUTF8Bytes(secret);
    }

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

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

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

    public Object getContext() {
        return context;
    }

    public byte [] getExportSecret() {
        return exportSecret;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org