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