view artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactFactory.java @ 70:ce488c1d3fc4

Serve services over artifact database. artifacts/trunk@597 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 22 Jan 2010 11:08:40 +0000
parents 5e4bc24ea438
children d4c4c23847f5
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 de.intevation.artifacts.ArtifactSerializer;

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, this, 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;
    }

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

http://dive4elements.wald.intevation.org