Mercurial > dive4elements > framework
comparison artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java @ 5:8f2de197bce2
Added config to artifact database and modelled bootstap of artifact factories on top of this.
artifacts/trunk@15 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 04 Sep 2009 12:04:12 +0000 |
parents | |
children | f95d9a449215 |
comparison
equal
deleted
inserted
replaced
4:13a12b607baf | 5:8f2de197bce2 |
---|---|
1 package de.intevation.artifactdatabase; | |
2 | |
3 import org.w3c.dom.Document; | |
4 import org.w3c.dom.NodeList; | |
5 | |
6 import java.util.ArrayList; | |
7 | |
8 import de.intevation.artifacts.ArtifactFactory; | |
9 import de.intevation.artifacts.ArtifactContextFactory; | |
10 | |
11 /** | |
12 * Bootstrap facility for the global context and the artifact factories. | |
13 * | |
14 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) | |
15 */ | |
16 public class FactoryBootstrap | |
17 { | |
18 public static final String CONTEXT_FACTORY = | |
19 "/artifact-database/factories/context-factory/text()"; | |
20 | |
21 public static final String DEFAULT_CONTEXT_FACTORY = | |
22 "de.intevation.artifactdatabase.DefaultArtifactContextFactory"; | |
23 | |
24 public static final String ARTIFACT_FACTORIES = | |
25 "/artifact-database/factories/artifact-factories/artifact-factory/text()"; | |
26 | |
27 protected Object context; | |
28 | |
29 protected ArtifactFactory [] artifactFactories; | |
30 | |
31 public FactoryBootstrap() { | |
32 } | |
33 | |
34 void buildContext() { | |
35 String className = Config.getStringXPath( | |
36 CONTEXT_FACTORY, DEFAULT_CONTEXT_FACTORY); | |
37 | |
38 ArtifactContextFactory factory = null; | |
39 | |
40 try { | |
41 Class clazz = Class.forName(className); | |
42 factory = (ArtifactContextFactory)clazz.newInstance(); | |
43 } | |
44 catch (ClassNotFoundException cnfe) { | |
45 cnfe.printStackTrace(System.err); | |
46 } | |
47 catch (InstantiationException ie) { | |
48 ie.printStackTrace(System.err); | |
49 } | |
50 catch (ClassCastException cce) { | |
51 cce.printStackTrace(System.err); | |
52 } | |
53 catch (IllegalAccessException iae) { | |
54 iae.printStackTrace(System.err); | |
55 } | |
56 | |
57 if (factory == null) { | |
58 factory = new DefaultArtifactContextFactory(); | |
59 } | |
60 | |
61 context = factory.createArtifactContext(Config.getConfig()); | |
62 } | |
63 | |
64 protected void loadArtifactFactories() { | |
65 ArrayList loadedFactories = new ArrayList(); | |
66 | |
67 NodeList nodes = Config.getNodeSetXPath(ARTIFACT_FACTORIES); | |
68 | |
69 Document config = Config.getConfig(); | |
70 | |
71 for (int i = 0, N = nodes.getLength(); i < N; ++i) { | |
72 String className = nodes.item(i).getTextContent(); | |
73 | |
74 ArtifactFactory factory = null; | |
75 | |
76 try { | |
77 Class clazz = Class.forName(className); | |
78 factory = (ArtifactFactory)clazz.newInstance(); | |
79 } | |
80 catch (ClassNotFoundException cnfe) { | |
81 cnfe.printStackTrace(System.err); | |
82 } | |
83 catch (InstantiationException ie) { | |
84 ie.printStackTrace(System.err); | |
85 } | |
86 catch (ClassCastException cce) { | |
87 cce.printStackTrace(System.err); | |
88 } | |
89 catch (IllegalAccessException iae) { | |
90 iae.printStackTrace(System.err); | |
91 } | |
92 | |
93 if (factory != null) { | |
94 factory.setup(config); | |
95 loadedFactories.add(factory); | |
96 } | |
97 } | |
98 | |
99 artifactFactories = (ArtifactFactory [])loadedFactories.toArray( | |
100 new ArtifactFactory[loadedFactories.size()]); | |
101 } | |
102 | |
103 public void boot() { | |
104 buildContext(); | |
105 loadArtifactFactories(); | |
106 } | |
107 | |
108 public ArtifactFactory [] getArtifactFactories() { | |
109 return artifactFactories; | |
110 } | |
111 | |
112 public Object getContext() { | |
113 return context; | |
114 } | |
115 } | |
116 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: |