ingo@100: /*
ingo@100:  * Copyright (c) 2010 by Intevation GmbH
ingo@100:  *
ingo@100:  * This program is free software under the LGPL (>=v2.1)
ingo@100:  * Read the file LGPL.txt coming with the software for details
ingo@100:  * or visit http://www.gnu.org/licenses/ if it does not exist.
ingo@100:  */
ingo@100: 
sascha@10: package de.intevation.artifactdatabase;
sascha@10: 
sascha@207: import de.intevation.artifacts.common.utils.Config;
sascha@207: 
sascha@10: import de.intevation.artifacts.Artifact;
tim@75: import de.intevation.artifacts.ArtifactFactory;
sascha@41: import de.intevation.artifacts.ArtifactSerializer;
ingo@297: import de.intevation.artifacts.CallMeta;
ingo@293: import de.intevation.artifacts.GlobalContext;
sascha@10: 
sascha@93: import org.apache.log4j.Logger;
sascha@93: 
sascha@93: import org.w3c.dom.Document;
sascha@93: import org.w3c.dom.Node;
sascha@93: 
sascha@17: /**
sascha@90:  * Trivial implementation of the ArtifactFactory interface.
sascha@90:  * Time to live (ttl), name and description are configured
sascha@90:  * via the Node given to #setup(Document, Node) with attributes
sascha@90:  * of same name. The class name of the artifacts to be build by this
sascha@90:  * factory is configures with the attribute 'artifact'.
sascha@90:  *
sascha@77:  * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
sascha@17:  */
sascha@10: public class DefaultArtifactFactory
sascha@10: implements   ArtifactFactory
sascha@10: {
sascha@17:     private static Logger logger =
sascha@17:         Logger.getLogger(DefaultArtifactFactory.class);
sascha@17: 
sascha@90:     /**
sascha@90:      * XPath to access the TTL of this artifact.
sascha@90:      */
sascha@10:     public static final String XPATH_TTL         = "@ttl";
sascha@90:     /**
sascha@90:      * XPath to access the name of this factory.
sascha@90:      */
sascha@10:     public static final String XPATH_NAME        = "@name";
sascha@90:     /**
sascha@90:      * XPath to access the description of this artifact factory.
sascha@90:      */
sascha@10:     public static final String XPATH_DESCRIPTION = "@description";
sascha@90:     /**
sascha@90:      * XPath to access the class name of the artifacts to be build
sascha@90:      * by this factory.
sascha@90:      */
sascha@10:     public static final String XPATH_ARTIFACT    = "@artifact";
sascha@10: 
sascha@90:     /**
sascha@90:      * Default description of this factory if none is given by the
sascha@90:      * configuration.
sascha@90:      */
sascha@10:     public static final String DEFAULT_DESCRIPTION =
sascha@10:         "No description available";
sascha@10: 
sascha@90:     /**
sascha@90:      * Class to load if no artifact class is given in the configuration.
sascha@90:      */
sascha@10:     public static final String DEFAULT_ARTIFACT =
sascha@10:         "de.intevation.artifactdatabase.DefaultArtifact";
sascha@10: 
sascha@90:     /**
sascha@90:      * The Time to live of the artifacts build by this factory.
sascha@90:      */
sascha@10:     protected Long   ttl;
sascha@10: 
sascha@90:     /**
sascha@90:      * The name of this factory.
sascha@90:      */
sascha@10:     protected String name;
sascha@10: 
sascha@90:     /**
sascha@90:      * The description of this factory.
sascha@90:      */
sascha@10:     protected String description;
sascha@10: 
sascha@90:     /**
sascha@90:      * The class of the artifacts to be build by this factory.
sascha@90:      */
sascha@10:     protected Class  artifactClass;
sascha@10: 
sascha@90:     /**
sascha@90:      * Default constructor.
sascha@90:      */
sascha@10:     public DefaultArtifactFactory() {
sascha@10:     }
sascha@10: 
sascha@10:     public String getName() {
sascha@10:         return name;
sascha@10:     }
sascha@10: 
sascha@10:     public String getDescription() {
sascha@10:         return description;
sascha@10:     }
sascha@10: 
sascha@90:     public Artifact createArtifact(
ingo@293:         String        identifier,
ingo@293:         GlobalContext context,
ingo@297:         CallMeta      callMeta,
ingo@293:         Document      data
sascha@90:     ) {
sascha@10:         try {
sascha@10:             Artifact artifact =
sascha@10:                 (Artifact)artifactClass.newInstance();
sascha@10: 
ingo@297:             artifact.setup(identifier, this, context, callMeta, data);
sascha@10: 
sascha@10:             return artifact;
sascha@10:         }
sascha@10:         catch (InstantiationException ie) {
sascha@17:             logger.error(ie.getLocalizedMessage(), ie);
sascha@10:         }
sascha@10:         catch (IllegalAccessException iae) {
sascha@17:             logger.error(iae.getLocalizedMessage(), iae);
sascha@10:         }
sascha@10:         catch (ClassCastException cce) {
sascha@17:             logger.error(cce.getLocalizedMessage(), cce);
sascha@10:         }
sascha@10: 
sascha@10:         return null;
sascha@10:     }
sascha@10: 
sascha@10:     public void setup(Document document, Node factoryNode) {
sascha@10: 
sascha@10:         String ttlString = Config.getStringXPath(factoryNode, XPATH_TTL);
sascha@10:         if (ttlString != null) {
sascha@10:             try {
sascha@10:                 ttl = Long.valueOf(ttlString);
sascha@10:             }
sascha@10:             catch (NumberFormatException nfe) {
sascha@17:                 logger.warn("'" + ttlString + "' is not an integer.");
sascha@10:             }
sascha@10:         }
sascha@10: 
sascha@10:         description = Config.getStringXPath(
sascha@10:             factoryNode, XPATH_DESCRIPTION, DEFAULT_DESCRIPTION);
sascha@10: 
sascha@10:         name = Config.getStringXPath(
sascha@10:             factoryNode, XPATH_NAME, toString());
sascha@10: 
sascha@10:         String artifact = Config.getStringXPath(
sascha@10:             factoryNode, XPATH_ARTIFACT, DEFAULT_ARTIFACT);
sascha@10: 
sascha@10:         try {
sascha@10:             artifactClass = Class.forName(artifact);
sascha@10:         }
sascha@10:         catch (ClassNotFoundException cnfe) {
sascha@17:             logger.error(cnfe.getLocalizedMessage(), cnfe);
sascha@10:         }
sascha@10: 
sascha@10:         if (artifactClass == null) {
sascha@10:             artifactClass = DefaultArtifact.class;
sascha@10:         }
sascha@10:     }
sascha@10: 
sascha@10:     public Long timeToLiveUntouched(Artifact artifact, Object context) {
sascha@10:         return ttl;
sascha@10:     }
sascha@41: 
sascha@41:     public ArtifactSerializer getSerializer() {
sascha@41:         return DefaultArtifactSerializer.INSTANCE;
sascha@41:     }
sascha@10: }
sascha@91: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :