# HG changeset patch # User Ingo Weinzierl # Date 1298997112 0 # Node ID c9cf5f33a2306f58f4eac4c401d5607303a575b3 # Parent 720d65bbba13aa456d6e21b9c6a6346faeb8bd0d Added a default implementation of an ArtifactCollectionFactory. artifacts/trunk@1345 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 720d65bbba13 -r c9cf5f33a230 ChangeLog --- a/ChangeLog Tue Mar 01 16:26:27 2011 +0000 +++ b/ChangeLog Tue Mar 01 16:31:52 2011 +0000 @@ -1,3 +1,8 @@ +2011-03-01 Ingo Weinzierl + + * artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactCollectionFactory.java: + A default implementation of an ArtifactCollectionFactory. + 2011-03-01 Sascha L. Teichmann * artifact-database/doc/schema-pg.sql, diff -r 720d65bbba13 -r c9cf5f33a230 artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactCollectionFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactCollectionFactory.java Tue Mar 01 16:31:52 2011 +0000 @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2011 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 org.apache.log4j.Logger; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; + +import de.intevation.artifacts.ArtifactCollection; +import de.intevation.artifacts.ArtifactCollectionFactory; + + +/** + * The default implementation of a ArtifactCollectionFactory. + * + * @author Ingo Weinzierl + */ +public class DefaultArtifactCollectionFactory +implements ArtifactCollectionFactory +{ + /** The logger that is used in this factory.*/ + private static Logger logger = + Logger.getLogger(DefaultArtifactCollectionFactory.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_ARTIFACTCOLLECTION = "@artifact-collection"; + + /** + * 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_ARTIFACTCOLLECTION = + "de.intevation.artifactdatabase.DefaultArtifact"; + + + /** The name of the factory.*/ + protected String name; + + /** The description of the factory.*/ + protected String description; + + /** The class that is used to instantiate new ArtifactCollection.*/ + protected Class clazz; + + /** The time to live of the artifact collection build by this factory.*/ + protected Long ttl; + + + /** + * The default constructor. + */ + public DefaultArtifactCollectionFactory() { + } + + + /** + * The short name of this factory. + * + * @return the name of this factory. + */ + public String getName() { + return name; + } + + + /** + * Description of this factory. + * + * @return description of the factory. + */ + public String getDescription() { + return description; + } + + + /** + * Returns the time to live of the given artifact. + * + * @param artifact + */ + public Long timeToLiveUntouched( + ArtifactCollection collection, + Object context) + { + return ttl; + } + + + /** + * Create a new artifact of certain type, given a general purpose context and + * an identifier. + * @param context a context from the ArtifactDatabase. + * @param identifier unique identifer for the new artifact + * @param data the data containing more details for the setup of an Artifact. + * @return a new {@linkplain de.intevation.artifacts.ArtifactCollection ArtifactCollection} + */ + public ArtifactCollection createCollection( + String identifier, + Object context, + Document data) + { + try { + ArtifactCollection collection = + (ArtifactCollection) clazz.newInstance(); + + collection.setup(identifier, this, context, data); + + return collection; + } + 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; + } + + /** + * Setup the factory with a given configuration + * @param config the configuration + * @param factoryNode the ConfigurationNode of this Factory + */ + public void setup(Document config, 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 artifactCollection = Config.getStringXPath( + factoryNode, XPATH_ARTIFACTCOLLECTION, DEFAULT_ARTIFACTCOLLECTION); + + try { + clazz = Class.forName(artifactCollection); + } + catch (ClassNotFoundException cnfe) { + logger.error(cnfe.getLocalizedMessage(), cnfe); + } + + if (clazz == null) { + clazz = DefaultArtifactCollection.class; + } + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :