view artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactFactory.java @ 437:423c6e6b0cac

Added tag flys-2.9.3 for changeset 15179c77aa1d
author Sascha L. Teichmann <teichmann@intevation.de>
date Tue, 16 Oct 2012 15:51:33 +0200
parents 694d818e99b2
children
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 de.intevation.artifacts.CallMeta;
import de.intevation.artifacts.GlobalContext;

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,
        GlobalContext context,
        CallMeta      callMeta,
        Document      data
    ) {
        try {
            Artifact artifact =
                (Artifact)artifactClass.newInstance();

            artifact.setup(identifier, this, context, callMeta, 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