Class JNDIObjectLocator

  • All Implemented Interfaces:
    Serializable

    public class JNDIObjectLocator
    extends Object
    implements Serializable
    JNDIObjectLocator is used to centralize JNDI lookups. It minimizes the overhead of JNDI lookups by caching the objects it looks up.

    Implements the ServiceLocator design pattern.

    Major features are:

    • thread-safe
    • immutable
    • serializable
    • selectively disables the cache if objects are remote

    Example:

     
     locator = JNDIObjectLocator.builder().build();
     MyEJB myEJB1 = locator.getObject(MyEJB.class);
     MyEJB myEJB2 = locator.getObject("java:module/MyEJB");
     
     

    Example Code (GitHub)

    Since:
    3.9
    Author:
    Lenny Primak
    See Also:
    Serialized Form
    • Method Detail

      • getEnvEntry

        public <T> T getEnvEntry​(String name)
        Same as JNDI.getEnvEntry(String), except that this is cached.
        Type Parameters:
        T - The expected return type.
        Parameters:
        name - the environment entry name relative to "java:comp/env".
        Returns:
        The environment entry value associated with the given name, or null if there is none.
        Throws:
        ClassCastException - When T is of wrong type.
        See Also:
        JNDI.getEnvEntry(String)
      • getObject

        public <T> T getObject​(Class<T> beanClass)
        Returns an object from JNDI based on beanClass. Uses portable object names and convention to derive appropriate JNDI name.
        Type Parameters:
        T - Object type.
        Parameters:
        beanClass - Type of object to look up in JNDI.
        Returns:
        Resulting object, or null if there is none.
      • getObject

        public <T> T getObject​(String jndiName)
        Returns an object based on JNDI name.
        Type Parameters:
        T - The expected return type.
        Parameters:
        jndiName - The JNDI name of the object to be retrieved.
        Returns:
        The named object, or null if there is none.
        Throws:
        ClassCastException - When T is of wrong type.
      • getObjectNoCache

        public <T> T getObjectNoCache​(String jndiName)
        Return an object based on JNDI name, bypassing the cache.
        Type Parameters:
        T - The expected return type.
        Parameters:
        jndiName - The JNDI name of the object to be retrieved.
        Returns:
        The named object, or null if there is none.
        Throws:
        ClassCastException - When T is of wrong type.
      • clearCache

        public void clearCache()
        Clears object cache.
      • prependNamespaceIfNecessary

        public String prependNamespaceIfNecessary​(String fieldName)
        Utility method used in matching fields to EJB injection points to try to find appropriate JNDI object to use for injection. It prepends the given field name with this locator's namespace when the given field name does not already start with JNDI.JNDI_NAMESPACE_PREFIX.
        Parameters:
        fieldName - The field name to prepend with this locator's name space if necessary.
        Returns:
        The given field name, prepended with this locator's name space if necessary.