View Javadoc

1   /*
2      Licensed under the Apache License, Version 2.0 (the "License");
3      you may not use this file except in compliance with the License.
4      You may obtain a copy of the License at
5   
6        http://www.apache.org/licenses/LICENSE-2.0
7   
8      Unless required by applicable law or agreed to in writing, software
9      distributed under the License is distributed on an "AS IS" BASIS,
10     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11     See the License for the specific language governing permissions and
12     limitations under the License.
13  */
14  package uk.nhs.interoperability.util;
15  
16  import java.io.IOException;
17  import java.util.Map.Entry;
18  import java.util.Properties;
19  import java.util.Set;
20  
21  /**
22   * The Class ITKApplicationProperties.
23   *
24   * @author Michael Odling-Smee
25   * @author Nicholas Jones
26   * @since 0.1
27   */
28  public class ITKApplicationProperties {
29  
30  	/** The props. */
31  	private static Properties props = new Properties();
32  	static {
33  		try {
34  			props.load(ITKApplicationProperties.class.getResourceAsStream("/application.properties"));			
35  		} catch (IOException ex){
36  			ex.printStackTrace();
37  		}
38  	}
39  	
40  	/**
41  	 * Gets the property.
42  	 *
43  	 * @param propertyName the property name
44  	 * @return the property
45  	 */
46  	public static String getProperty(String propertyName){
47  		return props.getProperty(propertyName);
48  	}
49  	
50  	/**
51  	 * Gets the properties matching.
52  	 *
53  	 * @param prefix the prefix
54  	 * @return the properties matching
55  	 */
56  	public static Properties getPropertiesMatching(String prefix) {
57  		return getPropertiesMatching(props, prefix);
58  	}
59  	
60  	/**
61  	 * Gets the properties matching.
62  	 *
63  	 * @param props the props
64  	 * @param prefix the prefix
65  	 * @return the properties matching
66  	 */
67  	public static Properties getPropertiesMatching(Properties props, String prefix) {
68  		Properties matchingProperties = new Properties();
69  		Set<Entry<Object, Object>> entries = props.entrySet();
70  		for (Entry<Object, Object> entry: entries) {
71  			if (entry.getKey() instanceof String && ((String)entry.getKey()).startsWith(prefix)) {
72  				matchingProperties.setProperty((String)entry.getKey(), (String)entry.getValue());
73  			}
74  		}
75  		return matchingProperties;
76  	}
77  
78  }