comparison geo-backend/src/main/java/de/intevation/gnv/geobackend/config/Configuration.java @ 1122:1985d5db0feb

Implemented a global configuration that should be used to initialize the geobackend. geo-backend/trunk@1149 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 03 Jun 2010 13:31:07 +0000
parents
children ebeb56428409
comparison
equal deleted inserted replaced
1121:9d530f913729 1122:1985d5db0feb
1 package de.intevation.gnv.geobackend.config;
2
3 import de.intevation.gnv.geobackend.base.connectionpool.ConnectionPoolFactory;
4
5 import de.intevation.gnv.geobackend.base.query.container.QueryContainerFactory;
6
7 import de.intevation.gnv.geobackend.base.query.container.exception.QueryContainerException;
8
9 import de.intevation.gnv.geobackend.util.XMLUtils;
10
11 import java.io.FileInputStream;
12 import java.io.FileNotFoundException;
13 import java.io.IOException;
14 import java.io.InputStream;
15
16 import java.util.Properties;
17
18 import javax.xml.xpath.XPathConstants;
19
20 import org.apache.log4j.Logger;
21
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Node;
24 import org.w3c.dom.NodeList;
25
26 /**
27 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
28 */
29 public final class Configuration {
30
31 public static final String CONFIGURATION_ROOT = "geo-backend";
32 public static final String XPATH_QUERIES = "query-configuration";
33 public static final String XPATH_BACKEND = "backend-configuration";
34 public static final String XPATH_CACHE = "cache";
35 public static final String XPATH_CACHE_ENABLED = "@enabled";
36 public static final String XPATH_CACHE_CONFIG = "configuration";
37
38 private static Logger logger = Logger.getLogger(Configuration.class);
39
40 private static Configuration instance;
41
42 private Node config;
43 private String configDir;
44 private String placeholder;
45
46 private boolean cacheEnabled;
47 private String cacheConfiguration;
48
49
50
51 /**
52 * Returns an instance of Configuration.
53 *
54 * @return an instance of Configuration.
55 */
56 public static Configuration getInstance() {
57 if (instance == null)
58 instance = new Configuration();
59
60 return instance;
61 }
62
63
64 /**
65 * Constructor that creates a new Configuration object with disabled cache.
66 */
67 public Configuration() {
68 cacheEnabled = false;
69 }
70
71
72 /**
73 * Initialize the geo-backend before it is ready to be used. This method
74 * calls other <i>init*</i> methods.
75 *
76 * @param conf A configuration document. This document should contain a node
77 * <i>geo-backend</i>. If there are more nodes named <i>geo-backend</i>, the
78 * first node is used.
79 * @param configDir The path to the root configuration directory.
80 * @param placeholder The placeholder used in the configuration document for
81 * the root configuration directory.
82 */
83 public void init(Document conf, String configDir, String placeholder)
84 throws QueryContainerException, FileNotFoundException, IOException
85 {
86 this.config = conf;
87 this.configDir = configDir;
88 this.placeholder = placeholder;
89
90 NodeList root = conf.getElementsByTagName(CONFIGURATION_ROOT);
91 if (root == null || root.getLength() == 0) {
92 logger.error("No valid configuration for this geobackend given!");
93 return;
94 }
95
96 initQueries(root.item(0));
97 initConnection(root.item(0));
98 initCache(root.item(0));
99 }
100
101
102 /**
103 * Initialize sql statements.
104 *
105 * @param conf The geo-backend configuration node.
106 */
107 protected void initQueries(Node conf)
108 throws FileNotFoundException, IOException, QueryContainerException
109 {
110 String queriesFile = (String) XMLUtils.xpath(
111 conf, XPATH_QUERIES, XPathConstants.STRING, null);
112
113 queriesFile = replaceConfigDir(queriesFile);
114 logger.info("Initialize queries: " + queriesFile);
115
116 Properties queries = getProperties(queriesFile);
117 QueryContainerFactory qcf = QueryContainerFactory.getInstance();
118 qcf.initializeQueryContainer(queries);
119 }
120
121
122 /**
123 * Initialize necessary objects used for the database connection.
124 *
125 * @param connection The geo-backend configuration node.
126 */
127 protected void initConnection(Node connection)
128 throws FileNotFoundException, IOException
129 {
130 String config = (String)XMLUtils.xpath(
131 connection, XPATH_BACKEND, XPathConstants.STRING, null);
132
133 config = replaceConfigDir(config);
134 logger.info("Initialize database connection: " + config);
135
136 Properties properties = getProperties(config);
137 ConnectionPoolFactory cpf = ConnectionPoolFactory.getInstance();
138 cpf.initializeConnectionPool(properties);
139 }
140
141
142 /**
143 * Initialize necessary objects used for the sql cache.
144 *
145 * @param conf The geo-backend configuration node.
146 */
147 protected void initCache(Node conf) {
148 Node cache = (Node) XMLUtils.xpath(
149 conf, XPATH_CACHE, XPathConstants.NODE, null);
150
151 String on = (String) XMLUtils.xpath(
152 cache, XPATH_CACHE_ENABLED, XPathConstants.STRING, null);
153
154 boolean enabled = Boolean.parseBoolean(on);
155
156 if (enabled) {
157 String config = (String) XMLUtils.xpath(
158 cache, XPATH_CACHE_CONFIG, XPathConstants.STRING, null);
159
160 if (config != null && config.length() > 0) {
161 config = replaceConfigDir(config);
162 logger.info("Initialize sql cache with config: " + config);
163
164 this.cacheConfiguration = config;
165 this.cacheEnabled = true;
166 }
167 else {
168 logger.error("SQL cache is enabled, " +
169 "but no configuration was found.");
170 }
171 }
172 else {
173 logger.info("SQL cache is disabled.");
174 }
175 }
176
177
178 /**
179 * Replace placeholder in the configuration. Placeholder are used for the
180 * base configuration directory.
181 *
182 * @param path A string that might contain placeholders.
183 *
184 * @return <i>path</i> with replaced placeholder.
185 */
186 protected String replaceConfigDir(String path) {
187 return path.replace(placeholder, configDir);
188 }
189
190
191 /**
192 * Read a file that contains properties and return a Java Properties object.
193 *
194 * @param path Path to a properties file.
195 *
196 * @return the properties contained in the file.
197 */
198 protected Properties getProperties(String path)
199 throws FileNotFoundException, IOException
200 {
201 InputStream inputStream = null;
202
203 try {
204 inputStream = new FileInputStream(path);
205 Properties properties = new Properties();
206 properties.load(inputStream);
207
208 return properties;
209 }
210 finally {
211 if (inputStream != null) {
212 try {
213 inputStream.close();
214 }
215 catch (IOException ioe) {
216 }
217 }
218 }
219 }
220
221
222 /**
223 * Returns the state of the cache.
224 *
225 * @return true if sql queries are cache, otherwise false.
226 */
227 public boolean isCacheEnabled() {
228 return cacheEnabled;
229 }
230
231
232 /**
233 * Returns the path of the cache configuration file.
234 *
235 * @return configuration path.
236 */
237 public String getCacheConfiguration() {
238 return cacheConfiguration;
239 }
240 }
241 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org