Mercurial > dive4elements > framework
changeset 122:c9cf5f33a230
Added a default implementation of an ArtifactCollectionFactory.
artifacts/trunk@1345 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 01 Mar 2011 16:31:52 +0000 |
parents | 720d65bbba13 |
children | 9187abefba8b |
files | ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactCollectionFactory.java |
diffstat | 2 files changed, 188 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- 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 <ingo@intevation.de> + + * artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactCollectionFactory.java: + A default implementation of an ArtifactCollectionFactory. + 2011-03-01 Sascha L. Teichmann <sascha.teichmann@intevation.de> * artifact-database/doc/schema-pg.sql,
--- /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 <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> + */ +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 :