View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2006 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.util.env;
18  
19  import java.util.Properties;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  import org.springframework.core.io.support.ResourcePatternResolver;
24  
25  import ch.elca.el4j.env.beans.EnvPropertyOverrideConfigurer;
26  import ch.elca.el4j.env.beans.EnvPropertyPlaceholderConfigurer;
27  import ch.elca.el4j.env.xml.EnvXml;
28  import ch.elca.el4j.util.codingsupport.CollectionUtils;
29  import ch.elca.el4j.util.codingsupport.PropertiesHelper;
30  
31  /**
32   * This class provides access to the currently used environment properties.
33   *
34   * @svnLink $Revision: 3874 $;$Date: 2009-08-04 14:25:40 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/env/src/main/java/ch/elca/el4j/util/env/EnvPropertiesUtils.java $
35   *
36   * @author Alex Mathey (AMA)
37   * @author Martin Zeltner (MZE)
38   */
39  public class EnvPropertiesUtils {
40  	/**
41  	 * Private logger of this class.
42  	 */
43  	private static Logger s_logger
44  		= LoggerFactory.getLogger(CollectionUtils.class);
45  	
46  	/**
47  	 * Hide default constructor.
48  	 */
49  	protected EnvPropertiesUtils() { }
50  	
51  	/**
52  	 * Retrieves the currently used environment properties.
53  	 * @return The currently used environment properties.
54  	 *
55  	 * @deprecated Use method {@link #getEnvPlaceholderProperties()} instead.
56  	 */
57  	@Deprecated
58  	public static Properties getEnvProperties() {
59  		s_logger.debug(
60  			"DEPRECATED: Use method 'getEnvPlaceholderProperties' instead.");
61  		return getEnvPlaceholderProperties();
62  	}
63  	
64  	/**
65  	 * Retrieves the currently used placeholder environment properties.
66  	 * @return The currently used placeholder environment properties.
67  	 */
68  	public static Properties getEnvPlaceholderProperties() {
69  		return getEnvPlaceholderProperties(null, true);
70  	}
71  	
72  	/**
73  	 * Retrieves the currently used placeholder environment properties.
74  	 * @param resourcePatternResolver     the resolver to ask for the resources
75  	 * @param mostSpecificResourceLast    is most specific resource last
76  	 * @return The currently used placeholder environment properties.
77  	 */
78  	public static Properties getEnvPlaceholderProperties(
79  		ResourcePatternResolver resourcePatternResolver, boolean mostSpecificResourceLast) {
80  		Properties properties;
81  		EnvXml parser;
82  		if (resourcePatternResolver != null) {
83  			parser = new EnvXml(resourcePatternResolver, mostSpecificResourceLast);
84  		} else {
85  			parser = new EnvXml();
86  		}
87  		if (parser.hasValidConfigurations()) {
88  			properties = (Properties) parser.getGroupConfiguration(EnvXml.ENV_GROUP_PLACEHOLDERS);
89  		} else {
90  			properties = new Properties();
91  		}
92  		properties.putAll(new PropertiesHelper().loadProperties(
93  			EnvPropertyPlaceholderConfigurer.ENV_PLACEHOLDER_PROPERTIES_LOCATION));
94  		
95  		return properties;
96  	}
97  	/**
98  	 * Retrieves the currently used bean property environment properties.
99  	 * @return The currently used bean override properties.
100 	 */
101 	public static Properties getEnvBeanPropertyProperties() {
102 		return getEnvBeanPropertyProperties(null, true);
103 	}
104 	/**
105 	 * Retrieves the currently used bean property environment properties.
106 	 * @param resourcePatternResolver     the resolver to ask for the resources
107 	 * @param mostSpecificResourceLast    is most specific resource last
108 	 * @return The currently used bean override properties.
109 	 */
110 	public static Properties getEnvBeanPropertyProperties(
111 		ResourcePatternResolver resourcePatternResolver, boolean mostSpecificResourceLast) {
112 		
113 		Properties properties;
114 		EnvXml parser;
115 		if (resourcePatternResolver != null) {
116 			parser = new EnvXml(resourcePatternResolver, mostSpecificResourceLast);
117 		} else {
118 			parser = new EnvXml();
119 		}
120 		if (parser.hasValidConfigurations()) {
121 			properties = (Properties) parser.getGroupConfiguration(EnvXml.ENV_GROUP_BEAN_OVERRIDES);
122 		} else {
123 			properties = new Properties();
124 		}
125 		properties.putAll(new PropertiesHelper().loadProperties(
126 			EnvPropertyOverrideConfigurer.ENV_BEAN_PROPERTY_PROPERTIES_LOCATION));
127 		
128 		return properties;
129 	}
130 }