comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/DefaultArtifactFactory.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/DefaultArtifactFactory.java@694d818e99b2
children 415df0fc4fa1
comparison
equal deleted inserted replaced
472:783cc1b6b615 473:d0ac790a6c89
1 /*
2 * Copyright (c) 2010 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
9 package de.intevation.artifactdatabase;
10
11 import de.intevation.artifacts.common.utils.Config;
12
13 import de.intevation.artifacts.Artifact;
14 import de.intevation.artifacts.ArtifactFactory;
15 import de.intevation.artifacts.ArtifactSerializer;
16 import de.intevation.artifacts.CallMeta;
17 import de.intevation.artifacts.GlobalContext;
18
19 import org.apache.log4j.Logger;
20
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Node;
23
24 /**
25 * Trivial implementation of the ArtifactFactory interface.
26 * Time to live (ttl), name and description are configured
27 * via the Node given to #setup(Document, Node) with attributes
28 * of same name. The class name of the artifacts to be build by this
29 * factory is configures with the attribute 'artifact'.
30 *
31 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
32 */
33 public class DefaultArtifactFactory
34 implements ArtifactFactory
35 {
36 private static Logger logger =
37 Logger.getLogger(DefaultArtifactFactory.class);
38
39 /**
40 * XPath to access the TTL of this artifact.
41 */
42 public static final String XPATH_TTL = "@ttl";
43 /**
44 * XPath to access the name of this factory.
45 */
46 public static final String XPATH_NAME = "@name";
47 /**
48 * XPath to access the description of this artifact factory.
49 */
50 public static final String XPATH_DESCRIPTION = "@description";
51 /**
52 * XPath to access the class name of the artifacts to be build
53 * by this factory.
54 */
55 public static final String XPATH_ARTIFACT = "@artifact";
56
57 /**
58 * Default description of this factory if none is given by the
59 * configuration.
60 */
61 public static final String DEFAULT_DESCRIPTION =
62 "No description available";
63
64 /**
65 * Class to load if no artifact class is given in the configuration.
66 */
67 public static final String DEFAULT_ARTIFACT =
68 "de.intevation.artifactdatabase.DefaultArtifact";
69
70 /**
71 * The Time to live of the artifacts build by this factory.
72 */
73 protected Long ttl;
74
75 /**
76 * The name of this factory.
77 */
78 protected String name;
79
80 /**
81 * The description of this factory.
82 */
83 protected String description;
84
85 /**
86 * The class of the artifacts to be build by this factory.
87 */
88 protected Class artifactClass;
89
90 /**
91 * Default constructor.
92 */
93 public DefaultArtifactFactory() {
94 }
95
96 public String getName() {
97 return name;
98 }
99
100 public String getDescription() {
101 return description;
102 }
103
104 public Artifact createArtifact(
105 String identifier,
106 GlobalContext context,
107 CallMeta callMeta,
108 Document data
109 ) {
110 try {
111 Artifact artifact =
112 (Artifact)artifactClass.newInstance();
113
114 artifact.setup(identifier, this, context, callMeta, data);
115
116 return artifact;
117 }
118 catch (InstantiationException ie) {
119 logger.error(ie.getLocalizedMessage(), ie);
120 }
121 catch (IllegalAccessException iae) {
122 logger.error(iae.getLocalizedMessage(), iae);
123 }
124 catch (ClassCastException cce) {
125 logger.error(cce.getLocalizedMessage(), cce);
126 }
127
128 return null;
129 }
130
131 public void setup(Document document, Node factoryNode) {
132
133 String ttlString = Config.getStringXPath(factoryNode, XPATH_TTL);
134 if (ttlString != null) {
135 try {
136 ttl = Long.valueOf(ttlString);
137 }
138 catch (NumberFormatException nfe) {
139 logger.warn("'" + ttlString + "' is not an integer.");
140 }
141 }
142
143 description = Config.getStringXPath(
144 factoryNode, XPATH_DESCRIPTION, DEFAULT_DESCRIPTION);
145
146 name = Config.getStringXPath(
147 factoryNode, XPATH_NAME, toString());
148
149 String artifact = Config.getStringXPath(
150 factoryNode, XPATH_ARTIFACT, DEFAULT_ARTIFACT);
151
152 try {
153 artifactClass = Class.forName(artifact);
154 }
155 catch (ClassNotFoundException cnfe) {
156 logger.error(cnfe.getLocalizedMessage(), cnfe);
157 }
158
159 if (artifactClass == null) {
160 artifactClass = DefaultArtifact.class;
161 }
162 }
163
164 public Long timeToLiveUntouched(Artifact artifact, Object context) {
165 return ttl;
166 }
167
168 public ArtifactSerializer getSerializer() {
169 return DefaultArtifactSerializer.INSTANCE;
170 }
171 }
172 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org