org.springframework.jndi
Class AbstractJndiLocator

java.lang.Object
  extended byorg.springframework.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 when accessing a locally mapped (ENC - Environmental Naming Context) resource. If it doesn't, the "java:comp/env/" prefix will be prepended if the "resourceRef" property is true (the default is false) and no other scheme like "java:" is given.

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.

Subclasses will often be used as singletons in a bean container. This sometiems presents a problem if that bean container pre-instantiates singletons, since this class does the JNDI lookup in its init method, but the resource being pointed to may not exist at that time, even though it may exist at the time of first usage. The solution is to tell the bean container not to pre-instantiate this class (i.e. lazy load it instead).

Version:
$Id: AbstractJndiLocator.java,v 1.9 2004/03/18 02:46:17 trisberg Exp $
Author:
Rod Johnson
See Also:
setJndiTemplate(org.springframework.jndi.JndiTemplate), setJndiEnvironment(java.util.Properties), setResourceRef(boolean)

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 (and satisfied BeanFactoryAware and ApplicationContextAware).
 java.util.Properties getJndiEnvironment()
          Return the JNDI enviromment to use for the JNDI lookup.
 java.lang.String getJndiName()
          Return the JNDI name to look up.
 JndiTemplate getJndiTemplate()
          Return the JNDI template to use for the JNDI lookup.
 boolean isResourceRef()
          Return if the lookup occurs in a J2EE container.
protected abstract  void located(java.lang.Object jndiObject)
          Subclasses must implement this to cache the object this class has obtained from JNDI.
 void setJndiEnvironment(java.util.Properties jndiEnvironment)
          Set the JNDI environment to use for the JNDI lookup.
 void setJndiName(java.lang.String jndiName)
          Set the JNDI name.
 void setJndiTemplate(JndiTemplate jndiTemplate)
          Set the JNDI template to use for the JNDI lookup.
 void setResourceRef(boolean resourceRef)
          Set if the lookup occurs in a J2EE container, i.e.
 
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 jndiTemplate)
Set the JNDI template to use for the JNDI lookup. You can also specify JNDI environment settings via setJndiEnvironment.

See Also:
setJndiEnvironment(java.util.Properties)

getJndiTemplate

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


setJndiEnvironment

public final void setJndiEnvironment(java.util.Properties jndiEnvironment)
Set the JNDI environment to use for the JNDI lookup. Creates a JndiTemplate with the given environment settings.

See Also:
setJndiTemplate(org.springframework.jndi.JndiTemplate)

getJndiEnvironment

public final java.util.Properties getJndiEnvironment()
Return the JNDI enviromment 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 resourceRef is set to True.

Parameters:
jndiName - JNDI name of bean to look up
See Also:
setResourceRef(boolean)

getJndiName

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


setResourceRef

public void setResourceRef(boolean resourceRef)
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 false.

Note: Will only get applied if no other scheme like "java:" is given.


isResourceRef

public final boolean isResourceRef()
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 (and satisfied BeanFactoryAware and ApplicationContextAware).

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
Throws:
javax.naming.NamingException
java.lang.IllegalArgumentException

located

protected abstract void located(java.lang.Object jndiObject)
Subclasses must implement this to cache the object this class has obtained from JNDI.

Parameters:
jndiObject - object successfully retrieved from JNDI


Copyright (C) 2003-2004 The Spring Framework Project.