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