view artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactFactory.java @ 23:00596a591a2f

Added possibibilty to ceate XPaths with namespace contextes. artifacts/trunk@57 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 10 Sep 2009 08:14:03 +0000
parents 5a6b6a3debc7
children d5dc2900392f
line wrap: on
line source
package de.intevation.artifactdatabase;

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

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

import org.apache.log4j.Logger;

/**
 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
 */
public class DefaultArtifactFactory
implements   ArtifactFactory
{
    private static Logger logger =
        Logger.getLogger(DefaultArtifactFactory.class);

    public static final String XPATH_TTL         = "@ttl";
    public static final String XPATH_NAME        = "@name";
    public static final String XPATH_DESCRIPTION = "@description";
    public static final String XPATH_ARTIFACT    = "@artifact";

    public static final String DEFAULT_DESCRIPTION =
        "No description available";

    public static final String DEFAULT_ARTIFACT =
        "de.intevation.artifactdatabase.DefaultArtifact";

    protected Long   ttl;

    protected String name;

    protected String description;

    protected Class  artifactClass;

    public DefaultArtifactFactory() {
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public Artifact createArtifact(String identifier, Object context) {

        try {
            Artifact artifact =
                (Artifact)artifactClass.newInstance();

            artifact.setup(identifier, context);

            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;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org