KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jndi > JndiLocatorSupport


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jndi;
18
19 import javax.naming.NamingException JavaDoc;
20
21 import org.springframework.util.Assert;
22
23 /**
24  * Convenient superclass for classes that can locate any number of JNDI objects.
25  * Derives from JndiAccessor to inherit the "jndiTemplate" and "jndiEnvironment"
26  * bean properties.
27  *
28  * <p>JNDI names may or may not include the "java:comp/env/" prefix expected
29  * by J2EE applications when accessing a locally mapped (ENC - Environmental
30  * Naming Context) resource. If it doesn't, the "java:comp/env/" prefix will
31  * be prepended if the "resourceRef" property is true (the default is
32  * <strong>false</strong>) and no other scheme (e.g. "java:") is given.
33  *
34  * @author Juergen Hoeller
35  * @since 1.1
36  * @see #setJndiTemplate
37  * @see #setJndiEnvironment
38  * @see #setResourceRef
39  */

40 public abstract class JndiLocatorSupport extends JndiAccessor {
41
42     /** JNDI prefix used in a J2EE container */
43     public static final String JavaDoc CONTAINER_PREFIX = "java:comp/env/";
44
45
46     private boolean resourceRef = false;
47
48
49     /**
50      * Set whether the lookup occurs in a J2EE container, i.e. if the prefix
51      * "java:comp/env/" needs to be added if the JNDI name doesn't already
52      * contain it. Default is "false".
53      * <p>Note: Will only get applied if no other scheme, such as "java:",
54      * is given.
55      */

56     public void setResourceRef(boolean resourceRef) {
57         this.resourceRef = resourceRef;
58     }
59
60     /**
61      * Return whether the lookup occurs in a J2EE container.
62      */

63     public boolean isResourceRef() {
64         return this.resourceRef;
65     }
66
67
68     /**
69      * Perform an actual JNDI lookup for the given name via the JndiTemplate.
70    * If the name doesn't begin with "java:comp/env/", this prefix is added
71      * if resourceRef is set to true.
72      * @param jndiName the JNDI name to look up
73      * @throws NamingException if the JNDI lookup failed
74      * @see #setResourceRef
75      */

76     protected Object JavaDoc lookup(String JavaDoc jndiName) throws NamingException JavaDoc {
77         return lookup(jndiName, null);
78     }
79
80     /**
81      * Perform an actual JNDI lookup for the given name via the JndiTemplate.
82    * If the name doesn't begin with "java:comp/env/", this prefix is added
83      * if resourceRef is set to true.
84      * @param jndiName the JNDI name to look up
85      * @throws NamingException if the JNDI lookup failed
86      * @see #setResourceRef
87      */

88     protected Object JavaDoc lookup(String JavaDoc jndiName, Class JavaDoc requiredType) throws NamingException JavaDoc {
89         Assert.notNull(jndiName, "'jndiName' must not be null");
90         String JavaDoc jndiNameToUse = convertJndiName(jndiName);
91         Object JavaDoc jndiObject = getJndiTemplate().lookup(jndiNameToUse, requiredType);
92         if (logger.isDebugEnabled()) {
93             logger.debug("Located object with JNDI name [" + jndiNameToUse + "]");
94         }
95         return jndiObject;
96     }
97
98     /**
99      * Convert the given JNDI name to the actual JNDI name to use.
100      * Default implementation applies the "java:comp/env/" prefix if
101      * resourceRef is true and no other scheme like "java:" is given.
102      * @param jndiName the original JNDI name
103      * @return the JNDI name to use
104      * @see #CONTAINER_PREFIX
105      * @see #setResourceRef
106      */

107     protected String JavaDoc convertJndiName(String JavaDoc jndiName) {
108         // Prepend container prefix if not already specified and no other scheme given.
109
if (isResourceRef() && !jndiName.startsWith(CONTAINER_PREFIX) && jndiName.indexOf(':') == -1) {
110             jndiName = CONTAINER_PREFIX + jndiName;
111         }
112         return jndiName;
113     }
114
115 }
116
Popular Tags