comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/DefaultArtifactFactory.java @ 513:69f99bdf3d65

Use a list of "static" facets loaded from configuration to generate facets on artifact setup.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 30 Apr 2014 15:05:54 +0200
parents 415df0fc4fa1
children 863f1fa66981
comparison
equal deleted inserted replaced
512:ff79b8df9aa6 513:69f99bdf3d65
6 * or visit http://www.gnu.org/licenses/ if it does not exist. 6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */ 7 */
8 8
9 package org.dive4elements.artifactdatabase; 9 package org.dive4elements.artifactdatabase;
10 10
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15
16 import javax.xml.xpath.XPathConstants;
17
11 import org.dive4elements.artifacts.common.utils.Config; 18 import org.dive4elements.artifacts.common.utils.Config;
19 import org.dive4elements.artifacts.common.utils.XMLUtils;
12 20
13 import org.dive4elements.artifacts.Artifact; 21 import org.dive4elements.artifacts.Artifact;
14 import org.dive4elements.artifacts.ArtifactFactory; 22 import org.dive4elements.artifacts.ArtifactFactory;
15 import org.dive4elements.artifacts.ArtifactSerializer; 23 import org.dive4elements.artifacts.ArtifactSerializer;
16 import org.dive4elements.artifacts.CallMeta; 24 import org.dive4elements.artifacts.CallMeta;
18 26
19 import org.apache.log4j.Logger; 27 import org.apache.log4j.Logger;
20 28
21 import org.w3c.dom.Document; 29 import org.w3c.dom.Document;
22 import org.w3c.dom.Node; 30 import org.w3c.dom.Node;
31 import org.w3c.dom.NodeList;
23 32
24 /** 33 /**
25 * Trivial implementation of the ArtifactFactory interface. 34 * Trivial implementation of the ArtifactFactory interface.
26 * Time to live (ttl), name and description are configured 35 * Time to live (ttl), name and description are configured
27 * via the Node given to #setup(Document, Node) with attributes 36 * via the Node given to #setup(Document, Node) with attributes
51 /** 60 /**
52 * XPath to access the class name of the artifacts to be build 61 * XPath to access the class name of the artifacts to be build
53 * by this factory. 62 * by this factory.
54 */ 63 */
55 public static final String XPATH_ARTIFACT = "@artifact"; 64 public static final String XPATH_ARTIFACT = "@artifact";
65 /**
66 * XPath to access the static facets configured for artifacts
67 */
68 public static final String XPATH_ARTIFACT_CONFIG =
69 "/artifact-database/artifacts/artifact[@name=$name]/load-facets/facet";
70
71 /**
72 * XPath to access the static facets configured for artifacts
73 */
74 public static final String XPATH_ARTIFACT_NAME =
75 "/artifact-database/artifacts/artifact/@name";
56 76
57 /** 77 /**
58 * Default description of this factory if none is given by the 78 * Default description of this factory if none is given by the
59 * configuration. 79 * configuration.
60 */ 80 */
86 * The class of the artifacts to be build by this factory. 106 * The class of the artifacts to be build by this factory.
87 */ 107 */
88 protected Class artifactClass; 108 protected Class artifactClass;
89 109
90 /** 110 /**
111 * The name of the artifacts to be build by this factory.
112 */
113 protected String artifactName;
114
115 /**
116 * The list of facets the generated artifact creates on instantiation.
117 */
118 protected List<Class> facetClasses;
119
120 /**
91 * Default constructor. 121 * Default constructor.
92 */ 122 */
93 public DefaultArtifactFactory() { 123 public DefaultArtifactFactory() {
124 facetClasses = new ArrayList<Class>();
94 } 125 }
95 126
96 public String getName() { 127 public String getName() {
97 return name; 128 return name;
98 } 129 }
109 ) { 140 ) {
110 try { 141 try {
111 Artifact artifact = 142 Artifact artifact =
112 (Artifact)artifactClass.newInstance(); 143 (Artifact)artifactClass.newInstance();
113 144
114 artifact.setup(identifier, this, context, callMeta, data); 145 if (artifact.getName() == null ||
146 artifact.getName().length() == 0) {
147 artifact.setName(artifactName);
148 }
149 artifact.setup(
150 identifier,
151 this,
152 context,
153 callMeta,
154 data,
155 facetClasses);
115 156
116 return artifact; 157 return artifact;
117 } 158 }
118 catch (InstantiationException ie) { 159 catch (InstantiationException ie) {
119 logger.error(ie.getLocalizedMessage(), ie); 160 logger.error(ie.getLocalizedMessage(), ie);
127 168
128 return null; 169 return null;
129 } 170 }
130 171
131 public void setup(Document document, Node factoryNode) { 172 public void setup(Document document, Node factoryNode) {
132
133 String ttlString = Config.getStringXPath(factoryNode, XPATH_TTL); 173 String ttlString = Config.getStringXPath(factoryNode, XPATH_TTL);
134 if (ttlString != null) { 174 if (ttlString != null) {
135 try { 175 try {
136 ttl = Long.valueOf(ttlString); 176 ttl = Long.valueOf(ttlString);
137 } 177 }
143 description = Config.getStringXPath( 183 description = Config.getStringXPath(
144 factoryNode, XPATH_DESCRIPTION, DEFAULT_DESCRIPTION); 184 factoryNode, XPATH_DESCRIPTION, DEFAULT_DESCRIPTION);
145 185
146 name = Config.getStringXPath( 186 name = Config.getStringXPath(
147 factoryNode, XPATH_NAME, toString()); 187 factoryNode, XPATH_NAME, toString());
188 logger.debug("setting up " + name);
148 189
149 String artifact = Config.getStringXPath( 190 String artifact = Config.getStringXPath(
150 factoryNode, XPATH_ARTIFACT, DEFAULT_ARTIFACT); 191 factoryNode, XPATH_ARTIFACT, DEFAULT_ARTIFACT);
151 192
193 artifactName = Config.getStringXPath(
194 document, XPATH_ARTIFACT_NAME, "default");
195 logger.debug(artifactName);
196 Map<String, String> variables = new HashMap<String, String>();
197 variables.put("name", name);
198 NodeList facets = (NodeList) XMLUtils.xpath(
199 document,
200 XPATH_ARTIFACT_CONFIG,
201 XPathConstants.NODESET,
202 null,
203 variables);
204
205 for (int i = 0; i < facets.getLength(); i++) {
206 logger.debug(facets.item(i).getAttributes().getNamedItem("class").getNodeValue());
207 String className =
208 facets.item(i).getAttributes().getNamedItem("class").getNodeValue();
209 try {
210 facetClasses.add(Class.forName(className));
211 }
212 catch (ClassNotFoundException cnfe) {
213 logger.error(cnfe.getLocalizedMessage(), cnfe);
214 }
215 }
152 try { 216 try {
153 artifactClass = Class.forName(artifact); 217 artifactClass = Class.forName(artifact);
154 } 218 }
155 catch (ClassNotFoundException cnfe) { 219 catch (ClassNotFoundException cnfe) {
156 logger.error(cnfe.getLocalizedMessage(), cnfe); 220 logger.error(cnfe.getLocalizedMessage(), cnfe);

http://dive4elements.wald.intevation.org