View Javadoc
1   /**
2    * Copyright 2005-2013 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  package edu.samplu.common;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.util.ArrayList;
21  import java.util.Enumeration;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.LinkedList;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Properties;
28  
29  /**
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  
33  public class PropertiesUtils {
34  
35  
36      public static Properties loadPropertiesWithSystemOverrides(InputStream inputStream) throws IOException {
37          Properties props = PropertiesUtils.loadProperties(inputStream);
38          PropertiesUtils.systemPropertiesOverride(props);
39          return props;
40      }
41  
42      public static Properties loadPropertiesWithSystemOverridesAndNumberedPropertiesToList(InputStream inputStream) throws IOException {
43          Properties props = PropertiesUtils.loadProperties(inputStream);
44          PropertiesUtils.systemPropertiesOverride(props);
45          PropertiesUtils.transformNumberedPropertiesToList(props);
46          return props;
47      }
48  
49      public static Properties loadProperties(InputStream inputStream) throws IOException {
50          Properties props = new Properties();
51  
52          if(inputStream != null) {
53              props.load(inputStream);
54          }
55  
56          return props;
57      }
58  
59      public static String removeNumber(final String numberedKey) {
60          String unnumberedKey = numberedKey;
61          int firstNumberIndex = unnumberedKey.length() - 1;
62          while (Character.isDigit(unnumberedKey.charAt(firstNumberIndex))) {
63              firstNumberIndex--;
64          }
65          unnumberedKey = unnumberedKey.substring(0, firstNumberIndex + 1);
66  
67          return unnumberedKey;
68      }
69  
70      public static void systemPropertiesOverride(Properties props) {
71          PropertiesUtils.systemPropertiesOverride(props, null);
72      }
73  
74      /**
75       * -Dkey.propertyname= to override the property value for propertyname.
76       * @param props properties to update with System.getProperty overrides.
77       * @param key optional value that the property names will be appended to.
78       */
79      public static void systemPropertiesOverride(Properties props, String key) {
80          Enumeration<?> names = props.propertyNames();
81          Object nameObject;
82          String name;
83          while (names.hasMoreElements()) {
84  
85              nameObject = names.nextElement();
86              if (nameObject instanceof String) {
87  
88                  name = (String)nameObject;
89                  if (key == null || key.isEmpty()) {
90                      props.setProperty(name, System.getProperty(name, props.getProperty(name)));
91                  } else {
92                      props.setProperty(name, System.getProperty(key + "." + name, props.getProperty(name)));
93                  }
94              }
95          }
96      }
97  
98      public static void transformNumberedPropertiesToList(Properties props) {
99          String key = null;
100         String unnumberedKey = null;
101         List<String> keyList = null;
102         List<String> removeKeys = new LinkedList<String>();
103 
104         // unnumber keys and place their values in a list
105         Iterator keys = props.keySet().iterator();
106         Map<String, List<String>> keysLists = new HashMap<String, List<String>>();
107         while (keys.hasNext()) {
108             key = (String)keys.next();
109             if (Character.isDigit(key.charAt(key.length()-1))) {
110                 unnumberedKey = removeNumber(key);
111                 if (keysLists.get(unnumberedKey) == null) {
112                     keyList = new ArrayList<String>();
113                     keyList.add(props.getProperty(key));
114                     keysLists.put(unnumberedKey, keyList);
115                     removeKeys.add(key);
116                 } else {
117                     keyList = keysLists.get(unnumberedKey);
118                     keyList.add(props.getProperty(key));
119                     keysLists.put(unnumberedKey, keyList);
120                     removeKeys.add(key);
121                 }
122             }
123         }
124 
125         // remove keys that where unnumbered
126         Iterator removeKey = removeKeys.iterator();
127         while (removeKey.hasNext()) {
128             key = (String)removeKey.next();
129             props.remove(key);
130         }
131 
132         // put new unnumbered key values mapped by unnumber key with an s appended to it.
133         Iterator newKeys = keysLists.keySet().iterator();
134         String newKey = null;
135         while (newKeys.hasNext()) {
136             newKey = (String)newKeys.next();
137             props.put(newKey + "s", keysLists.get(newKey));
138         }
139     }
140 }