Mercurial > dive4elements > framework
comparison artifacts-common/src/main/java/de/intevation/artifacts/common/utils/Config.java @ 206:33f58a847fc4
Added a copy of Config in the commons package. TODO: Remove it from artifacts-database.
artifacts/trunk@1475 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 15 Mar 2011 15:25:43 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
205:2a9591f76270 | 206:33f58a847fc4 |
---|---|
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.artifacts.common.utils; | |
10 | |
11 import java.io.File; | |
12 import java.io.IOException; | |
13 | |
14 import javax.xml.namespace.QName; | |
15 | |
16 import javax.xml.parsers.DocumentBuilderFactory; | |
17 import javax.xml.parsers.ParserConfigurationException; | |
18 | |
19 import javax.xml.xpath.XPathConstants; | |
20 | |
21 import org.apache.log4j.Logger; | |
22 | |
23 import org.w3c.dom.Document; | |
24 import org.w3c.dom.Node; | |
25 import org.w3c.dom.NodeList; | |
26 | |
27 import org.xml.sax.SAXException; | |
28 | |
29 /** | |
30 * The central access to the configuration of the artifact database. | |
31 * This class provides some static methods to access the central | |
32 * configuration XML file via XPath. | |
33 * | |
34 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> | |
35 */ | |
36 public final class Config | |
37 { | |
38 private static Logger logger = Logger.getLogger(Config.class); | |
39 | |
40 /** | |
41 * System property name where to find the configuration directory. | |
42 */ | |
43 public static final String CONFIG_DIR = "artifact.database.dir"; | |
44 | |
45 /** | |
46 * Default path to the configuration directory if none | |
47 * was specified by the CONFIG_DIR system property. | |
48 */ | |
49 public static final File CONFIG_DIR_DEFAULT = | |
50 new File(new File(System.getProperty("user.home", | |
51 System.getProperty("user.dir", "."))), ".artifactdb"); | |
52 | |
53 /** | |
54 * Name of the central configuration XML file. | |
55 */ | |
56 public static final String CONFIG_FILE = "conf.xml"; | |
57 | |
58 /** | |
59 * Name of the configuration filename alias to be use | |
60 * within the configuration. This alias is replaced by | |
61 * the real path. | |
62 */ | |
63 public static final String CONFIG_DIR_PLACEHOLDER = | |
64 "${artifacts.config.dir}"; | |
65 | |
66 private static Document config; | |
67 | |
68 private Config() { | |
69 } | |
70 | |
71 /** | |
72 * Singleton access to the central XML configuration document. | |
73 * @return The central XML configuration document. | |
74 */ | |
75 public static synchronized final Document getConfig() { | |
76 if (config == null) { | |
77 config = loadConfig(); | |
78 } | |
79 return config; | |
80 } | |
81 | |
82 /** | |
83 * Returns the path to the configuartion directory. If a path | |
84 * was specified via the CONFIG_DIR system property this one | |
85 * is used. Else it falls back to default configuration path. | |
86 * @return The path to the configuartion directory. | |
87 */ | |
88 public static File getConfigDirectory() { | |
89 String configDirString = System.getProperty(CONFIG_DIR); | |
90 | |
91 File configDir = configDirString != null | |
92 ? new File(configDirString) | |
93 : CONFIG_DIR_DEFAULT; | |
94 | |
95 if (!configDir.isDirectory()) { | |
96 logger.warn("'" + configDir + "' is not a directory."); | |
97 configDir = CONFIG_DIR_DEFAULT; | |
98 } | |
99 | |
100 return configDir; | |
101 } | |
102 | |
103 /** | |
104 * Replaces the CONFIG_DIR_PLACEHOLDER alias with the real path | |
105 * of the configuration directory. | |
106 * @param path The path containing the CONFIG_DIR_PLACEHOLDER placeholder. | |
107 * @return The path where the CONFIG_DIR_PLACEHOLDER placeholders are | |
108 * replaced by the real path name. | |
109 */ | |
110 public static String replaceConfigDir(String path) { | |
111 String configDir = getConfigDirectory().getAbsolutePath(); | |
112 return path.replace(CONFIG_DIR_PLACEHOLDER, configDir); | |
113 } | |
114 | |
115 private static Document loadConfig() { | |
116 | |
117 File configDir = getConfigDirectory(); | |
118 | |
119 File file = new File(configDir, CONFIG_FILE); | |
120 | |
121 if (!file.canRead() && !file.isFile()) { | |
122 logger.error("Cannot read config file '" | |
123 + file + "'."); | |
124 return null; | |
125 } | |
126 | |
127 try { | |
128 DocumentBuilderFactory factory = | |
129 DocumentBuilderFactory.newInstance(); | |
130 factory.setValidating(false); // XXX: This may change in future. | |
131 return factory.newDocumentBuilder().parse(file); | |
132 } | |
133 catch (SAXException se) { | |
134 logger.error(se.getLocalizedMessage(), se); | |
135 } | |
136 catch (ParserConfigurationException pce) { | |
137 logger.error(pce.getLocalizedMessage(), pce); | |
138 } | |
139 catch (IOException ioe) { | |
140 logger.error(ioe.getLocalizedMessage()); | |
141 } | |
142 | |
143 return null; | |
144 } | |
145 | |
146 /** | |
147 * Convenience method to search within a given document tree via XPath. | |
148 * See {@link XMLUtils#xpath(Object, String, QName) } for details. | |
149 * @param root The object which is used as the root of the tree to | |
150 * be searched in. | |
151 * @param query The XPath query. | |
152 * @param returnType The type of the result. | |
153 * @return The result of type 'returnTyp' or null if something went | |
154 * wrong during XPath evaluation. | |
155 */ | |
156 public static final Object getXPath( | |
157 Object root, String query, QName returnType | |
158 ) { | |
159 return XMLUtils.xpath(root, query, returnType); | |
160 } | |
161 | |
162 /** | |
163 * Convenience method to search within the central configuration via XPath. | |
164 * See {@link XMLUtils#xpath(Object, String, QName) } for details. | |
165 * @param query The XPath query. | |
166 * @param returnType The type of the result. | |
167 * @return The result of type 'returnTyp' or null if something went | |
168 * wrong during XPath evaluation. | |
169 */ | |
170 public static final Object getXPath(String query, QName returnType) { | |
171 return XMLUtils.xpath(getConfig(), query, returnType); | |
172 } | |
173 | |
174 /** | |
175 * Convenience method to search for a node list within the central | |
176 * configuation document via XPath. | |
177 * @param query The XPath query. | |
178 * @return The queried node list or null if something went | |
179 * wrong during XPath evaluation. | |
180 */ | |
181 public static final NodeList getNodeSetXPath(String query) { | |
182 return (NodeList)getXPath(query, XPathConstants.NODESET); | |
183 } | |
184 | |
185 /** | |
186 * Convenience method to search for a node within the central | |
187 * configuation document via XPath. | |
188 * @param query The XPath query. | |
189 * @return The queried node or null if something went | |
190 * wrong during XPath evaluation. | |
191 */ | |
192 public static final Node getNodeXPath(String query) { | |
193 return (Node)getXPath(query, XPathConstants.NODE); | |
194 } | |
195 | |
196 /** | |
197 * Convenience method to search for a string within the central | |
198 * configuation document via XPath. | |
199 * @param xpath The XPath query. | |
200 * @return The queried string or null if something went | |
201 * wrong during XPath evaluation. | |
202 */ | |
203 public static final String getStringXPath(String xpath) { | |
204 return getStringXPath(xpath, null); | |
205 } | |
206 | |
207 /** | |
208 * Convenience method to search for a string within the central | |
209 * configuation document via XPath. | |
210 * @param query The XPath query. | |
211 * @param def The string to be returned if the search has no results. | |
212 * @return The queried string or the default value if something went | |
213 * wrong during XPath evaluation. | |
214 */ | |
215 public static final String getStringXPath(String query, String def) { | |
216 String s = (String)getXPath(query, XPathConstants.STRING); | |
217 return s == null || s.length() == 0 | |
218 ? def | |
219 : s; | |
220 } | |
221 | |
222 /** | |
223 * Convenience method to search for a node list within a given tree | |
224 * via XPath. | |
225 * @param root The root of the tree to be searched in. | |
226 * @param query The XPath query. | |
227 * @return The queried node list or null if something went | |
228 * wrong during XPath evaluation. | |
229 */ | |
230 public static final NodeList getNodeSetXPath(Object root, String query) { | |
231 return (NodeList)getXPath(root, query, XPathConstants.NODESET); | |
232 } | |
233 | |
234 /** | |
235 * Convenience method to search for a node within a given tree | |
236 * via XPath. | |
237 * @param root The root of the tree to be searched in. | |
238 * @param query The XPath query. | |
239 * @return The queried node or null if something went | |
240 * wrong during XPath evaluation. | |
241 */ | |
242 public static final Node getNodeXPath(Object root, String query) { | |
243 return (Node)getXPath(root, query, XPathConstants.NODE); | |
244 } | |
245 | |
246 /** | |
247 * Convenience method to search for a string within a given tree | |
248 * via XPath. | |
249 * @param root The root of the tree to be searched in. | |
250 * @param xpath The XPath query. | |
251 * @return The queried string or null if something went | |
252 * wrong during XPath evaluation. | |
253 */ | |
254 public static final String getStringXPath(Object root, String xpath) { | |
255 return getStringXPath(root, xpath, null); | |
256 } | |
257 | |
258 /** | |
259 * Convenience method to search for a string within a given tree | |
260 * via XPath. | |
261 * @param root The root of the tree to be searched in. | |
262 * @param query xpath The XPath query. | |
263 * @param def The string to be returned if the search has no results. | |
264 * @return The queried string or the default value if something went | |
265 * wrong during XPath evaluation. | |
266 */ | |
267 public static final String getStringXPath( | |
268 Object root, String query, String def | |
269 ) { | |
270 String s = (String)getXPath(root, query, XPathConstants.STRING); | |
271 return s == null || s.length() == 0 | |
272 ? def | |
273 : s; | |
274 } | |
275 } | |
276 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |