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 org.springframework.util.StringUtils;
21  
22  /**
23   * This class simplifies the setup of JNDI.
24   *
25   * @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/JndiConfigurationHelper.java $
26   *
27   * @author Andreas Bur (ABU)
28   */
29  public class JndiConfigurationHelper {
30  	
31  	/** The default JNDI context <code>java:comp/env</code>. */
32  	public static final String ENV_CONTEXT = "java:comp/env";
33  	
34  	/** The JNDI template. */
35  	private JndiTemplate m_jndiTemplate;
36  	
37  	/** The JNDI context. */
38  	private String m_context;
39  
40  	/**
41  	 * @return Returns the JNDI context.
42  	 */
43  	public String getContext() {
44  		if (!StringUtils.hasText(m_context)) {
45  			return ENV_CONTEXT;
46  		}
47  		return m_context;
48  	}
49  
50  	/**
51  	 * Sets the JNDI context. Default is <code>java:comp/env</code>.
52  	 *
53  	 * @param context
54  	 *      The JNDI context.
55  	 */
56  	public void setContext(String context) {
57  		m_context = context;
58  	}
59  
60  	/**
61  	 * @return Returns the JNDI template.
62  	 */
63  	public JndiTemplate getJndiTemplate() {
64  		if (m_jndiTemplate == null) {
65  			m_jndiTemplate = new JndiTemplate();
66  		}
67  		return m_jndiTemplate;
68  	}
69  
70  	/**
71  	 * Sets the JNDI template. Default ist {@link JndiTemplate}.
72  	 *
73  	 * @param jndiTemplate
74  	 *      The JNDI template.
75  	 */
76  	public void setJndiTemplate(JndiTemplate jndiTemplate) {
77  		m_jndiTemplate = jndiTemplate;
78  	}
79  	
80  	/**
81  	 * Makes a context-relative JNDI resource name absolute.
82  	 *
83  	 * @param name
84  	 *      A relative JNDI resource name.
85  	 *
86  	 * @return Returns the absolute representation of the given relative
87  	 *      JNDI resource name.
88  	 */
89  	public String buildJndiResourceName(String name) {
90  		String result;
91  		if (StringUtils.hasText(getContext())) {
92  			result = getContext() + "/" + name;
93  		} else {
94  			result = name;
95  		}
96  		return result;
97  	}
98  }