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) 2005 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  
18  package ch.elca.el4j.core.config;
19  
20  import java.util.Properties;
21  
22  import javax.naming.NamingException;
23  
24  import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
25  
26  
27  
28  /**
29   * This subclass of {@link
30   * org.springframework.beans.factory.config.PropertyPlaceholderConfigurer}
31   * acts a bit like {@link
32   * org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer}
33   * but searches for JNDI entries instead. This allows to configure an
34   * application more easily at deployment time through the container's admin
35   * console for instance.
36   *
37   * @svnLink $Revision: 3879 $;$Date: 2009-08-04 15:13:46 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/core/config/JndiPropertyPlaceholderConfigurer.java $
38   *
39   * @author Sylvain Laurent (SLA)
40   * @author Andreas Bur (ABU)
41   * @see ch.elca.el4j.core.config.JndiPropertyOverrideConfigurer
42   */
43  public class JndiPropertyPlaceholderConfigurer extends
44  	PropertyPlaceholderConfigurer {
45  
46  	/** The JNDI configuration helper. */
47  	private JndiConfigurationHelper m_jndiPropertyConfigurationHelper;
48  
49  	/**
50  	 * @return Returns the JNDI configuration helper.
51  	 */
52  	public JndiConfigurationHelper getJndiPropertyConfigurationHelper() {
53  		if (m_jndiPropertyConfigurationHelper == null) {
54  			m_jndiPropertyConfigurationHelper
55  				= new JndiConfigurationHelper();
56  		}
57  		return m_jndiPropertyConfigurationHelper;
58  	}
59  	
60  	/**
61  	 * Sets the JNDI configuration helper. Default is
62  	 * {@link JndiConfigurationHelper}.
63  	 *
64  	 * @param jndiPropertyConfigurationHelper
65  	 *      A JNDI configuration helper.
66  	 */
67  	public void setJndiPropertyConfigurationHelper(
68  			JndiConfigurationHelper jndiPropertyConfigurationHelper) {
69  		m_jndiPropertyConfigurationHelper = jndiPropertyConfigurationHelper;
70  	}
71  	
72  	/**
73  	 * {@inheritDoc}
74  	 */
75  	protected String resolvePlaceholder(String placeholder, Properties props) {
76  		String result = null;
77  		try {
78  			result = getJndiPropertyConfigurationHelper().getJndiTemplate().
79  				lookup(getJndiPropertyConfigurationHelper().
80  						buildJndiResourceName(placeholder)).toString();
81  		} catch (NamingException nex) {
82  			logger.error("Unable to resolve the placeholder '"
83  					+ placeholder + "' using JNDI. Fall back to property file."
84  					, nex);
85  		}
86  
87  		if (result == null) {
88  			result = super.resolvePlaceholder(placeholder, props);
89  		}
90  		return result;
91  	}
92  }