com.interface21.jndi
Class JndiTemplate

java.lang.Object
  |
  +--com.interface21.jndi.JndiTemplate

public class JndiTemplate
extends java.lang.Object

Helper class that simplifies JNDI operations. It provides methods to lookup and bind, and allows implementations of the ContextCallback interface to perform any operation they like with a JNDI naming context provided.

This is the central class in this package. It is analogous to the JdbcTemplate class. This class performs all error handling.

Author:
Rod Johnson, Juergen Hoeller
See Also:
ContextCallback, JdbcTemplate

Field Summary
protected  org.apache.commons.logging.Log logger
           
 
Constructor Summary
JndiTemplate()
          Create a new JndiTemplate instance.
JndiTemplate(java.util.Properties environment)
          Create a new JndiTemplate instance, using the given environment.
 
Method Summary
 void bind(java.lang.String name, java.lang.Object object)
          Bind the given object to the current JNDI context, using the given name.
protected  javax.naming.Context createInitialContext()
          Create a new initial context.
 java.lang.Object execute(ContextCallback contextCallback)
          Execute the given callback implementation.
 java.util.Properties getEnvironment()
           
 java.lang.Object lookup(java.lang.String name)
          Lookup the object with the given name in the current JNDI context.
 void setEnvironment(java.util.Properties environment)
          Set the environment for the InitialContext.
 void unbind(java.lang.String name)
          Remove the binding for the given name from the current JNDI context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final org.apache.commons.logging.Log logger
Constructor Detail

JndiTemplate

public JndiTemplate()
Create a new JndiTemplate instance.

JndiTemplate

public JndiTemplate(java.util.Properties environment)
Create a new JndiTemplate instance, using the given environment.
Method Detail

setEnvironment

public void setEnvironment(java.util.Properties environment)
Set the environment for the InitialContext.

getEnvironment

public java.util.Properties getEnvironment()

lookup

public java.lang.Object lookup(java.lang.String name)
                        throws javax.naming.NamingException
Lookup the object with the given name in the current JNDI context.
Parameters:
name - JNDI name of the object
Returns:
object found (cannot be null, if a not so well-behaved JNDI implementations returns null, a NamingException gets thrown)
Throws:
javax.naming.NamingException - if there is no object with the given name bound to JNDI

bind

public void bind(java.lang.String name,
                 java.lang.Object object)
          throws javax.naming.NamingException
Bind the given object to the current JNDI context, using the given name.
Parameters:
name - JNDI name of the object
object - object to bind
Throws:
javax.naming.NamingException - thrown by JNDI, mostly name already bound

unbind

public void unbind(java.lang.String name)
            throws javax.naming.NamingException
Remove the binding for the given name from the current JNDI context.
Parameters:
name - the JNDI name of the object
Throws:
javax.naming.NamingException - thrown by JNDI, mostly name not found

execute

public java.lang.Object execute(ContextCallback contextCallback)
                         throws javax.naming.NamingException
Execute the given callback implementation.
Parameters:
contextCallback - ContextCallback implementation
Returns:
a result object returned by the callback, or null
Throws:
javax.naming.NamingException - thrown by the callback implementation.

createInitialContext

protected javax.naming.Context createInitialContext()
                                             throws javax.naming.NamingException
Create a new initial context. The default implementation use this template's environment settings. Can be subclassed for custom contexts, e.g. for testing.
Returns:
the new InitialContext instance
Throws:
javax.naming.NamingException - in case of initialization errors


Rod Johnson and Spring contributors 2001-2003.