001    /**
002     * Copyright 2010 The Kuali Foundation Licensed under the
003     * Educational Community License, Version 2.0 (the "License"); you may
004     * not use this file except in compliance with the License. You may
005     * obtain a copy of the License at
006     *
007     * http://www.osedu.org/licenses/ECL-2.0
008     *
009     * Unless required by applicable law or agreed to in writing,
010     * software distributed under the License is distributed on an "AS IS"
011     * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012     * or implied. See the License for the specific language governing
013     * permissions and limitations under the License.
014     */
015    
016    package org.kuali.student.common.util;
017    
018    import java.io.File;
019    import java.io.FileInputStream;
020    import java.io.FileNotFoundException;
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.net.URL;
024    import java.util.HashMap;
025    import java.util.Map;
026    import java.util.Properties;
027    
028    /**
029     * A really freaking simple properties filter that returns a properties map for a subset of a properties file. Give this
030     * method the file name of the properties file, and then give it the prefix to filter on. The prefix gets a "." appended to
031     * it on filtering.
032     * <p>
033     * For example, if the properties are:
034     * <ul>
035     * <li>prop1.test1.value1</li>
036     * <li>prop1.test2.value2</li>
037     * <li>prop2.test1.value1</li>
038     * <li>prop1butnottherealprop1.test3.value3</li>
039     * </ul>
040     * Then filtering on "prop1" returns a map with values:
041     * <ul>
042     * <li>test1.value1</li>
043     * <li>test2.value2</li>
044     * </ul>
045     * </p>
046     * 
047     * @author Kuali Student Team (ks.team2@kuali.org)
048     */
049    public class PropertiesFactory {
050    
051        Properties properties = new Properties();
052    
053        /**
054         * This constructs a PropertiesFactory.
055         * 
056         * @param filename
057         *            the filename of the properties file as determined by {@link ClassLoader#getSystemResource(String)}.
058         * @throws IOException
059         */
060        public PropertiesFactory(String filename) throws IOException {
061            this(ClassLoader.getSystemResource(filename).openStream());
062        }
063    
064        /**
065         * This constructs a PropertiesFactory.
066         * 
067         * @param file
068         *            the properties file
069         * @throws FileNotFoundException
070         * @throws IOException
071         */
072        public PropertiesFactory(File file) throws FileNotFoundException, IOException {
073            this(new FileInputStream(file));
074        }
075    
076        /**
077         * This constructs a PropertiesFactory.
078         * 
079         * @param in
080         *            an InputStream to read the properties from
081         * @throws IOException
082         */
083        public PropertiesFactory(InputStream in) throws IOException {
084            try {
085                properties.load(in);
086            } finally {
087                if (in != null) {
088                    try {
089                        in.close();
090                    } catch (IOException e) {}
091                }
092            }
093        }
094    
095        /**
096         * This method filters the properties based on the prefix given as described above. The prefix will be appended with a
097         * "." delimiter and the returned values will not have either the prefix or the delimiter.
098         * 
099         * @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         *     &lt;bean id=&quot;test&quot; class=&quot;org.kuali.student.common.util.PropertiesFactory&quot; factory-method=&quot;getProperties&quot; &gt;
119         *         &lt;constructor-arg value=&quot;file.properties&quot;/&gt;
120         *         &lt;constructor-arg value=&quot;prefix1&quot;/&gt;
121         *     &lt;/bean&gt;
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    }