com.interface21.jndi
Class AbstractJndiLocator

java.lang.Object
  |
  +--com.interface21.jndi.AbstractJndiLocator
All Implemented Interfaces:
InitializingBean
Direct Known Subclasses:
AbstractSlsbInvokerInterceptor, JndiObjectFactoryBean

public abstract class AbstractJndiLocator
extends java.lang.Object
implements InitializingBean

Convenient superclass for JNDI-based Service Locators. Subclasses are JavaBeans, exposing a jndiName property. This may or may not include the "java:comp/env/" prefix expected by J2EE applications. If it doesn't, it will be prepended.

Subclasses must implement the located() method to cache the results of the JNDI lookup. They don't need to worry about error handling.

Assumptions: The resource obtained from JNDI can be cached.

Author:
Rod Johnson

Field Summary
static java.lang.String CONTAINER_PREFIX
          JNDI prefix used in a J2EE container
protected  org.apache.commons.logging.Log logger
           
 
Constructor Summary
AbstractJndiLocator()
          Create a new JNDI locator.
AbstractJndiLocator(java.lang.String jndiName)
          Create a new JNDI locator, specifying the JNDI name.
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied.
 java.lang.String getJndiName()
          Return the JNDI name to look up.
 JndiTemplate getJndiTemplate()
          Return the JNDI template to use for the JNDI lookup.
 boolean isInContainer()
          Return if the lookup occurs in a J2EE container.
protected abstract  void located(java.lang.Object o)
          Subclasses must implement this to cache the object this class has obtained from JNDI.
 void setInContainer(boolean inContainer)
          Set if the lookup occurs in a J2EE container, i.e. if the prefix "java:comp/env/" needs to be added if the JNDI name doesn't already contain it.
 void setJndiName(java.lang.String jndiName)
          Set the JNDI name.
 void setJndiTemplate(JndiTemplate template)
          Set the JNDI template to use for the JNDI lookup.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONTAINER_PREFIX

public static java.lang.String CONTAINER_PREFIX
JNDI prefix used in a J2EE container

logger

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

AbstractJndiLocator

public AbstractJndiLocator()
Create a new JNDI locator. The jndiName property must be set, and afterPropertiesSet be called to perform the JNDI lookup.

Obviously, this class is typically used via a BeanFactory.


AbstractJndiLocator

public AbstractJndiLocator(java.lang.String jndiName)
                    throws javax.naming.NamingException,
                           java.lang.IllegalArgumentException
Create a new JNDI locator, specifying the JNDI name. If the name doesn't include a java:comp/env/ prefix, it will be prepended.

As this is a shortcut, it calls afterPropertiesSet to perform the JNDI lookup immediately.

Parameters:
jndiName - JNDI name.
Method Detail

setJndiTemplate

public final void setJndiTemplate(JndiTemplate template)
Set the JNDI template to use for the JNDI lookup.

getJndiTemplate

public final JndiTemplate getJndiTemplate()
Return the JNDI template to use for the JNDI lookup.

setJndiName

public final void setJndiName(java.lang.String jndiName)
Set the JNDI name. If it doesn't begin "java:comp/env/" we add this prefix if we're running in a container.
Parameters:
jndiName - JNDI name of bean to look up
See Also:
setInContainer(boolean)

getJndiName

public final java.lang.String getJndiName()
Return the JNDI name to look up.

setInContainer

public final void setInContainer(boolean inContainer)
Set if the lookup occurs in a J2EE container, i.e. if the prefix "java:comp/env/" needs to be added if the JNDI name doesn't already contain it. Default is true.

isInContainer

public final boolean isInContainer()
Return if the lookup occurs in a J2EE container.

afterPropertiesSet

public final void afterPropertiesSet()
                              throws javax.naming.NamingException,
                                     java.lang.IllegalArgumentException
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied.
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
Specified by:
afterPropertiesSet in interface InitializingBean
Following copied from interface: com.interface21.beans.factory.InitializingBean
Throws:
java.lang.Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails.

located

protected abstract void located(java.lang.Object o)
Subclasses must implement this to cache the object this class has obtained from JNDI.
Parameters:
o - object successfully retrieved from JNDI


Rod Johnson and Spring contributors 2001-2003.