Class JndiTemplate


  • public class JndiTemplate
    extends 
    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.

    Note that this implementation is an almost exact copy of the Spring Framework's identically named class from their 2.5.4 distribution - we didn't want to re-invent the wheel, but not require a full dependency on the Spring framework, nor does Spring make available only its JNDI classes in a small jar, or we would have used that. Since Shiro is also Apache 2.0 licensed, all regular licenses and conditions and authors have remained in tact.

    See Also:
    JndiCallback, execute(org.apache.shiro.jndi.JndiCallback)
    • Constructor Summary

      Constructors 
      Constructor Description
      JndiTemplate()
      Create a new JndiTemplate instance.
       environment)
      Create a new JndiTemplate instance, using the given environment.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void  name,  object)
      Bind the given object to the current JNDI context, using the given name.
      protected createInitialContext()
      Create a new JNDI initial context.
      execute​(JndiCallback contextCallback)
      Execute the given JNDI context callback implementation.
      getEnvironment()
      Return the environment for the JNDI InitialContext, or null if none should be used.
       name)
      Look up the object with the given name in the current JNDI context.
       name,  requiredType)
      Look up the object with the given name in the current JNDI context.
      void  name,  object)
      Rebind the given object to the current JNDI context, using the given name.
      void  environment)
      Set the environment for the JNDI InitialContext.
      void  name)
      Remove the binding for the given name from the current JNDI context.
      • Methods inherited from class java.lang.

        , , , , , , , , , ,
    • Constructor Detail

      • JndiTemplate

        public JndiTemplate()
        Create a new JndiTemplate instance.
      • JndiTemplate

        public JndiTemplate​( environment)
        Create a new JndiTemplate instance, using the given environment.
        Parameters:
        environment - the Properties to initialize with
    • Method Detail

      • setEnvironment

        public void setEnvironment​( environment)
        Set the environment for the JNDI InitialContext.
        Parameters:
        environment - the Properties to initialize with
      • getEnvironment

        public  getEnvironment()
        Return the environment for the JNDI InitialContext, or null if none should be used.
        Returns:
        the environment for the JNDI InitialContext, or null if none should be used.
      • execute

        public  execute​(JndiCallback contextCallback)
                       throws 
        Execute the given JNDI context callback implementation.
        Parameters:
        contextCallback - JndiCallback implementation
        Returns:
        a result object returned by the callback, or null
        Throws:
        - thrown by the callback implementation
        See Also:
        createInitialContext()
      • createInitialContext

        protected  createInitialContext()
                                        throws 
        Create a new JNDI initial context. Invoked by execute(org.apache.shiro.jndi.JndiCallback).

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

        Returns:
        the initial Context instance
        Throws:
        - in case of initialization errors
      • lookup

        public  lookup​( name)
                      throws 
        Look up the object with the given name in the current JNDI context.
        Parameters:
        name - the 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:
        - if there is no object with the given name bound to JNDI
      • lookup

        public  lookup​( name,
                              requiredType)
                      throws 
        Look up the object with the given name in the current JNDI context.
        Parameters:
        name - the JNDI name of the object
        requiredType - 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:
        object found (cannot be null; if a not so well-behaved JNDI implementations returns null, a NamingException gets thrown)
        Throws:
        - if there is no object with the given name bound to JNDI
      • bind

        public void bind​( name,
                          object)
                  throws 
        Bind the given object to the current JNDI context, using the given name.
        Parameters:
        name - the JNDI name of the object
        object - the object to bind
        Throws:
        - thrown by JNDI, mostly name already bound
      • rebind

        public void rebind​( name,
                            object)
                    throws 
        Rebind the given object to the current JNDI context, using the given name. Overwrites any existing binding.
        Parameters:
        name - the JNDI name of the object
        object - the object to rebind
        Throws:
        - thrown by JNDI
      • unbind

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