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.lang.StringUtils; |
20 | |
import org.apache.commons.lang.text.StrLookup; |
21 | |
import org.apache.commons.lang.text.StrSubstitutor; |
22 | |
import org.apache.log4j.Logger; |
23 | |
import org.kuali.rice.core.util.RiceUtilities; |
24 | |
import org.kuali.rice.core.util.XmlJotter; |
25 | |
import org.kuali.rice.core.xml.XmlException; |
26 | |
import org.w3c.dom.Document; |
27 | |
import org.w3c.dom.Element; |
28 | |
import org.w3c.dom.Node; |
29 | |
import org.w3c.dom.NodeList; |
30 | |
import org.xml.sax.SAXException; |
31 | |
|
32 | |
import javax.xml.parsers.DocumentBuilderFactory; |
33 | |
import javax.xml.parsers.ParserConfigurationException; |
34 | |
import java.io.IOException; |
35 | |
import java.io.InputStream; |
36 | |
import java.util.LinkedHashMap; |
37 | |
import java.util.Map; |
38 | |
import java.util.Random; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public class ConfigParserImpl implements ConfigParser { |
49 | |
|
50 | 0 | private static final Random RANDOM = new Random(); |
51 | |
|
52 | 0 | private static final Logger LOG = Logger.getLogger(ConfigParserImpl.class); |
53 | |
private static final String IMPORT_NAME = "config.location"; |
54 | |
private static final String PARAM_NAME= "param"; |
55 | |
private static final String NAME_ATTR = "name"; |
56 | |
private static final String OVERRIDE_ATTR = "override"; |
57 | |
private static final String RANDOM_ATTR = "random"; |
58 | |
private static final String INDENT = " "; |
59 | |
|
60 | |
public static final String ALTERNATE_BUILD_LOCATION_KEY = "alt.build.location"; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | 0 | private static class SystemPropertiesDelegatingStrLookup extends StrLookup { |
68 | |
private final Map map; |
69 | 0 | private SystemPropertiesDelegatingStrLookup(Map map) { |
70 | 0 | this.map = map; |
71 | 0 | } |
72 | |
@Override |
73 | |
public String lookup(String key) { |
74 | 0 | Object o = map.get(key); |
75 | 0 | if (o != null) { |
76 | 0 | return String.valueOf(o); |
77 | |
} else { |
78 | 0 | String s = System.getProperty(key); |
79 | 0 | if (s != null) { |
80 | 0 | return s; |
81 | |
} else { |
82 | |
|
83 | |
|
84 | 0 | return ""; |
85 | |
} |
86 | |
} |
87 | |
} |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public void parse(Map props, String[] locations) throws IOException { |
94 | 0 | LinkedHashMap params = new LinkedHashMap(); |
95 | 0 | params.putAll(props); |
96 | 0 | parse(params, locations); |
97 | 0 | props.putAll(params); |
98 | 0 | } |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
protected void parse(LinkedHashMap<String, Object> params, String[] locations) throws IOException { |
107 | 0 | StrSubstitutor subs = new StrSubstitutor(new SystemPropertiesDelegatingStrLookup(params)); |
108 | 0 | for (String location: locations) { |
109 | 0 | parse(params, location, subs, 0); |
110 | |
} |
111 | 0 | } |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
protected void parse(LinkedHashMap<String, Object> params, String location, StrSubstitutor subs, int depth) throws IOException { |
121 | 0 | InputStream configStream = RiceUtilities.getResourceAsStream(location); |
122 | 0 | if (configStream == null) { |
123 | 0 | LOG.warn("###############################"); |
124 | 0 | LOG.warn("#"); |
125 | 0 | LOG.warn("# Configuration file '" + location + "' not found!"); |
126 | 0 | LOG.warn("#"); |
127 | 0 | LOG.warn("###############################"); |
128 | 0 | return; |
129 | |
} |
130 | |
|
131 | 0 | final String prefix = StringUtils.repeat(INDENT, depth); |
132 | 0 | LOG.info(prefix + "+ Parsing config: " + location); |
133 | |
|
134 | |
Document doc; |
135 | |
try { |
136 | 0 | doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(configStream); |
137 | 0 | if (LOG.isDebugEnabled()) { |
138 | 0 | LOG.debug("Contents of config " + location + ": \n" + XmlJotter.jotNode(doc, true)); |
139 | |
} |
140 | 0 | } catch (SAXException se) { |
141 | 0 | IOException ioe = new IOException("Error parsing config resource: " + location); |
142 | 0 | ioe.initCause(se); |
143 | 0 | throw ioe; |
144 | 0 | } catch (ParserConfigurationException pce) { |
145 | 0 | IOException ioe = new IOException("Unable to obtain document builder"); |
146 | 0 | ioe.initCause(pce); |
147 | 0 | throw ioe; |
148 | |
} finally { |
149 | 0 | configStream.close(); |
150 | 0 | } |
151 | |
|
152 | 0 | Element root = doc.getDocumentElement(); |
153 | |
|
154 | |
|
155 | 0 | NodeList list = root.getChildNodes(); |
156 | 0 | StringBuilder content = new StringBuilder(); |
157 | 0 | for (int i = 0; i < list.getLength(); i++) { |
158 | 0 | Node node = list.item(i); |
159 | 0 | if (node.getNodeType() != Node.ELEMENT_NODE) |
160 | 0 | continue; |
161 | 0 | if (!PARAM_NAME.equals(node.getNodeName())) { |
162 | 0 | LOG.warn("Encountered non-param config node: " + node.getNodeName()); |
163 | 0 | continue; |
164 | |
} |
165 | 0 | Element param = (Element) node; |
166 | 0 | String name = param.getAttribute(NAME_ATTR); |
167 | 0 | if (name == null) { |
168 | 0 | LOG.error("Unnamed parameter in config resource '" + location + "': " + XmlJotter.jotNode(param)); |
169 | 0 | continue; |
170 | |
} |
171 | 0 | Boolean override = Boolean.TRUE; |
172 | 0 | String overrideVal = param.getAttribute(OVERRIDE_ATTR); |
173 | 0 | if (!StringUtils.isEmpty(overrideVal)) { |
174 | 0 | override = Boolean.valueOf(overrideVal); |
175 | |
} |
176 | |
|
177 | 0 | content.setLength(0); |
178 | |
|
179 | 0 | getNodeValue(name, location, param, content); |
180 | 0 | String value = subs.replace(content); |
181 | 0 | if (LOG.isDebugEnabled()) { |
182 | 0 | LOG.debug(prefix + INDENT + "* " + name + "=[" + ConfigLogger.getDisplaySafeValue(name, value) + "]"); |
183 | |
} |
184 | |
|
185 | 0 | if (IMPORT_NAME.equals(name)) { |
186 | |
|
187 | |
|
188 | |
|
189 | 0 | if (!value.contains(ALTERNATE_BUILD_LOCATION_KEY)) { |
190 | 0 | parse(params, value, subs, depth + 1); |
191 | |
} |
192 | |
} else { |
193 | 0 | if (Boolean.valueOf(param.getAttribute(RANDOM_ATTR))) { |
194 | |
|
195 | 0 | value = String.valueOf(generateRandomInteger(value)); |
196 | |
} |
197 | 0 | setParam(params, override, name, value, prefix + INDENT); |
198 | |
} |
199 | |
} |
200 | 0 | LOG.info(prefix + "- Parsed config: " + location); |
201 | 0 | } |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
protected int generateRandomInteger(String rangeSpec) { |
209 | 0 | String[] range = rangeSpec.split("-"); |
210 | 0 | if (range.length != 2) { |
211 | 0 | throw new RuntimeException("Invalid range specifier: " + rangeSpec); |
212 | |
} |
213 | 0 | int from = Integer.parseInt(range[0].trim()); |
214 | 0 | int to = Integer.parseInt(range[1].trim()); |
215 | 0 | if (from > to) { |
216 | 0 | int tmp = from; |
217 | 0 | from = to; |
218 | 0 | to = tmp; |
219 | |
} |
220 | |
int num; |
221 | |
|
222 | 0 | if (from == to) { |
223 | 0 | num = from; |
224 | |
} else { |
225 | 0 | num = from + RANDOM.nextInt((to - from) + 1); |
226 | |
} |
227 | 0 | return num; |
228 | |
} |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
protected void getNodeValue(String name, String location, Node n, StringBuilder sb) throws IOException { |
238 | 0 | NodeList children = n.getChildNodes(); |
239 | |
|
240 | |
try { |
241 | 0 | sb.setLength(0); |
242 | 0 | for (int j = 0; j < children.getLength(); j++) { |
243 | 0 | sb.append(XmlJotter.jotNode(children.item(j), true)); |
244 | |
} |
245 | 0 | } catch (XmlException te) { |
246 | 0 | IOException ioe = new IOException("Error obtaining parameter '" + name + "' from config resource: " + location); |
247 | 0 | ioe.initCause(te); |
248 | 0 | throw ioe; |
249 | 0 | } |
250 | 0 | } |
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
private void setParam(Map params, Boolean override, String name, String value, String indent) { |
261 | 0 | if (value == null || "null".equals(value)) { |
262 | 0 | LOG.warn("Not adding property [" + name + "] because it is null - most likely no token could be found for substituion."); |
263 | 0 | return; |
264 | |
} |
265 | 0 | if (override) { |
266 | |
final String message; |
267 | 0 | Object existingValue = params.get(name); |
268 | 0 | if (existingValue != null) { |
269 | |
|
270 | 0 | message = indent + "Overriding property " + name + "=[" + existingValue + "] with " + name + "=[" + value + "]"; |
271 | |
|
272 | 0 | params.remove(name); |
273 | |
} else { |
274 | 0 | message = indent + "Defining property " + name + "=[" + value + "]"; |
275 | |
} |
276 | 0 | LOG.debug(message); |
277 | 0 | params.put(name, value); |
278 | 0 | } else if (!params.containsKey(name)) { |
279 | 0 | LOG.debug(indent + "Defining property " + name + "=[" + value + "]"); |
280 | 0 | params.put(name, value); |
281 | |
} else { |
282 | 0 | LOG.debug(indent + "Not overriding existing parameter: " + name + " '" + params.get(name) + "'"); |
283 | |
} |
284 | 0 | } |
285 | |
} |