ingo@122: /* ingo@122: * Copyright (c) 2011 by Intevation GmbH ingo@122: * ingo@122: * This program is free software under the LGPL (>=v2.1) ingo@122: * Read the file LGPL.txt coming with the software for details ingo@122: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@122: */ ingo@122: package de.intevation.artifactdatabase; ingo@122: ingo@122: import org.apache.log4j.Logger; ingo@122: ingo@122: import org.w3c.dom.Document; ingo@122: import org.w3c.dom.Node; ingo@122: ingo@122: import de.intevation.artifacts.ArtifactCollection; ingo@122: import de.intevation.artifacts.ArtifactCollectionFactory; ingo@122: ingo@122: ingo@122: /** ingo@122: * The default implementation of a ArtifactCollectionFactory. ingo@122: * ingo@122: * @author Ingo Weinzierl ingo@122: */ ingo@122: public class DefaultArtifactCollectionFactory ingo@122: implements ArtifactCollectionFactory ingo@122: { ingo@122: /** The logger that is used in this factory.*/ ingo@122: private static Logger logger = ingo@122: Logger.getLogger(DefaultArtifactCollectionFactory.class); ingo@122: ingo@122: /** XPath to access the TTL of this artifact.*/ ingo@122: public static final String XPATH_TTL = "@ttl"; ingo@122: ingo@122: /** XPath to access the name of this factory.*/ ingo@122: public static final String XPATH_NAME = "@name"; ingo@122: ingo@122: /** XPath to access the description of this artifact factory.*/ ingo@122: public static final String XPATH_DESCRIPTION = "@description"; ingo@122: ingo@122: /** ingo@122: * XPath to access the class name of the artifacts to be build ingo@122: * by this factory. ingo@122: */ ingo@122: public static final String XPATH_ARTIFACTCOLLECTION = "@artifact-collection"; ingo@122: ingo@122: /** ingo@122: * Default description of this factory if none is given by the ingo@122: * configuration. ingo@122: */ ingo@122: public static final String DEFAULT_DESCRIPTION = ingo@122: "No description available"; ingo@122: ingo@122: /** ingo@122: * Class to load if no artifact class is given in the configuration. ingo@122: */ ingo@122: public static final String DEFAULT_ARTIFACTCOLLECTION = ingo@122: "de.intevation.artifactdatabase.DefaultArtifact"; ingo@122: ingo@122: ingo@122: /** The name of the factory.*/ ingo@122: protected String name; ingo@122: ingo@122: /** The description of the factory.*/ ingo@122: protected String description; ingo@122: ingo@122: /** The class that is used to instantiate new ArtifactCollection.*/ ingo@122: protected Class clazz; ingo@122: ingo@122: /** The time to live of the artifact collection build by this factory.*/ ingo@122: protected Long ttl; ingo@122: ingo@122: ingo@122: /** ingo@122: * The default constructor. ingo@122: */ ingo@122: public DefaultArtifactCollectionFactory() { ingo@122: } ingo@122: ingo@122: ingo@122: /** ingo@122: * The short name of this factory. ingo@122: * ingo@122: * @return the name of this factory. ingo@122: */ ingo@122: public String getName() { ingo@122: return name; ingo@122: } ingo@122: ingo@122: ingo@122: /** ingo@122: * Description of this factory. ingo@122: * ingo@122: * @return description of the factory. ingo@122: */ ingo@122: public String getDescription() { ingo@122: return description; ingo@122: } ingo@122: ingo@122: ingo@122: /** ingo@122: * Returns the time to live of the given artifact. ingo@122: * ingo@122: * @param artifact ingo@122: */ ingo@122: public Long timeToLiveUntouched( ingo@122: ArtifactCollection collection, ingo@122: Object context) ingo@122: { ingo@122: return ttl; ingo@122: } ingo@122: ingo@122: ingo@122: /** ingo@122: * Create a new artifact of certain type, given a general purpose context and ingo@122: * an identifier. ingo@122: * @param context a context from the ArtifactDatabase. ingo@122: * @param identifier unique identifer for the new artifact ingo@122: * @param data the data containing more details for the setup of an Artifact. ingo@122: * @return a new {@linkplain de.intevation.artifacts.ArtifactCollection ArtifactCollection} ingo@122: */ ingo@122: public ArtifactCollection createCollection( ingo@122: String identifier, ingo@122: Object context, ingo@122: Document data) ingo@122: { ingo@122: try { ingo@122: ArtifactCollection collection = ingo@122: (ArtifactCollection) clazz.newInstance(); ingo@122: ingo@122: collection.setup(identifier, this, context, data); ingo@122: ingo@122: return collection; ingo@122: } ingo@122: catch (InstantiationException ie) { ingo@122: logger.error(ie.getLocalizedMessage(), ie); ingo@122: } ingo@122: catch (IllegalAccessException iae) { ingo@122: logger.error(iae.getLocalizedMessage(), iae); ingo@122: } ingo@122: catch (ClassCastException cce) { ingo@122: logger.error(cce.getLocalizedMessage(), cce); ingo@122: } ingo@122: ingo@122: return null; ingo@122: } ingo@122: ingo@122: /** ingo@122: * Setup the factory with a given configuration ingo@122: * @param config the configuration ingo@122: * @param factoryNode the ConfigurationNode of this Factory ingo@122: */ ingo@122: public void setup(Document config, Node factoryNode) { ingo@122: String ttlString = Config.getStringXPath(factoryNode, XPATH_TTL); ingo@122: if (ttlString != null) { ingo@122: try { ingo@122: ttl = Long.valueOf(ttlString); ingo@122: } ingo@122: catch (NumberFormatException nfe) { ingo@122: logger.warn("'" + ttlString + "' is not an integer."); ingo@122: } ingo@122: } ingo@122: ingo@122: description = Config.getStringXPath( ingo@122: factoryNode, XPATH_DESCRIPTION, DEFAULT_DESCRIPTION); ingo@122: ingo@122: name = Config.getStringXPath(factoryNode, XPATH_NAME, toString()); ingo@122: ingo@122: String artifactCollection = Config.getStringXPath( ingo@122: factoryNode, XPATH_ARTIFACTCOLLECTION, DEFAULT_ARTIFACTCOLLECTION); ingo@122: ingo@122: try { ingo@122: clazz = Class.forName(artifactCollection); ingo@122: } ingo@122: catch (ClassNotFoundException cnfe) { ingo@122: logger.error(cnfe.getLocalizedMessage(), cnfe); ingo@122: } ingo@122: ingo@122: if (clazz == null) { ingo@122: clazz = DefaultArtifactCollection.class; ingo@122: } ingo@122: } ingo@122: } ingo@122: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :