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