1 /**
2 * Copyright 2010 The Kuali Foundation Licensed under the
3 * Educational Community License, Version 2.0 (the "License"); you may
4 * not use this file except in compliance with the License. You may
5 * obtain a copy of the License at
6 *
7 * http://www.osedu.org/licenses/ECL-2.0
8 *
9 * Unless required by applicable law or agreed to in writing,
10 * software distributed under the License is distributed on an "AS IS"
11 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 package org.kuali.student.common.util;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.URL;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Properties;
27
28 /**
29 * A really freaking simple properties filter that returns a properties map for a subset of a properties file. Give this
30 * method the file name of the properties file, and then give it the prefix to filter on. The prefix gets a "." appended to
31 * it on filtering.
32 * <p>
33 * For example, if the properties are:
34 * <ul>
35 * <li>prop1.test1.value1</li>
36 * <li>prop1.test2.value2</li>
37 * <li>prop2.test1.value1</li>
38 * <li>prop1butnottherealprop1.test3.value3</li>
39 * </ul>
40 * Then filtering on "prop1" returns a map with values:
41 * <ul>
42 * <li>test1.value1</li>
43 * <li>test2.value2</li>
44 * </ul>
45 * </p>
46 *
47 * @author Kuali Student Team (ks.team2@kuali.org)
48 */
49 public class PropertiesFactory {
50
51 Properties properties = new Properties();
52
53 /**
54 * This constructs a PropertiesFactory.
55 *
56 * @param filename
57 * the filename of the properties file as determined by {@link ClassLoader#getSystemResource(String)}.
58 * @throws IOException
59 */
60 public PropertiesFactory(String filename) throws IOException {
61 this(ClassLoader.getSystemResource(filename).openStream());
62 }
63
64 /**
65 * This constructs a PropertiesFactory.
66 *
67 * @param file
68 * the properties file
69 * @throws FileNotFoundException
70 * @throws IOException
71 */
72 public PropertiesFactory(File file) throws FileNotFoundException, IOException {
73 this(new FileInputStream(file));
74 }
75
76 /**
77 * This constructs a PropertiesFactory.
78 *
79 * @param in
80 * an InputStream to read the properties from
81 * @throws IOException
82 */
83 public PropertiesFactory(InputStream in) throws IOException {
84 try {
85 properties.load(in);
86 } finally {
87 if (in != null) {
88 try {
89 in.close();
90 } catch (IOException e) {}
91 }
92 }
93 }
94
95 /**
96 * This method filters the properties based on the prefix given as described above. The prefix will be appended with a
97 * "." delimiter and the returned values will not have either the prefix or the delimiter.
98 *
99 * @param prefix
100 * the prefix to filter on
101 * @return a map of the properties minus their prefix. This list is really String,Object, oh well.
102 */
103 public Map<Object, Object> getProperties(String prefix) {
104 Map<Object, Object> map = new HashMap<Object, Object>();
105 for (Object key : properties.keySet()) {
106 String realPrefix = prefix + ".";
107 String keyString = key.toString();
108 if (keyString.startsWith(realPrefix)) {
109 map.put(keyString.substring(realPrefix.length()), properties.get(key));
110 }
111 }
112
113 return map;
114 }
115
116 /**
117 * A static version of the properties filter. <code><pre>
118 * <bean id="test" class="org.kuali.student.common.util.PropertiesFactory" factory-method="getProperties" >
119 * <constructor-arg value="file.properties"/>
120 * <constructor-arg value="prefix1"/>
121 * </bean>
122 * </pre></code>
123 *
124 * @param filename
125 * @param prefix
126 * the prefix to filter on
127 * @return a map of the properties minus their prefix. This list is really String,Object, oh well.
128 * @throws IOException
129 */
130 public static Map<Object, Object> getProperties(String filename, String prefix) throws IOException {
131 Properties props = new Properties();
132 InputStream in = null;
133 URL propFile = ClassLoader.getSystemResource(filename);
134 // System.out.println("\t===PROPFACT: "+propFile);
135 try {
136 props.load(in = propFile.openStream());
137 } finally {
138 if (in != null) {
139 try {
140 in.close();
141 } catch (IOException e) {}
142 }
143 }
144
145 Map<Object, Object> map = new HashMap<Object, Object>();
146 for (Object key : props.keySet()) {
147 String realPrefix = prefix + ".";
148 String keyString = key.toString();
149 if (keyString.startsWith(realPrefix)) {
150 map.put(keyString.substring(realPrefix.length()), props.get(key));
151 }
152 }
153
154 return map;
155 }
156
157 }