KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.factory.InitializingBean;
22 import org.springframework.util.StringUtils;
23
24 /**
25  * Convenient superclass for JNDI-based service locators,
26  * providing configurable lookup of a specific JNDI resource.
27  *
28  * <p>Exposes a {@link #setJndiName "jndiName"} property. This may or may not
29  * include the "java:comp/env/" prefix expected by J2EE applications when
30  * accessing a locally mapped (Environmental Naming Context) resource. If it
31  * doesn't, the "java:comp/env/" prefix will be prepended if the "resourceRef"
32  * property is true (the default is <strong>false</strong>) and no other scheme
33  * (e.g. "java:") is given.
34  *
35  * <p>Subclasses may invoke the lookup method whenever it is appropriate.
36  * Some classes might do this on initialization, while others might do it
37  * on demand. The latter strategy is more flexible in that it allows for
38  * initialization of the locator before the JNDI object is available.
39  *
40  * @author Juergen Hoeller
41  * @since 1.1
42  * @see #setJndiName
43  * @see #setJndiTemplate
44  * @see #setJndiEnvironment
45  * @see #setResourceRef
46  * @see #lookup()
47  */

48 public abstract class JndiObjectLocator extends JndiLocatorSupport implements InitializingBean {
49
50     private String JavaDoc jndiName;
51
52     private Class JavaDoc expectedType;
53
54
55     /**
56      * Set the JNDI name to look up. If it doesn't begin with "java:comp/env/"
57      * this prefix is added if resourceRef is set to true.
58      * @param jndiName JNDI name to look up
59      * @see #setResourceRef
60      */

61     public void setJndiName(String JavaDoc jndiName) {
62         this.jndiName = jndiName;
63     }
64
65     /**
66      * Return the JNDI name to look up.
67      */

68     public String JavaDoc getJndiName() {
69         return this.jndiName;
70     }
71
72     /**
73      * Set the type that the located JNDI object is supposed
74      * to be assignable to, if any.
75      */

76     public void setExpectedType(Class JavaDoc expectedType) {
77         this.expectedType = expectedType;
78     }
79
80     /**
81      * Return the type that the located JNDI object is supposed
82      * to be assignable to, if any.
83      */

84     public Class JavaDoc getExpectedType() {
85         return this.expectedType;
86     }
87
88     public void afterPropertiesSet() throws IllegalArgumentException JavaDoc, NamingException JavaDoc {
89         if (!StringUtils.hasLength(getJndiName())) {
90             throw new IllegalArgumentException JavaDoc("Property 'jndiName' is required");
91         }
92     }
93
94
95     /**
96      * Perform the actual JNDI lookup for this locator's target resource.
97      * @return the located target object
98      * @throws NamingException if the JNDI lookup failed or if the
99      * located JNDI object is not assigable to the expected type
100      * @see #setJndiName
101      * @see #setExpectedType
102      * @see #lookup(String, Class)
103      */

104     protected Object JavaDoc lookup() throws NamingException JavaDoc {
105         return lookup(getJndiName(), getExpectedType());
106     }
107
108 }
109
Popular Tags