Class ContextFactory

  • All Implemented Interfaces:

    public class ContextFactory
    extends java.lang.Object
    implements javax.naming.spi.ObjectFactory

    This is an object factory that produces a jndi naming context based on a classloader.

    It is used for the java:comp context.

    This object factory is bound at java:comp. When a lookup arrives for java:comp, this object factory is invoked and will return a context specific to the caller's environment (so producing the java:comp/env specific to a webapp).

    The context selected is based on classloaders. First we try looking at the thread context classloader if it is set, and walk its hierarchy, creating a context if none is found. If the thread context classloader is not set, then we use the classloader associated with the current Context.

    If there is no current context, or no classloader, we return null.

    • Constructor Summary

      Constructor Description
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.ClassLoader associateClassLoader​(java.lang.ClassLoader loader)  
      static javax.naming.Context associateContext​(javax.naming.Context ctx)
      Associate the given Context with the current thread.
      static void disassociateClassLoader()  
      static void disassociateContext​(javax.naming.Context ctx)  
      static void dump​(java.lang.Appendable out, java.lang.String indent)  
      javax.naming.Context getContextForClassLoader​(java.lang.ClassLoader loader)
      Find the naming Context for the given classloader
      java.lang.Object getObjectInstance​(java.lang.Object obj, javax.naming.Name name, javax.naming.Context nameCtx, java.util.Hashtable env)
      Find or create a context which pertains to a classloader.
      NamingContext newNamingContext​(java.lang.Object obj, java.lang.ClassLoader loader, java.util.Hashtable env, javax.naming.Name name, javax.naming.Context parentCtx)
      Create a new NamingContext.
      • Methods inherited from class java.lang.Object

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

      • ContextFactory

        public ContextFactory()
    • Method Detail

      • getObjectInstance

        public java.lang.Object getObjectInstance​(java.lang.Object obj,
                                                  javax.naming.Name name,
                                                  javax.naming.Context nameCtx,
                                                  java.util.Hashtable env)
                                           throws java.lang.Exception
        Find or create a context which pertains to a classloader.

        If the thread context classloader is set, we try to find an already-created naming context for it. If one does not exist, we walk its classloader hierarchy until one is found, or we run out of parent classloaders. In the latter case, we will create a new naming context associated with the original thread context classloader.

        If the thread context classloader is not set, we obtain the classloader from the current jetty Context, and look for an already-created naming context.

        If there is no current jetty Context, or it has no associated classloader, we return null.

        Specified by:
        getObjectInstance in interface javax.naming.spi.ObjectFactory
        See Also:
        ObjectFactory.getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
      • newNamingContext

        public NamingContext newNamingContext​(java.lang.Object obj,
                                              java.lang.ClassLoader loader,
                                              java.util.Hashtable env,
                                              javax.naming.Name name,
                                              javax.naming.Context parentCtx)
                                       throws java.lang.Exception
        Create a new NamingContext.
        obj - the object to create
        loader - the classloader for the naming context
        env - the jndi env for the entry
        name - the name of the entry
        parentCtx - the parent context of the entry
        the newly created naming context
        java.lang.Exception - if unable to create a new naming context
      • getContextForClassLoader

        public javax.naming.Context getContextForClassLoader​(java.lang.ClassLoader loader)
        Find the naming Context for the given classloader
        loader - the classloader for the context
        the context for the classloader
      • associateContext

        public static javax.naming.Context associateContext​(javax.naming.Context ctx)
        Associate the given Context with the current thread. disassociate method should be called to reset the context.
        ctx - the context to associate to the current thread.
        the previous context associated on the thread (can be null)
      • disassociateContext

        public static void disassociateContext​(javax.naming.Context ctx)
      • associateClassLoader

        public static java.lang.ClassLoader associateClassLoader​(java.lang.ClassLoader loader)
      • disassociateClassLoader

        public static void disassociateClassLoader()
      • dump

        public static void dump​(java.lang.Appendable out,
                                java.lang.String indent)