view artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactFactory.java @ 271:aa9f2f5ff032

flys/issue75: fixed doc and schema to make H2 work again. artifacts/trunk@2059 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 06 Jun 2011 14:00:38 +0000
parents b2115f484edb
children a367a0d011af
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.artifactdatabase;

import de.intevation.artifacts.common.utils.Config;

import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.ArtifactFactory;
import de.intevation.artifacts.ArtifactSerializer;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
 * Trivial implementation of the ArtifactFactory interface.
 * Time to live (ttl), name and description are configured
 * via the Node given to #setup(Document, Node) with attributes
 * of same name. The class name of the artifacts to be build by this
 * factory is configures with the attribute 'artifact'.
 *
 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
 */
public class DefaultArtifactFactory
implements   ArtifactFactory
{
    private static Logger logger =
        Logger.getLogger(DefaultArtifactFactory.class);

    /**
     * XPath to access the TTL of this artifact.
     */
    public static final String XPATH_TTL         = "@ttl";
    /**
     * XPath to access the name of this factory.
     */
    public static final String XPATH_NAME        = "@name";
    /**
     * XPath to access the description of this artifact factory.
     */
    public static final String XPATH_DESCRIPTION = "@description";
    /**
     * XPath to access the class name of the artifacts to be build
     * by this factory.
     */
    public static final String XPATH_ARTIFACT    = "@artifact";

    /**
     * Default description of this factory if none is given by the
     * configuration.
     */
    public static final String DEFAULT_DESCRIPTION =
        "No description available";

    /**
     * Class to load if no artifact class is given in the configuration.
     */
    public static final String DEFAULT_ARTIFACT =
        "de.intevation.artifactdatabase.DefaultArtifact";

    /**
     * The Time to live of the artifacts build by this factory.
     */
    protected Long   ttl;

    /**
     * The name of this factory.
     */
    protected String name;

    /**
     * The description of this factory.
     */
    protected String description;

    /**
     * The class of the artifacts to be build by this factory.
     */
    protected Class  artifactClass;

    /**
     * Default constructor.
     */
    public DefaultArtifactFactory() {
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public Artifact createArtifact(
        String   identifier,
        Object   context,
        Document data
    ) {
        try {
            Artifact artifact =
                (Artifact)artifactClass.newInstance();

            artifact.setup(identifier, this, context, data);

            return artifact;
        }
        catch (InstantiationException ie) {
            logger.error(ie.getLocalizedMessage(), ie);
        }
        catch (IllegalAccessException iae) {
            logger.error(iae.getLocalizedMessage(), iae);
        }
        catch (ClassCastException cce) {
            logger.error(cce.getLocalizedMessage(), cce);
        }

        return null;
    }

    public void setup(Document document, Node factoryNode) {

        String ttlString = Config.getStringXPath(factoryNode, XPATH_TTL);
        if (ttlString != null) {
            try {
                ttl = Long.valueOf(ttlString);
            }
            catch (NumberFormatException nfe) {
                logger.warn("'" + ttlString + "' is not an integer.");
            }
        }

        description = Config.getStringXPath(
            factoryNode, XPATH_DESCRIPTION, DEFAULT_DESCRIPTION);

        name = Config.getStringXPath(
            factoryNode, XPATH_NAME, toString());

        String artifact = Config.getStringXPath(
            factoryNode, XPATH_ARTIFACT, DEFAULT_ARTIFACT);

        try {
            artifactClass = Class.forName(artifact);
        }
        catch (ClassNotFoundException cnfe) {
            logger.error(cnfe.getLocalizedMessage(), cnfe);
        }

        if (artifactClass == null) {
            artifactClass = DefaultArtifact.class;
        }
    }

    public Long timeToLiveUntouched(Artifact artifact, Object context) {
        return ttl;
    }

    public ArtifactSerializer getSerializer() {
        return DefaultArtifactSerializer.INSTANCE;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org