KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Properties JavaDoc;
20
21 import javax.naming.Context JavaDoc;
22 import javax.naming.InitialContext JavaDoc;
23 import javax.naming.NameNotFoundException JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30  * Helper class that simplifies JNDI operations. It provides methods to lookup and
31  * bind objects, and allows implementations of the {@link JndiCallback} interface
32  * to perform any operation they like with a JNDI naming context provided.
33  *
34  * @author Rod Johnson
35  * @author Juergen Hoeller
36  * @see JndiCallback
37  * @see #execute
38  */

39 public class JndiTemplate {
40     
41     protected final Log logger = LogFactory.getLog(getClass());
42
43     private Properties JavaDoc environment;
44
45
46     /**
47      * Create a new JndiTemplate instance.
48      */

49     public JndiTemplate() {
50     }
51
52     /**
53      * Create a new JndiTemplate instance, using the given environment.
54      */

55     public JndiTemplate(Properties JavaDoc environment) {
56         this.environment = environment;
57     }
58
59
60     /**
61      * Set the environment for the JNDI InitialContext.
62      */

63     public void setEnvironment(Properties JavaDoc environment) {
64         this.environment = environment;
65     }
66
67     /**
68      * Return the environment for the JNDI InitialContext, if any.
69      */

70     public Properties JavaDoc getEnvironment() {
71         return this.environment;
72     }
73
74
75     /**
76      * Execute the given JNDI context callback implementation.
77      * @param contextCallback JndiCallback implementation
78      * @return a result object returned by the callback, or <code>null</code>
79      * @throws NamingException thrown by the callback implementation
80      * @see #createInitialContext
81      */

82     public Object JavaDoc execute(JndiCallback contextCallback) throws NamingException JavaDoc {
83         Context JavaDoc ctx = createInitialContext();
84         try {
85             return contextCallback.doInContext(ctx);
86         }
87         finally {
88             try {
89                 ctx.close();
90             }
91             catch (NamingException JavaDoc ex) {
92                 logger.debug("Could not close JNDI InitialContext", ex);
93             }
94         }
95     }
96
97     /**
98      * Create a new JNDI initial context. Invoked by execute.
99      * The default implementation use this template's environment settings.
100      * Can be subclassed for custom contexts, e.g. for testing.
101      * @return the initial Context instance
102      * @throws NamingException in case of initialization errors
103      */

104     protected Context JavaDoc createInitialContext() throws NamingException JavaDoc {
105         return new InitialContext JavaDoc(getEnvironment());
106     }
107
108
109     /**
110      * Look up the object with the given name in the current JNDI context.
111      * @param name the JNDI name of the object
112      * @return object found (cannot be <code>null</code>; if a not so well-behaved
113      * JNDI implementations returns null, a NamingException gets thrown)
114      * @throws NamingException if there is no object with the given
115      * name bound to JNDI
116      */

117     public Object JavaDoc lookup(final String JavaDoc name) throws NamingException JavaDoc {
118         if (logger.isDebugEnabled()) {
119             logger.debug("Looking up JNDI object with name [" + name + "]");
120         }
121         return execute(new JndiCallback() {
122             public Object JavaDoc doInContext(Context JavaDoc ctx) throws NamingException JavaDoc {
123                 Object JavaDoc located = ctx.lookup(name);
124                 if (located == null) {
125                     throw new NameNotFoundException JavaDoc(
126                             "JNDI object with [" + name + "] not found: JNDI implementation returned null");
127                 }
128                 return located;
129             }
130         });
131     }
132
133     /**
134      * Look up the object with the given name in the current JNDI context.
135      * @param name the JNDI name of the object
136      * @param requiredType type the JNDI object must match. Can be an interface or
137      * superclass of the actual class, or <code>null</code> for any match. For example,
138      * if the value is <code>Object.class</code>, this method will succeed whatever
139      * the class of the returned instance.
140      * @return object found (cannot be <code>null</code>; if a not so well-behaved
141      * JNDI implementations returns null, a NamingException gets thrown)
142      * @throws NamingException if there is no object with the given
143      * name bound to JNDI
144      */

145     public Object JavaDoc lookup(String JavaDoc name, Class JavaDoc requiredType) throws NamingException JavaDoc {
146         Object JavaDoc jndiObject = lookup(name);
147         if (requiredType != null && !requiredType.isInstance(jndiObject)) {
148             throw new TypeMismatchNamingException(
149                     "Object [" + jndiObject + "] available at JNDI location [" +
150                     name + "] is not assignable to [" + requiredType.getName() + "]");
151         }
152         return jndiObject;
153     }
154
155     /**
156      * Bind the given object to the current JNDI context, using the given name.
157      * @param name the JNDI name of the object
158      * @param object the object to bind
159      * @throws NamingException thrown by JNDI, mostly name already bound
160      */

161     public void bind(final String JavaDoc name, final Object JavaDoc object) throws NamingException JavaDoc {
162         if (logger.isDebugEnabled()) {
163             logger.debug("Binding JNDI object with name [" + name + "]");
164         }
165         execute(new JndiCallback() {
166             public Object JavaDoc doInContext(Context JavaDoc ctx) throws NamingException JavaDoc {
167                 ctx.bind(name, object);
168                 return null;
169             }
170         });
171     }
172     
173     /**
174      * Rebind the given object to the current JNDI context, using the given name.
175      * Overwrites any existing binding.
176      * @param name the JNDI name of the object
177      * @param object the object to rebind
178      * @throws NamingException thrown by JNDI
179      */

180     public void rebind(final String JavaDoc name, final Object JavaDoc object) throws NamingException JavaDoc {
181         if (logger.isDebugEnabled()) {
182             logger.debug("Rebinding JNDI object with name [" + name + "]");
183         }
184         execute(new JndiCallback() {
185             public Object JavaDoc doInContext(Context JavaDoc ctx) throws NamingException JavaDoc {
186                 ctx.rebind(name, object);
187                 return null;
188             }
189         });
190     }
191
192     /**
193      * Remove the binding for the given name from the current JNDI context.
194      * @param name the JNDI name of the object
195      * @throws NamingException thrown by JNDI, mostly name not found
196      */

197     public void unbind(final String JavaDoc name) throws NamingException JavaDoc {
198         if (logger.isDebugEnabled()) {
199             logger.debug("Unbinding JNDI object with name [" + name + "]");
200         }
201         execute(new JndiCallback() {
202             public Object JavaDoc doInContext(Context JavaDoc ctx) throws NamingException JavaDoc {
203                 ctx.unbind(name);
204                 return null;
205             }
206         });
207     }
208     
209 }
210
Popular Tags