1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.impl.config.property; |
18 | |
|
19 | |
import org.apache.commons.collections.CollectionUtils; |
20 | |
import org.apache.commons.lang.StringUtils; |
21 | |
import org.apache.log4j.Logger; |
22 | |
import org.kuali.rice.core.api.config.ConfigurationException; |
23 | |
import org.kuali.rice.core.api.config.property.Config; |
24 | |
import org.kuali.rice.core.util.ImmutableProperties; |
25 | |
import org.kuali.rice.core.util.RiceUtilities; |
26 | |
import org.xml.sax.Attributes; |
27 | |
import org.xml.sax.InputSource; |
28 | |
import org.xml.sax.SAXException; |
29 | |
import org.xml.sax.XMLFilter; |
30 | |
import org.xml.sax.helpers.XMLFilterImpl; |
31 | |
|
32 | |
import javax.xml.bind.JAXBContext; |
33 | |
import javax.xml.bind.JAXBException; |
34 | |
import javax.xml.bind.Unmarshaller; |
35 | |
import javax.xml.bind.UnmarshallerHandler; |
36 | |
import javax.xml.parsers.ParserConfigurationException; |
37 | |
import javax.xml.parsers.SAXParserFactory; |
38 | |
import java.io.IOException; |
39 | |
import java.io.InputStream; |
40 | |
import java.util.ArrayList; |
41 | |
import java.util.Collections; |
42 | |
import java.util.HashSet; |
43 | |
import java.util.LinkedHashMap; |
44 | |
import java.util.List; |
45 | |
import java.util.Map; |
46 | |
import java.util.Properties; |
47 | |
import java.util.Random; |
48 | |
import java.util.Set; |
49 | |
import java.util.SortedSet; |
50 | |
import java.util.TreeSet; |
51 | |
import java.util.regex.Matcher; |
52 | |
import java.util.regex.Pattern; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public class JAXBConfigImpl extends AbstractBaseConfig { |
66 | |
|
67 | 0 | private static final Logger LOG = Logger.getLogger(JAXBConfigImpl.class); |
68 | |
|
69 | |
private static final String IMPORT_NAME = "config.location"; |
70 | |
private static final String INDENT = " "; |
71 | |
private static final String PLACEHOLDER_REGEX = "\\$\\{([^{}]+)\\}"; |
72 | |
|
73 | |
|
74 | 0 | private static final Random RANDOM = new Random(); |
75 | |
|
76 | 0 | private final List<String> fileLocs = new ArrayList<String>(); |
77 | |
|
78 | 0 | private final Map<String, Object> objects = new LinkedHashMap<String, Object>(); |
79 | 0 | private final Properties rawProperties = new Properties(); |
80 | 0 | private final Properties resolvedProperties = new Properties(); |
81 | |
|
82 | |
|
83 | 0 | private final Pattern pattern = Pattern.compile(PLACEHOLDER_REGEX); |
84 | |
|
85 | |
private boolean systemOverride; |
86 | |
|
87 | 0 | public JAXBConfigImpl(){} |
88 | |
|
89 | 0 | public JAXBConfigImpl(Config config) { |
90 | 0 | this.copyConfig(config); |
91 | 0 | } |
92 | |
|
93 | 0 | public JAXBConfigImpl(String fileLoc, Config config) { |
94 | 0 | this.copyConfig(config); |
95 | 0 | this.fileLocs.add(fileLoc); |
96 | 0 | } |
97 | |
|
98 | 0 | public JAXBConfigImpl(List<String> fileLocs, Config config) { |
99 | 0 | this.copyConfig(config); |
100 | 0 | this.fileLocs.addAll(fileLocs); |
101 | |
|
102 | 0 | } |
103 | |
|
104 | 0 | public JAXBConfigImpl(String fileLoc) { |
105 | 0 | this.fileLocs.add(fileLoc); |
106 | 0 | } |
107 | |
|
108 | 0 | public JAXBConfigImpl(List<String> fileLocs) { |
109 | 0 | this.fileLocs.addAll(fileLocs); |
110 | 0 | } |
111 | |
|
112 | 0 | public JAXBConfigImpl(Properties properties) { |
113 | 0 | this.putProperties(properties); |
114 | 0 | } |
115 | |
|
116 | 0 | public JAXBConfigImpl(String fileLoc, Properties properties) { |
117 | 0 | this.fileLocs.add(fileLoc); |
118 | 0 | this.putProperties(properties); |
119 | 0 | } |
120 | |
|
121 | 0 | public JAXBConfigImpl(List<String> fileLocs, Properties properties) { |
122 | 0 | this.fileLocs.addAll(fileLocs); |
123 | 0 | this.putProperties(properties); |
124 | 0 | } |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
private void copyConfig(Config config){ |
133 | 0 | if(config == null) { |
134 | 0 | return; |
135 | |
} |
136 | |
|
137 | 0 | this.putProperties(config.getProperties()); |
138 | |
|
139 | 0 | if(config.getObjects() != null) { |
140 | 0 | this.objects.putAll(config.getObjects()); |
141 | |
} |
142 | 0 | } |
143 | |
|
144 | |
@Override |
145 | |
public Object getObject(String key) { |
146 | 0 | return objects.get(key); |
147 | |
} |
148 | |
|
149 | |
@Override |
150 | |
public Map<String, Object> getObjects() { |
151 | 0 | return Collections.unmodifiableMap(objects); |
152 | |
} |
153 | |
|
154 | |
@Override |
155 | |
public Properties getProperties() { |
156 | 0 | return new ImmutableProperties(resolvedProperties); |
157 | |
} |
158 | |
|
159 | |
@Override |
160 | |
public String getProperty(String key) { |
161 | 0 | return resolvedProperties.getProperty(key); |
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
@Override |
171 | |
public void putProperty(String key, String value) { |
172 | 0 | this.setProperty(key, replaceVariable(key, value)); |
173 | 0 | resolveRawToCache(); |
174 | 0 | } |
175 | |
|
176 | |
@Override |
177 | |
public void putProperties(Properties properties) { |
178 | 0 | if (properties != null) { |
179 | 0 | for(Object o : properties.keySet()) { |
180 | 0 | this.setProperty((String)o, replaceVariable((String)o, properties.getProperty((String)o))); |
181 | |
} |
182 | |
|
183 | 0 | resolveRawToCache(); |
184 | |
} |
185 | 0 | } |
186 | |
|
187 | |
@Override |
188 | |
public void parseConfig() throws IOException { |
189 | |
|
190 | 0 | if (!fileLocs.isEmpty()) { |
191 | |
|
192 | 0 | if (LOG.isInfoEnabled()) { |
193 | 0 | LOG.info("Loading Rice configs: " + StringUtils.join(fileLocs, ", ")); |
194 | |
} |
195 | |
|
196 | |
Unmarshaller unmarshaller; |
197 | |
|
198 | |
try { |
199 | 0 | JAXBContext jaxbContext = JAXBContext.newInstance(org.kuali.rice.core.impl.config.property.Config.class); |
200 | 0 | unmarshaller = jaxbContext.createUnmarshaller(); |
201 | 0 | } catch (Exception ex) { |
202 | 0 | throw new ConfigurationException("Error initializing JAXB for config", ex); |
203 | 0 | } |
204 | |
|
205 | |
|
206 | 0 | configureBuiltIns(); |
207 | |
|
208 | |
|
209 | 0 | for (String s : fileLocs) { |
210 | 0 | parseConfig(s, unmarshaller, 0); |
211 | |
} |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | 0 | resolveRawToCache(); |
217 | |
|
218 | 0 | if (LOG.isInfoEnabled()) { |
219 | 0 | final StringBuilder log = new StringBuilder(); |
220 | 0 | log.append("\n"); |
221 | 0 | log.append("####################################\n"); |
222 | 0 | log.append("#\n"); |
223 | 0 | log.append("# Properties used after config override/replacement\n"); |
224 | 0 | log.append("# " + StringUtils.join(fileLocs, ", ") + "\n"); |
225 | 0 | log.append("#\n"); |
226 | 0 | log.append("####################################\n"); |
227 | |
|
228 | |
|
229 | |
|
230 | 0 | SortedSet<String> sorted = new TreeSet<String>(); |
231 | 0 | CollectionUtils.addAll(sorted, rawProperties.propertyNames()); |
232 | |
|
233 | 0 | for (String s : sorted) { |
234 | 0 | log.append("Using config Prop "); |
235 | 0 | log.append(s); |
236 | 0 | log.append("=["); |
237 | 0 | log.append(ConfigLogger.getDisplaySafeValue(s, this.getProperty(s))); |
238 | 0 | log.append("]\n"); |
239 | |
} |
240 | 0 | LOG.info(log); |
241 | |
} |
242 | |
|
243 | 0 | } else { |
244 | 0 | LOG.info("Loading Rice configs: No config files specified"); |
245 | |
} |
246 | 0 | } |
247 | |
|
248 | |
protected void parseConfig(String filename, Unmarshaller unmarshaller, int depth) throws IOException { |
249 | |
|
250 | 0 | InputStream in = null; |
251 | |
|
252 | |
|
253 | |
|
254 | 0 | if (StringUtils.isNotEmpty(filename)) { |
255 | 0 | in = RiceUtilities.getResourceAsStream(filename); |
256 | |
} |
257 | |
|
258 | 0 | if (in == null) { |
259 | 0 | final StringBuilder log = new StringBuilder(); |
260 | 0 | log.append("\n"); |
261 | 0 | log.append("####################################\n"); |
262 | 0 | log.append("#\n"); |
263 | 0 | log.append("# Configuration file '" + filename + "' not found!\n"); |
264 | 0 | log.append("#\n"); |
265 | 0 | log.append("####################################\n"); |
266 | 0 | LOG.warn(log); |
267 | 0 | } else { |
268 | |
|
269 | 0 | final String prefix = StringUtils.repeat(INDENT, depth); |
270 | 0 | LOG.info(prefix + "+ Parsing config: " + filename); |
271 | |
org.kuali.rice.core.impl.config.property.Config config; |
272 | |
|
273 | |
try { |
274 | 0 | config = unmarshal(unmarshaller, in); |
275 | 0 | } catch (Exception ex) { |
276 | 0 | throw new ConfigurationException("Error parsing config file: " + filename, ex); |
277 | 0 | } |
278 | |
|
279 | 0 | for (Param p : config.getParamList()) { |
280 | |
|
281 | 0 | String name = p.getName(); |
282 | |
|
283 | 0 | if (name.equals(IMPORT_NAME)) { |
284 | 0 | String configLocation = parseValue(p.getValue(), new HashSet<String>()); |
285 | |
|
286 | 0 | if(configLocation != null){ |
287 | 0 | configLocation = configLocation.trim(); |
288 | |
} |
289 | 0 | parseConfig(configLocation, unmarshaller, depth + 1); |
290 | 0 | } else if(p.isSystem()){ |
291 | 0 | if (p.isOverride() || System.getProperty(name) == null){ |
292 | 0 | if(p.isRandom()){ |
293 | 0 | String randStr = String.valueOf(generateRandomInteger(p.getValue())); |
294 | 0 | System.setProperty(name, randStr); |
295 | 0 | this.setProperty(p.getName(), randStr); |
296 | 0 | if(LOG.isInfoEnabled()) |
297 | |
{ |
298 | 0 | LOG.info("generating random string " + randStr + " for system property " + p.getName()); |
299 | |
} |
300 | 0 | }else{ |
301 | |
|
302 | |
|
303 | |
|
304 | 0 | HashSet<String> set = new HashSet<String>(); |
305 | 0 | set.add(p.getName()); |
306 | 0 | String value = parseValue(p.getValue(), set); |
307 | 0 | System.setProperty(name, value); |
308 | 0 | this.setProperty(name, value); |
309 | 0 | } |
310 | |
} |
311 | |
} |
312 | 0 | else if (p.isOverride() || !rawProperties.containsKey(name)) { |
313 | |
|
314 | 0 | if (p.isRandom()) { |
315 | |
|
316 | 0 | String randStr = String.valueOf(generateRandomInteger(p.getValue())); |
317 | 0 | this.setProperty(p.getName(), randStr); |
318 | 0 | if(LOG.isInfoEnabled()) |
319 | |
{ |
320 | 0 | LOG.info("generating random string " + randStr + " for property " + p.getName()); |
321 | |
} |
322 | 0 | } else { |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | 0 | String value = replaceVariable(name, p.getValue()); |
333 | |
|
334 | 0 | this.setProperty(name, value); |
335 | |
} |
336 | |
} |
337 | 0 | } |
338 | |
|
339 | 0 | LOG.info(prefix + "- Parsed config: " + filename); |
340 | |
} |
341 | 0 | } |
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
protected void setProperty(String name, String value){ |
348 | 0 | if(LOG.isInfoEnabled()){ |
349 | 0 | String oldProp = rawProperties.getProperty(name); |
350 | 0 | if(oldProp != null && !oldProp.equals(value)){ |
351 | 0 | LOG.info("Raw Config Override: " + name + "=[" + ConfigLogger.getDisplaySafeValue(name,oldProp) +"]->[" + ConfigLogger.getDisplaySafeValue(name,value) +"]"); |
352 | |
} |
353 | |
} |
354 | 0 | rawProperties.setProperty(name, value); |
355 | 0 | } |
356 | |
|
357 | |
protected String resolve(String key) { |
358 | 0 | return resolve(key, null); |
359 | |
} |
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
protected String resolve(String key, Set<String> keySet) { |
375 | |
|
376 | |
|
377 | 0 | if (keySet != null && keySet.contains(key)) { |
378 | 0 | throw new ConfigurationException("Circular reference in config: " + key); |
379 | |
} |
380 | |
|
381 | 0 | String value = this.rawProperties.getProperty(key); |
382 | |
|
383 | 0 | if ((value == null || systemOverride) && System.getProperties().containsKey(key)) { |
384 | 0 | value = System.getProperty(key); |
385 | |
} |
386 | |
|
387 | 0 | if (value != null && value.contains("${")) { |
388 | 0 | if(keySet == null) { |
389 | 0 | keySet = new HashSet<String>(); |
390 | |
} |
391 | 0 | keySet.add(key); |
392 | |
|
393 | 0 | value = parseValue(value, keySet); |
394 | |
|
395 | 0 | keySet.remove(key); |
396 | |
} |
397 | |
|
398 | 0 | if(value == null) { |
399 | 0 | value = ""; |
400 | 0 | LOG.warn("Property key: '" + key + "' is not available and hence set to empty"); |
401 | |
} |
402 | |
|
403 | 0 | return value; |
404 | |
} |
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
protected String parseValue(String value, Set<String> keySet) { |
416 | 0 | String result = value; |
417 | |
|
418 | 0 | Matcher matcher = pattern.matcher(value); |
419 | |
|
420 | 0 | while (matcher.find()) { |
421 | |
|
422 | |
|
423 | 0 | String key = matcher.group(1); |
424 | |
|
425 | 0 | String resolved = resolve(key, keySet); |
426 | |
|
427 | 0 | result = matcher.replaceFirst(Matcher.quoteReplacement(resolved)); |
428 | 0 | matcher = matcher.reset(result); |
429 | 0 | } |
430 | |
|
431 | 0 | return result; |
432 | |
} |
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
protected String replaceVariable(String name, String value){ |
457 | 0 | String regex = "(?:\\$\\{"+ name +"\\})"; |
458 | 0 | String temporary = null; |
459 | |
|
460 | |
|
461 | |
|
462 | |
|
463 | 0 | if(value.contains("${" + name + "}")) { |
464 | 0 | if( (temporary = rawProperties.getProperty(name)) == null ) { |
465 | 0 | temporary = System.getProperty(name); |
466 | |
} |
467 | |
|
468 | 0 | if(temporary != null) { |
469 | 0 | return value.replaceAll(regex, Matcher.quoteReplacement(temporary)); |
470 | |
} |
471 | |
} |
472 | |
|
473 | 0 | return value; |
474 | |
} |
475 | |
|
476 | |
|
477 | |
|
478 | |
|
479 | |
|
480 | |
|
481 | |
protected void resolveRawToCache() { |
482 | 0 | if(rawProperties.size() > 0) { |
483 | 0 | Properties oldProps = new Properties(new ImmutableProperties(resolvedProperties)); |
484 | |
|
485 | 0 | resolvedProperties.clear(); |
486 | |
|
487 | 0 | for(Object o : rawProperties.keySet()) { |
488 | 0 | String resolved = resolve((String)o); |
489 | |
|
490 | 0 | if(LOG.isInfoEnabled()){ |
491 | 0 | String oldResolved = oldProps.getProperty((String)o); |
492 | 0 | if(oldResolved != null && !oldResolved.equals(resolved)){ |
493 | 0 | String key = (String)o; |
494 | 0 | String unResolved = rawProperties.getProperty(key); |
495 | |
|
496 | 0 | if(unResolved.contains("$")){ |
497 | 0 | LOG.info("Resolved Config Override: " + key + "(" + unResolved +")=[" + ConfigLogger.getDisplaySafeValue(key,oldResolved) +"]->[" + ConfigLogger.getDisplaySafeValue(key,resolved) +"]"); |
498 | |
}else{ |
499 | 0 | LOG.info("Resolved Config Override: " + key + "=[" + ConfigLogger.getDisplaySafeValue(key,oldResolved) +"]->[" + ConfigLogger.getDisplaySafeValue(key,resolved) +"]"); |
500 | |
} |
501 | |
} |
502 | |
} |
503 | 0 | resolvedProperties.setProperty((String)o, resolved); |
504 | 0 | } |
505 | |
} |
506 | 0 | } |
507 | |
|
508 | |
|
509 | |
|
510 | |
|
511 | |
protected void configureBuiltIns() { |
512 | 0 | this.setProperty("host.ip", RiceUtilities.getIpNumber()); |
513 | 0 | this.setProperty("host.name", RiceUtilities.getHostName()); |
514 | 0 | } |
515 | |
|
516 | |
|
517 | |
|
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
|
523 | |
protected int generateRandomInteger(String rangeSpec) { |
524 | 0 | String[] range = rangeSpec.split("-"); |
525 | 0 | if (range.length != 2) { |
526 | 0 | throw new RuntimeException("Invalid range specifier: " + rangeSpec); |
527 | |
} |
528 | 0 | int from = Integer.parseInt(range[0].trim()); |
529 | 0 | int to = Integer.parseInt(range[1].trim()); |
530 | 0 | if (from > to) { |
531 | 0 | int tmp = from; |
532 | 0 | from = to; |
533 | 0 | to = tmp; |
534 | |
} |
535 | |
int num; |
536 | |
|
537 | 0 | if (from == to) { |
538 | 0 | num = from; |
539 | 0 | if(LOG.isInfoEnabled()) |
540 | |
{ |
541 | 0 | LOG.info("from==to, so not generating random value for property."); |
542 | |
} |
543 | |
} else { |
544 | 0 | num = from + RANDOM.nextInt((to - from) + 1); |
545 | |
} |
546 | 0 | return num; |
547 | |
} |
548 | |
|
549 | |
public boolean isSystemOverride() { |
550 | 0 | return systemOverride; |
551 | |
} |
552 | |
|
553 | |
|
554 | |
|
555 | |
|
556 | |
|
557 | |
|
558 | |
|
559 | |
|
560 | |
|
561 | |
public void setSystemOverride(boolean systemOverride) { |
562 | 0 | this.systemOverride = systemOverride; |
563 | 0 | } |
564 | |
|
565 | |
protected org.kuali.rice.core.impl.config.property.Config unmarshal(Unmarshaller unmarshaller, InputStream in) throws SAXException, JAXBException, ParserConfigurationException, IOException { |
566 | 0 | SAXParserFactory spf = SAXParserFactory.newInstance(); |
567 | 0 | spf.setNamespaceAware(true); |
568 | |
|
569 | 0 | XMLFilter filter = new ConfigNamespaceURIFilter(); |
570 | 0 | filter.setParent(spf.newSAXParser().getXMLReader()); |
571 | |
|
572 | 0 | UnmarshallerHandler handler = unmarshaller.getUnmarshallerHandler(); |
573 | 0 | filter.setContentHandler(handler); |
574 | |
|
575 | 0 | filter.parse(new InputSource(in)); |
576 | |
|
577 | 0 | return (org.kuali.rice.core.impl.config.property.Config)handler.getResult(); |
578 | |
} |
579 | |
|
580 | |
|
581 | |
|
582 | |
|
583 | |
|
584 | |
|
585 | |
|
586 | |
|
587 | |
|
588 | |
|
589 | |
|
590 | 0 | public class ConfigNamespaceURIFilter extends XMLFilterImpl { |
591 | |
|
592 | |
public static final String CONFIG_URI="http://rice.kuali.org/core/impl/config"; |
593 | |
|
594 | |
@Override |
595 | |
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { |
596 | 0 | if(StringUtils.isBlank(uri)) { |
597 | 0 | uri = CONFIG_URI; |
598 | |
} |
599 | |
|
600 | 0 | super.startElement(uri, localName, qName, atts); |
601 | 0 | } |
602 | |
|
603 | |
@Override |
604 | |
public void endElement(String uri, String localName, String qName) throws SAXException { |
605 | 0 | if(StringUtils.isBlank(uri)) { |
606 | 0 | uri = CONFIG_URI; |
607 | |
} |
608 | |
|
609 | 0 | super.endElement(uri, localName, qName); |
610 | 0 | } |
611 | |
} |
612 | |
|
613 | |
|
614 | |
@Override |
615 | |
public void putObject(String key, Object value) { |
616 | 0 | this.objects.put(key, value); |
617 | 0 | } |
618 | |
|
619 | |
@Override |
620 | |
public void putObjects(Map<String, Object> objects) { |
621 | 0 | this.objects.putAll(objects); |
622 | 0 | } |
623 | |
|
624 | |
@Override |
625 | |
public void removeObject(String key){ |
626 | 0 | this.objects.remove(key); |
627 | 0 | } |
628 | |
|
629 | |
@Override |
630 | |
public void removeProperty(String key){ |
631 | 0 | this.rawProperties.remove(key); |
632 | |
|
633 | 0 | resolveRawToCache(); |
634 | 0 | } |
635 | |
|
636 | |
@Override |
637 | |
public void putConfig(Config config) { |
638 | 0 | this.copyConfig(config); |
639 | 0 | } |
640 | |
|
641 | |
|
642 | |
} |