Mercurial > dive4elements > framework
comparison artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactCollectionFactory.java @ 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 | |
children | db0d20440b92 |
comparison
equal
deleted
inserted
replaced
121:720d65bbba13 | 122:c9cf5f33a230 |
---|---|
1 /* | |
2 * Copyright (c) 2011 by Intevation GmbH | |
3 * | |
4 * This program is free software under the LGPL (>=v2.1) | |
5 * Read the file LGPL.txt coming with the software for details | |
6 * or visit http://www.gnu.org/licenses/ if it does not exist. | |
7 */ | |
8 package de.intevation.artifactdatabase; | |
9 | |
10 import org.apache.log4j.Logger; | |
11 | |
12 import org.w3c.dom.Document; | |
13 import org.w3c.dom.Node; | |
14 | |
15 import de.intevation.artifacts.ArtifactCollection; | |
16 import de.intevation.artifacts.ArtifactCollectionFactory; | |
17 | |
18 | |
19 /** | |
20 * The default implementation of a ArtifactCollectionFactory. | |
21 * | |
22 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
23 */ | |
24 public class DefaultArtifactCollectionFactory | |
25 implements ArtifactCollectionFactory | |
26 { | |
27 /** The logger that is used in this factory.*/ | |
28 private static Logger logger = | |
29 Logger.getLogger(DefaultArtifactCollectionFactory.class); | |
30 | |
31 /** XPath to access the TTL of this artifact.*/ | |
32 public static final String XPATH_TTL = "@ttl"; | |
33 | |
34 /** XPath to access the name of this factory.*/ | |
35 public static final String XPATH_NAME = "@name"; | |
36 | |
37 /** XPath to access the description of this artifact factory.*/ | |
38 public static final String XPATH_DESCRIPTION = "@description"; | |
39 | |
40 /** | |
41 * XPath to access the class name of the artifacts to be build | |
42 * by this factory. | |
43 */ | |
44 public static final String XPATH_ARTIFACTCOLLECTION = "@artifact-collection"; | |
45 | |
46 /** | |
47 * Default description of this factory if none is given by the | |
48 * configuration. | |
49 */ | |
50 public static final String DEFAULT_DESCRIPTION = | |
51 "No description available"; | |
52 | |
53 /** | |
54 * Class to load if no artifact class is given in the configuration. | |
55 */ | |
56 public static final String DEFAULT_ARTIFACTCOLLECTION = | |
57 "de.intevation.artifactdatabase.DefaultArtifact"; | |
58 | |
59 | |
60 /** The name of the factory.*/ | |
61 protected String name; | |
62 | |
63 /** The description of the factory.*/ | |
64 protected String description; | |
65 | |
66 /** The class that is used to instantiate new ArtifactCollection.*/ | |
67 protected Class clazz; | |
68 | |
69 /** The time to live of the artifact collection build by this factory.*/ | |
70 protected Long ttl; | |
71 | |
72 | |
73 /** | |
74 * The default constructor. | |
75 */ | |
76 public DefaultArtifactCollectionFactory() { | |
77 } | |
78 | |
79 | |
80 /** | |
81 * The short name of this factory. | |
82 * | |
83 * @return the name of this factory. | |
84 */ | |
85 public String getName() { | |
86 return name; | |
87 } | |
88 | |
89 | |
90 /** | |
91 * Description of this factory. | |
92 * | |
93 * @return description of the factory. | |
94 */ | |
95 public String getDescription() { | |
96 return description; | |
97 } | |
98 | |
99 | |
100 /** | |
101 * Returns the time to live of the given artifact. | |
102 * | |
103 * @param artifact | |
104 */ | |
105 public Long timeToLiveUntouched( | |
106 ArtifactCollection collection, | |
107 Object context) | |
108 { | |
109 return ttl; | |
110 } | |
111 | |
112 | |
113 /** | |
114 * Create a new artifact of certain type, given a general purpose context and | |
115 * an identifier. | |
116 * @param context a context from the ArtifactDatabase. | |
117 * @param identifier unique identifer for the new artifact | |
118 * @param data the data containing more details for the setup of an Artifact. | |
119 * @return a new {@linkplain de.intevation.artifacts.ArtifactCollection ArtifactCollection} | |
120 */ | |
121 public ArtifactCollection createCollection( | |
122 String identifier, | |
123 Object context, | |
124 Document data) | |
125 { | |
126 try { | |
127 ArtifactCollection collection = | |
128 (ArtifactCollection) clazz.newInstance(); | |
129 | |
130 collection.setup(identifier, this, context, data); | |
131 | |
132 return collection; | |
133 } | |
134 catch (InstantiationException ie) { | |
135 logger.error(ie.getLocalizedMessage(), ie); | |
136 } | |
137 catch (IllegalAccessException iae) { | |
138 logger.error(iae.getLocalizedMessage(), iae); | |
139 } | |
140 catch (ClassCastException cce) { | |
141 logger.error(cce.getLocalizedMessage(), cce); | |
142 } | |
143 | |
144 return null; | |
145 } | |
146 | |
147 /** | |
148 * Setup the factory with a given configuration | |
149 * @param config the configuration | |
150 * @param factoryNode the ConfigurationNode of this Factory | |
151 */ | |
152 public void setup(Document config, Node factoryNode) { | |
153 String ttlString = Config.getStringXPath(factoryNode, XPATH_TTL); | |
154 if (ttlString != null) { | |
155 try { | |
156 ttl = Long.valueOf(ttlString); | |
157 } | |
158 catch (NumberFormatException nfe) { | |
159 logger.warn("'" + ttlString + "' is not an integer."); | |
160 } | |
161 } | |
162 | |
163 description = Config.getStringXPath( | |
164 factoryNode, XPATH_DESCRIPTION, DEFAULT_DESCRIPTION); | |
165 | |
166 name = Config.getStringXPath(factoryNode, XPATH_NAME, toString()); | |
167 | |
168 String artifactCollection = Config.getStringXPath( | |
169 factoryNode, XPATH_ARTIFACTCOLLECTION, DEFAULT_ARTIFACTCOLLECTION); | |
170 | |
171 try { | |
172 clazz = Class.forName(artifactCollection); | |
173 } | |
174 catch (ClassNotFoundException cnfe) { | |
175 logger.error(cnfe.getLocalizedMessage(), cnfe); | |
176 } | |
177 | |
178 if (clazz == null) { | |
179 clazz = DefaultArtifactCollection.class; | |
180 } | |
181 } | |
182 } | |
183 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |