org.springframework.jndi

Class JndiTemplate

  • java.lang.Object
    • org.springframework.jndi.JndiTemplate
    • Field Summary

      Fields 
      Modifier and Type Field and Description
      protected Log logger 
    • Constructor Summary

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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      void bind(java.lang.String name, java.lang.Object object)
      Bind the given object to the current JNDI context, using the given name.
      protected javax.naming.Context createInitialContext()
      Create a new JNDI initial context.
      <T> T execute(JndiCallback<T> contextCallback)
      Execute the given JNDI context callback implementation.
      javax.naming.Context getContext()
      Obtain a JNDI context corresponding to this template's configuration.
      java.util.Properties getEnvironment()
      Return the environment for the JNDI InitialContext, if any.
      java.lang.Object lookup(java.lang.String name)
      Look up the object with the given name in the current JNDI context.
      <T> T lookup(java.lang.String name, java.lang.Class<T> requiredType)
      Look up the object with the given name in the current JNDI context.
      void rebind(java.lang.String name, java.lang.Object object)
      Rebind the given object to the current JNDI context, using the given name.
      void releaseContext(javax.naming.Context ctx)
      Release a JNDI context as obtained from getContext().
      void setEnvironment(java.util.Properties environment)
      Set the environment for the JNDI InitialContext.
      void unbind(java.lang.String name)
      Remove the binding for the given name from the current JNDI context.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • logger

        protected final Log logger
    • Constructor Detail

      • JndiTemplate

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

        public JndiTemplate(@Nullable
                            java.util.Properties environment)
        Create a new JndiTemplate instance, using the given environment.
    • Method Detail

      • setEnvironment

        public void setEnvironment(@Nullable
                                   java.util.Properties environment)
        Set the environment for the JNDI InitialContext.
      • getEnvironment

        @Nullable
        public java.util.Properties getEnvironment()
        Return the environment for the JNDI InitialContext, if any.
      • execute

        @Nullable
        public <T> T execute(JndiCallback<T> contextCallback)
                                throws javax.naming.NamingException
        Execute the given JNDI context callback implementation.
        Parameters:
        contextCallback - the JndiCallback implementation to use
        Returns:
        a result object returned by the callback, or null
        Throws:
        javax.naming.NamingException - thrown by the callback implementation
        See Also:
        createInitialContext()
      • releaseContext

        public void releaseContext(@Nullable
                                   javax.naming.Context ctx)
        Release a JNDI context as obtained from getContext().
        Parameters:
        ctx - the JNDI context to release (may be null)
        See Also:
        getContext()
      • createInitialContext

        protected javax.naming.Context createInitialContext()
                                                     throws javax.naming.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:
        the initial Context instance
        Throws:
        javax.naming.NamingException - in case of initialization errors
      • lookup

        public java.lang.Object lookup(java.lang.String name)
                                throws javax.naming.NamingException
        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:
        javax.naming.NamingException - if there is no object with the given name bound to JNDI
      • lookup

        public <T> T lookup(java.lang.String name,
                            @Nullable
                            java.lang.Class<T> requiredType)
                     throws javax.naming.NamingException
        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:
        javax.naming.NamingException - if there is no object with the given name bound to JNDI
      • bind

        public void bind(java.lang.String name,
                         java.lang.Object object)
                  throws javax.naming.NamingException
        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:
        javax.naming.NamingException - thrown by JNDI, mostly name already bound
      • rebind

        public void rebind(java.lang.String name,
                           java.lang.Object object)
                    throws javax.naming.NamingException
        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:
        javax.naming.NamingException - thrown by JNDI
      • unbind

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