1 package org.lsst.ccs.bootstrap.resources;
2
3 import java.util.Map.Entry;
4 import java.util.Properties;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
7 import org.lsst.ccs.bootstrap.Bootstrap;
8 import org.lsst.ccs.bootstrap.BootstrapUtils;
9
10
11
12
13
14 public class ResourcesTreeProperties extends Properties {
15
16 private final String resourceDirectory, propertyFileName;
17 private final Properties parent;
18 private ResourcesTreeProperties child;
19
20
21 private static final String prop_str_pattern = ".*(\\$\\[prop\\.(.*)\\]).*";
22 private static final Pattern prop_pattern = Pattern.compile(prop_str_pattern);
23
24
25 public ResourcesTreeProperties(String propertyFileName, String resourceDirectory, Properties parent) {
26 super(parent);
27 this.resourceDirectory = resourceDirectory;
28 this.propertyFileName = propertyFileName;
29 this.parent = parent;
30 if ( parent != null && parent instanceof ResourcesTreeProperties ) {
31 ((ResourcesTreeProperties) parent).setChild(this);
32 }
33
34 }
35
36 private void setChild(ResourcesTreeProperties child) {
37 this.child = child;
38 }
39
40 public String getResourceDirectory() {
41 return resourceDirectory;
42 }
43
44 public String getPropertyFileName() {
45 return propertyFileName;
46 }
47
48 public boolean hasParent() {
49 return parent != null;
50 }
51
52 public Properties getParent() {
53 return parent;
54 }
55
56 @Override
57 public synchronized Object get(Object key) {
58 return parseGoingOut((String)super.get(key));
59 }
60
61 @Override
62 public String getProperty(String key) {
63 return parseGoingOut(super.getProperty(key));
64 }
65
66 @Override
67 public String getProperty(String key, String defaultValue) {
68 return parseGoingOut(super.getProperty(key, defaultValue));
69 }
70
71 public String getPropertyFromTop(String key) {
72 String value = null;
73 if ( child != null ) {
74 return child.getPropertyFromTop(key);
75 } else {
76 return getProperty(key);
77 }
78 }
79
80
81 @Override
82 public synchronized Object put(Object key, Object value) {
83 String valueStr = BootstrapUtils.parseProperty((String) value);
84 return super.put(key, valueStr);
85 }
86
87 public void copyProperties(Properties props) {
88 if ( parent != null ) {
89 if ( parent instanceof ResourcesTreeProperties ) {
90 ((ResourcesTreeProperties)parent).copyProperties(props);
91 } else {
92 for ( Object key : parent.keySet() ) {
93 props.put(key,parent.get(key));
94 }
95 }
96 }
97
98 for ( Entry<Object,Object> entry : entrySet() ) {
99 props.put(entry.getKey(),entry.getValue());
100 }
101 }
102
103 private String parseGoingOut(String inputProperty) {
104 if (inputProperty == null) {
105 return inputProperty;
106 }
107 String outProperty = inputProperty;
108 Matcher m = prop_pattern.matcher(outProperty);
109 if (m.matches()) {
110
111 String propValue = getPropertyFromTop(m.group(2));
112
113 if ( propValue == null ) {
114 propValue = System.getProperty(m.group(2));
115 }
116 if (propValue != null) {
117 outProperty = outProperty.replace(m.group(1), propValue);
118 } else {
119 if (Bootstrap.isBootstrapEnvironment()) {
120 System.out.println("[WARNING] Property " + m.group(2) + " is not defined.");
121 }
122 }
123 }
124 return outProperty;
125 }
126
127 }