sascha@10: package de.intevation.artifactdatabase; sascha@10: tim@75: import org.apache.log4j.Logger; sascha@10: import org.w3c.dom.Document; sascha@10: import org.w3c.dom.Node; sascha@10: sascha@10: import de.intevation.artifacts.Artifact; tim@75: import de.intevation.artifacts.ArtifactFactory; sascha@41: import de.intevation.artifacts.ArtifactSerializer; sascha@10: sascha@17: /** sascha@17: * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) 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@10: public static final String XPATH_TTL = "@ttl"; sascha@10: public static final String XPATH_NAME = "@name"; sascha@10: public static final String XPATH_DESCRIPTION = "@description"; sascha@10: public static final String XPATH_ARTIFACT = "@artifact"; sascha@10: sascha@10: public static final String DEFAULT_DESCRIPTION = sascha@10: "No description available"; sascha@10: sascha@10: public static final String DEFAULT_ARTIFACT = sascha@10: "de.intevation.artifactdatabase.DefaultArtifact"; sascha@10: sascha@10: protected Long ttl; sascha@10: sascha@10: protected String name; sascha@10: sascha@10: protected String description; sascha@10: sascha@10: protected Class artifactClass; sascha@10: 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: tim@75: public Artifact createArtifact(String identifier, tim@75: Object context, tim@75: Document data) { sascha@10: sascha@10: try { sascha@10: Artifact artifact = sascha@10: (Artifact)artifactClass.newInstance(); sascha@10: tim@75: artifact.setup(identifier, this, context, 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@10: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: