Top Description Fields Constructors Methods
org.springframework.jndi

public Class JndiTemplate

extends Object
Class Inheritance
Imports
java.util.Hashtable, .Properties, javax.naming.Context, .InitialContext, .NameNotFoundException, .NamingException, org.apache.commons.logging.Log, .LogFactory, org.springframework.lang.Nullable, org.springframework.util.CollectionUtils

Helper class that simplifies JNDI operations. It provides methods to lookup and bind objects, and allows implementations of the JndiCallback interface to perform any operation they like with a JNDI naming context provided.
Authors
Rod Johnson, Juergen Hoeller
See Also
JndiCallback, execute

Field Summary

Modifier and TypeField and Description
private Properties
protected final Log

Constructor Summary

AccessConstructor and Description
public
JndiTemplate()

Create a new JndiTemplate instance.

public
JndiTemplate(Properties environment)

Create a new JndiTemplate instance, using the given environment.

Method Summary

Modifier and TypeMethod and Description
public void
bind(final String
the JNDI name of the object
name
,
final Object
the object to bind
object
)

Bind the given object to the current JNDI context, using the given name.

protected Context

Returns:

the initial Context instance
createInitialContext
()

Create a new JNDI initial context.

public <T> T

Returns:

a result object returned by the callback, or null
execute
(JndiCallback<T>
the JndiCallback implementation to use
contextCallback
)

Execute the given JNDI context callback implementation.

public Context

Returns:

the JNDI context (never null)
getContext
()

Obtain a JNDI context corresponding to this template's configuration.

public Properties
getEnvironment()

Return the environment for the JNDI InitialContext, if any.

public Object

Returns:

object found (cannot be null; if a not so well-behaved JNDI implementations returns null, a NamingException gets thrown)
lookup
(final String
the JNDI name of the object
name
)

Look up the object with the given name in the current JNDI context.

public <T> T

Returns:

object found (cannot be null; if a not so well-behaved JNDI implementations returns null, a NamingException gets thrown)
lookup
(String
the JNDI name of the object
name
,
Class<T>
type the JNDI object must match. Can be an interface or superclass of the actual class, or null for any match. For example, if the value is Object.class, this method will succeed whatever the class of the returned instance.
requiredType
)

Look up the object with the given name in the current JNDI context.

public void
rebind(final String
the JNDI name of the object
name
,
final Object
the object to rebind
object
)

Rebind the given object to the current JNDI context, using the given name.

public void
releaseContext(Context
the JNDI context to release (may be null)
ctx
)

Release a JNDI context as obtained from getContext().

public void
setEnvironment(Properties environment)

Set the environment for the JNDI InitialContext.

public void
unbind(final String
the JNDI name of the object
name
)

Remove the binding for the given name from the current JNDI context.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

environmentback to summary
private Properties environment
Annotations
@Nullable
loggerback to summary
protected final Log logger

Constructor Detail

JndiTemplateback to summary
public JndiTemplate()

Create a new JndiTemplate instance.

JndiTemplateback to summary
public JndiTemplate(Properties environment)

Create a new JndiTemplate instance, using the given environment.

Method Detail

bindback to summary
public void bind(final String name, final Object object) throws NamingException

Bind the given object to the current JNDI context, using the given name.

Parameters
name:String

the JNDI name of the object

object:Object

the object to bind

Exceptions
NamingException:
thrown by JNDI, mostly name already bound
createInitialContextback to summary
protected Context createInitialContext() throws NamingException

Create a new JNDI initial context. Invoked by getContext.

The default implementation use this template's environment settings. Can be subclassed for custom contexts, e.g. for testing.

Returns:Context

the initial Context instance

Exceptions
NamingException:
in case of initialization errors
executeback to summary
public <T> T execute(JndiCallback<T> contextCallback) throws NamingException

Execute the given JNDI context callback implementation.

Parameters
contextCallback:JndiCallback<T>

the JndiCallback implementation to use

Returns:T

a result object returned by the callback, or null

Annotations
@Nullable
Exceptions
NamingException:
thrown by the callback implementation
See Also
createInitialContext
getContextback to summary
public Context getContext() throws NamingException

Obtain a JNDI context corresponding to this template's configuration. Called by execute; may also be called directly.

The default implementation delegates to createInitialContext().

Returns:Context

the JNDI context (never null)

Exceptions
NamingException:
if context retrieval failed
See Also
releaseContext
getEnvironmentback to summary
public Properties getEnvironment()

Return the environment for the JNDI InitialContext, if any.

Annotations
@Nullable
lookupback to summary
public Object lookup(final String name) throws NamingException

Look up the object with the given name in the current JNDI context.

Parameters
name:String

the JNDI name of the object

Returns:Object

object found (cannot be null; if a not so well-behaved JNDI implementations returns null, a NamingException gets thrown)

Exceptions
NamingException:
if there is no object with the given name bound to JNDI
lookupback to summary
public <T> T lookup(String name, Class<T> requiredType) throws NamingException

Look up the object with the given name in the current JNDI context.

Parameters
name:String

the JNDI name of the object

requiredType:Class<T>

type the JNDI object must match. Can be an interface or superclass of the actual class, or null for any match. For example, if the value is Object.class, this method will succeed whatever the class of the returned instance.

Returns:T

object found (cannot be null; if a not so well-behaved JNDI implementations returns null, a NamingException gets thrown)

Annotations
@SuppressWarnings:unchecked
Exceptions
NamingException:
if there is no object with the given name bound to JNDI
rebindback to summary
public void rebind(final String name, final Object object) throws NamingException

Rebind the given object to the current JNDI context, using the given name. Overwrites any existing binding.

Parameters
name:String

the JNDI name of the object

object:Object

the object to rebind

Exceptions
NamingException:
thrown by JNDI
releaseContextback to summary
public void releaseContext(Context ctx)

Release a JNDI context as obtained from getContext().

Parameters
ctx:Context

the JNDI context to release (may be null)

See Also
getContext
setEnvironmentback to summary
public void setEnvironment(Properties environment)

Set the environment for the JNDI InitialContext.

unbindback to summary
public void unbind(final String name) throws NamingException

Remove the binding for the given name from the current JNDI context.

Parameters
name:String

the JNDI name of the object

Exceptions
NamingException:
thrown by JNDI, mostly name not found