Coverage Report - org.kuali.rice.core.impl.config.property.ConfigParserImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigParserImpl
0%
0/107
0%
0/42
5.25
ConfigParserImpl$1
N/A
N/A
5.25
ConfigParserImpl$SystemPropertiesDelegatingStrLookup
0%
0/11
0%
0/4
5.25
 
 1  
 /*
 2  
  * Copyright 2006-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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.xml.XmlException;
 25  
 import org.kuali.rice.core.util.xml.XmlJotter;
 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  
  * ConfigParser implementation that supports a hierarchy of configs, in which 
 42  
  * configs can include other configs.  Variable tokens are resolved at parse time,
 43  
  * in the order in which they are encountered.  This class relies on Spring for resource
 44  
  * loading and Apache Commons Lang for variable replacement.
 45  
  *
 46  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 47  
  */
 48  0
 public class ConfigParserImpl implements ConfigParser {
 49  
     // keep the same random
 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  
      * A StrLookup implementation that delegates to System properties if the key is not
 64  
      * found in the supplied map.
 65  
      * @author Kuali Rice Team (rice.collab@kuali.org)
 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  
                     // implement behavior for missing property here...e.g. return ""
 83  
                     // returning null will result in the substitutor not substituting
 84  0
                     return "";
 85  
                 }
 86  
             }
 87  
         }
 88  
     }
 89  
 
 90  
     /**
 91  
      * @see org.kuali.rice.core.api.config.ConfigParser#parse(java.lang.String[])
 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  
      * Parses a list of locations
 102  
      * @param params the current parameter map
 103  
      * @param locations a list of locations to parse
 104  
      * @throws IOException
 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  
      * Parses a single config location
 115  
      * @param params the current parameter map
 116  
      * @param location the location to parse
 117  
      * @param subs a StrSubstitutor used to substitute variable tokens
 118  
      * @throws IOException
 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  
         // ignore the actual type of the document element for now
 154  
         // so that plugin descriptors can be parsed
 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  
             // accumulate all content (preserving any XML content)
 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  
                 // what is this...we don't follow a string with this substring in it? i.e. if the value does not
 187  
                 // resolve then don't try to follow it (it won't find it anyway; this is the case for any path
 188  
                 // with unresolved params...)?
 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  
                     // this is a special type of property whose value is a randomly generated number in the range specified
 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  
      * Generates a random integer in the range specified by the specifier, in the format: min-max
 205  
      * @param rangeSpec a range specification, 'min-max'
 206  
      * @return a random integer in the range specified by the specifier, in the format: min-max
 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  
         // not very random huh...
 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  
      * @param name name of the node
 232  
      * @param location config file location
 233  
      * @param n the node
 234  
      * @param sb a StringBuilder into which to set contents of the node, preserving any XML content
 235  
      * @throws IOException
 236  
      */
 237  
     protected void getNodeValue(String name, String location, Node n, StringBuilder sb) throws IOException {
 238  0
         NodeList children = n.getChildNodes();
 239  
         // accumulate all content (preserving any XML content)
 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  
      * Sets a parameter in the parameter map, based on the override setting and whether a parameter of the same
 254  
      * name is already present 
 255  
      * @param params the current parameter map
 256  
      * @param override whether to override a previous parameter definition
 257  
      * @param name the parameter name
 258  
      * @param value the parameter value
 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  
                 //if (!existingValue.equals(value)) {
 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  
 }