Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/context/GNVArtifactContextFactory.java @ 426:3a0c0ad113d9
Load palettes from configuration.
gnv-artifacts/trunk@474 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 22 Dec 2009 10:34:15 +0000 |
parents | 77cd3a2bc381 |
children | 7399bb8f83ea |
comparison
equal
deleted
inserted
replaced
425:15b8e95fa8da | 426:3a0c0ad113d9 |
---|---|
8 import java.io.IOException; | 8 import java.io.IOException; |
9 import java.io.InputStream; | 9 import java.io.InputStream; |
10 import java.io.File; | 10 import java.io.File; |
11 | 11 |
12 import java.util.Properties; | 12 import java.util.Properties; |
13 import java.util.HashMap; | |
13 | 14 |
14 import org.apache.log4j.Logger; | 15 import org.apache.log4j.Logger; |
16 | |
15 import org.w3c.dom.Document; | 17 import org.w3c.dom.Document; |
16 | 18 import org.w3c.dom.Element; |
17 import de.intevation.artifactdatabase.Config; | 19 import org.w3c.dom.NodeList; |
18 import de.intevation.artifacts.ArtifactContextFactory; | 20 import org.w3c.dom.Node; |
19 import de.intevation.gnv.artifacts.cache.CacheFactory; | 21 |
20 import de.intevation.gnv.geobackend.base.connectionpool.ConnectionPoolFactory; | 22 import de.intevation.gnv.geobackend.base.connectionpool.ConnectionPoolFactory; |
23 | |
21 import de.intevation.gnv.geobackend.base.query.container.QueryContainerFactory; | 24 import de.intevation.gnv.geobackend.base.query.container.QueryContainerFactory; |
22 import de.intevation.gnv.geobackend.base.query.container.exception.QueryContainerException; | 25 import de.intevation.gnv.geobackend.base.query.container.exception.QueryContainerException; |
26 | |
23 import de.intevation.gnv.chart.XMLChartTheme; | 27 import de.intevation.gnv.chart.XMLChartTheme; |
24 | 28 |
29 import de.intevation.gnv.artifacts.cache.CacheFactory; | |
30 | |
31 import de.intevation.gnv.raster.Palette; | |
32 | |
33 import de.intevation.artifacts.ArtifactContextFactory; | |
34 | |
35 import de.intevation.artifactdatabase.Config; | |
25 import de.intevation.artifactdatabase.XMLUtils; | 36 import de.intevation.artifactdatabase.XMLUtils; |
26 | 37 |
27 /** | 38 /** |
28 * @author Tim Englich <tim.englich@intevation.de> | 39 * @author Tim Englich <tim.englich@intevation.de> |
29 * | 40 * |
32 /** | 43 /** |
33 * the logger, used to log exceptions and additonaly information | 44 * the logger, used to log exceptions and additonaly information |
34 */ | 45 */ |
35 private static Logger log = Logger.getLogger(GNVArtifactContext.class); | 46 private static Logger log = Logger.getLogger(GNVArtifactContext.class); |
36 | 47 |
37 public static final String XPATH_GEOBACKEND_CONFIGURATION = "artifact-database/geo-backend/backend-configuration"; | 48 public static final String XPATH_GEOBACKEND_CONFIGURATION = |
38 | 49 "artifact-database/geo-backend/backend-configuration"; |
39 public static final String XPATH_GEOBACKEND_QUERYCONFIGURATION = "artifact-database/geo-backend/query-configuration"; | 50 |
40 | 51 public static final String XPATH_GEOBACKEND_QUERYCONFIGURATION = |
41 private final static String CACHECONFIGNODEPATH = "/artifact-database/ehcache/configuration"; | 52 "artifact-database/geo-backend/query-configuration"; |
42 | 53 |
43 private final static String CHARTCONFIGNODEPATH = "/artifact-database/charttemplate/configuration"; | 54 private final static String CACHECONFIGNODEPATH = |
55 "/artifact-database/ehcache/configuration"; | |
56 | |
57 private final static String CHARTCONFIGNODEPATH = | |
58 "/artifact-database/charttemplate/configuration"; | |
59 | |
60 public final static String PALETTES_PATH = | |
61 "/artifact-database/palettes"; | |
62 | |
63 public final static String PALETTE_ITEMS = | |
64 "palette"; | |
65 | |
66 public final static String PALETTES = | |
67 "color.palettes"; | |
44 | 68 |
45 public final static String CHARTTEMPLATE = "template"; | 69 public final static String CHARTTEMPLATE = "template"; |
46 | 70 |
47 /** | 71 /** |
48 * Constructor | 72 * Constructor |
60 try { | 84 try { |
61 log.debug("GNVArtifactContextFactory.createArtifactContext"); | 85 log.debug("GNVArtifactContextFactory.createArtifactContext"); |
62 log.info("Initialisation of the Geo-BackendConnectionPool"); | 86 log.info("Initialisation of the Geo-BackendConnectionPool"); |
63 String backendConfigurationFile = Config.getStringXPath(config, | 87 String backendConfigurationFile = Config.getStringXPath(config, |
64 XPATH_GEOBACKEND_CONFIGURATION); | 88 XPATH_GEOBACKEND_CONFIGURATION); |
65 backendConfigurationFile = Config.replaceConfigDir(backendConfigurationFile); | 89 backendConfigurationFile = Config.replaceConfigDir( |
90 backendConfigurationFile); | |
66 | 91 |
67 Properties properties = getProperties(backendConfigurationFile); | 92 Properties properties = getProperties(backendConfigurationFile); |
68 ConnectionPoolFactory cpf = ConnectionPoolFactory.getInstance(); | 93 ConnectionPoolFactory cpf = ConnectionPoolFactory.getInstance(); |
69 cpf.initializeConnectionPool(properties); | 94 cpf.initializeConnectionPool(properties); |
70 | 95 |
71 log.info("Initialisation of the QueryContainer"); | 96 log.info("Initialisation of the QueryContainer"); |
72 String queryConfigurationFile = Config.getStringXPath(config, | 97 String queryConfigurationFile = Config.getStringXPath(config, |
73 XPATH_GEOBACKEND_QUERYCONFIGURATION); | 98 XPATH_GEOBACKEND_QUERYCONFIGURATION); |
74 queryConfigurationFile = Config.replaceConfigDir(queryConfigurationFile); | 99 queryConfigurationFile = Config.replaceConfigDir( |
100 queryConfigurationFile); | |
75 | 101 |
76 Properties queryProperties = getProperties(queryConfigurationFile); | 102 Properties queryProperties = getProperties(queryConfigurationFile); |
77 QueryContainerFactory qcf = QueryContainerFactory.getInstance(); | 103 QueryContainerFactory qcf = QueryContainerFactory.getInstance(); |
78 qcf.initializeQueryContainer(queryProperties); | 104 qcf.initializeQueryContainer(queryProperties); |
79 | 105 |
80 log.info("Initialisation of the Cache"); | 106 log.info("Initialisation of the Cache"); |
81 String cacheConfigurationFile = Config.getStringXPath(config, | 107 String cacheConfigurationFile = Config.getStringXPath(config, |
82 CACHECONFIGNODEPATH); | 108 CACHECONFIGNODEPATH); |
83 cacheConfigurationFile = Config.replaceConfigDir(cacheConfigurationFile); | 109 cacheConfigurationFile = Config.replaceConfigDir( |
110 cacheConfigurationFile); | |
111 | |
84 CacheFactory cf = CacheFactory.getInstance(); | 112 CacheFactory cf = CacheFactory.getInstance(); |
85 cf.initializeCache(cacheConfigurationFile); | 113 cf.initializeCache(cacheConfigurationFile); |
86 | 114 |
87 log.info("Initialisation of chart template"); | |
88 String chartConfigFile = Config.getStringXPath( | |
89 config, CHARTCONFIGNODEPATH | |
90 ); | |
91 chartConfigFile = Config.replaceConfigDir(chartConfigFile); | |
92 log.debug("Parse xml configuration of " + chartConfigFile); | |
93 | |
94 Document tmpl = XMLUtils.parseDocument(new File(chartConfigFile)); | |
95 XMLChartTheme theme = new XMLChartTheme("XMLChartTheme"); | |
96 if (tmpl != null) { | |
97 theme.applyXMLConfiguration(tmpl); | |
98 } | |
99 else { | |
100 log.error( | |
101 "Cannot load chart template from '" + | |
102 chartConfigFile + "'"); | |
103 } | |
104 | |
105 returnValue = new GNVArtifactContext(config); | 115 returnValue = new GNVArtifactContext(config); |
106 | 116 |
107 returnValue.put(CHARTTEMPLATE, theme); | 117 configurePalettes(config, returnValue); |
118 | |
119 configureChartTemplate(config, returnValue); | |
120 | |
121 | |
108 } catch (FileNotFoundException e) { | 122 } catch (FileNotFoundException e) { |
109 log.error(e, e); | 123 log.error(e, e); |
110 } catch (IOException e) { | 124 } catch (IOException e) { |
111 log.error(e, e); | 125 log.error(e, e); |
112 } catch (QueryContainerException e) { | 126 } catch (QueryContainerException e) { |
113 log.error(e, e); | 127 log.error(e, e); |
114 } | 128 } |
115 return returnValue; | 129 return returnValue; |
116 } | 130 } |
117 | 131 |
132 protected void configureChartTemplate( | |
133 Document config, | |
134 GNVArtifactContext context | |
135 ) { | |
136 log.info("Initialisation of chart template"); | |
137 String chartConfigFile = Config.getStringXPath( | |
138 config, CHARTCONFIGNODEPATH | |
139 ); | |
140 chartConfigFile = Config.replaceConfigDir(chartConfigFile); | |
141 log.debug("Parse xml configuration of " + chartConfigFile); | |
142 | |
143 Document tmpl = XMLUtils.parseDocument(new File(chartConfigFile)); | |
144 XMLChartTheme theme = new XMLChartTheme("XMLChartTheme"); | |
145 if (tmpl != null) { | |
146 theme.applyXMLConfiguration(tmpl); | |
147 } | |
148 else { | |
149 log.error( | |
150 "Cannot load chart template from '" + | |
151 chartConfigFile + "'"); | |
152 } | |
153 | |
154 context.put(CHARTTEMPLATE, theme); | |
155 } | |
156 | |
157 protected void configurePalettes( | |
158 Document config, | |
159 GNVArtifactContext context | |
160 ) { | |
161 log.info("configure palettes"); | |
162 | |
163 HashMap palettes = new HashMap(); | |
164 | |
165 Node node = Config.getNodeXPath(config, PALETTES_PATH); | |
166 | |
167 if (node == null) { | |
168 log.error("No palettes found"); | |
169 } | |
170 else { | |
171 NodeList pals = Config.getNodeSetXPath(PALETTE_ITEMS); | |
172 for (int i = 0, N = pals == null ? 0 : pals.getLength(); i < N; ++i) { | |
173 Element pal = (Element)pals.item(i); | |
174 String name = pal.getAttribute("name"); | |
175 String description = pal.getAttribute("description"); | |
176 String filename = pal.getAttribute("file"); | |
177 | |
178 if (name == null || name.length() == 0) { | |
179 log.error("Palette has no 'name' attribute."); | |
180 } | |
181 else if (filename == null || filename.length() == 0) { | |
182 log.error("Palette has no 'file' attribute."); | |
183 } | |
184 else { | |
185 filename = Config.replaceConfigDir(filename); | |
186 Document document = XMLUtils.parseDocument( | |
187 new File(filename)); | |
188 if (document == null) { | |
189 log.error("Cannot load palette file '" + | |
190 filename + "'"); | |
191 } | |
192 else { | |
193 Palette p = new Palette(document, description); | |
194 palettes.put(name, p); | |
195 } | |
196 } | |
197 } | |
198 } | |
199 | |
200 context.put(PALETTES, palettes); | |
201 } | |
202 | |
118 /** | 203 /** |
119 * @param filePath | 204 * @param filePath |
120 * @return | 205 * @return |
121 * @throws FileNotFoundException | 206 * @throws FileNotFoundException |
122 * @throws IOException | 207 * @throws IOException |
123 */ | 208 */ |
124 private Properties getProperties(String filePath) | 209 private Properties getProperties(String filePath) |
125 throws FileNotFoundException, | 210 throws FileNotFoundException, IOException |
126 IOException { | 211 { |
127 InputStream inputStream = null; | 212 InputStream inputStream = null; |
128 try { | 213 try { |
129 inputStream = new FileInputStream(filePath); | 214 inputStream = new FileInputStream(filePath); |
130 Properties properties = new Properties(); | 215 Properties properties = new Properties(); |
131 properties.load(inputStream); | 216 properties.load(inputStream); |