FileDocCategorySizeDatePackage
DirContextURLStreamHandler.javaAPI DocGlassfish v2 API8834Fri May 04 22:33:00 BST 2007org.apache.naming.resources

DirContextURLStreamHandler

public class DirContextURLStreamHandler extends URLStreamHandler
Stream handler to a JNDI directory context.
author
Remy Maucherat
version
$Revision: 1.4 $

Fields Summary
private static Hashtable
clBindings
Bindings class loader - directory context. Keyed by CL id.
private static Hashtable
threadBindings
Bindings thread - directory context. Keyed by thread id.
protected DirContext
context
Directory context.
Constructors Summary
public DirContextURLStreamHandler()

    
public DirContextURLStreamHandler(DirContext context)

        this.context = context;
    
Methods Summary
public static voidbind(java.lang.ClassLoader cl, javax.naming.directory.DirContext dirContext)
Binds a directory context to a class loader.

        clBindings.put(cl, dirContext);
    
public static voidbind(javax.naming.directory.DirContext dirContext)
Binds a directory context to a class loader.

        ClassLoader currentCL = 
            Thread.currentThread().getContextClassLoader();
        if (currentCL != null)
            clBindings.put(currentCL, dirContext);
    
public static voidbindThread(javax.naming.directory.DirContext dirContext)
Binds a directory context to a thread.

        threadBindings.put(Thread.currentThread(), dirContext);
    
public static javax.naming.directory.DirContextget()
Get the bound context.


        DirContext result = null;

        Thread currentThread = Thread.currentThread();
        ClassLoader currentCL = currentThread.getContextClassLoader();

        // Checking CL binding
        result = (DirContext) clBindings.get(currentCL);
        if (result != null)
            return result;

        // Checking thread biding
        result = (DirContext) threadBindings.get(currentThread);

        // Checking parent CL binding
        currentCL = currentCL.getParent();
        while (currentCL != null) {
            result = (DirContext) clBindings.get(currentCL);
            if (result != null)
                return result;
            currentCL = currentCL.getParent();
        }

        if (result == null)
            throw new IllegalStateException("Illegal class loader binding");

        return result;

    
public static javax.naming.directory.DirContextget(java.lang.ClassLoader cl)
Get the bound context.

        return (DirContext) clBindings.get(cl);
    
public static javax.naming.directory.DirContextget(java.lang.Thread thread)
Get the bound context.

        return (DirContext) threadBindings.get(thread);
    
public static booleanisBound()
Returns true if the thread or the context class loader of the current thread is bound.

        return (clBindings.containsKey
                (Thread.currentThread().getContextClassLoader()))
            || (threadBindings.containsKey(Thread.currentThread()));
    
protected java.net.URLConnectionopenConnection(java.net.URL u)
Opens a connection to the object referenced by the URL argument.

    
    
    // ------------------------------------------------------------- Properties
    
    
    // ----------------------------------------------- URLStreamHandler Methods
    
    
                     
        
          
        DirContext currentContext = this.context;
        if (currentContext == null)
            currentContext = get();
        return new DirContextURLConnection(currentContext, u);
    
public static voidsetProtocolHandler()
Set the java.protocol.handler.pkgs system property.

        String value = System.getProperty(Constants.PROTOCOL_HANDLER_VARIABLE);
        if (value == null) {
            value = Constants.Package;
            System.setProperty(Constants.PROTOCOL_HANDLER_VARIABLE, value);
        } else if (value.indexOf(Constants.Package) == -1) {
            value += "|" + Constants.Package;
            System.setProperty(Constants.PROTOCOL_HANDLER_VARIABLE, value);
        }
    
protected java.lang.StringtoExternalForm(java.net.URL u)
Converts a URL of a specific protocol to a String. The impl of this method is almost identical to that of the java.net.URLStreamHandler superclass, except that it omits the URL's authority field from the URL's String representation.

param
u the URL.
return
a string representation of the URL argument.


        // pre-compute length of StringBuffer
        int len = u.getProtocol().length() + 1;
        if (u.getPath() != null) {
            len += u.getPath().length();
        }
        if (u.getQuery() != null) {
            len += 1 + u.getQuery().length();
        }
        if (u.getRef() != null) 
            len += 1 + u.getRef().length();

        StringBuffer result = new StringBuffer(len);
        result.append(u.getProtocol());
        result.append(":");
        if (u.getPath() != null) {
            result.append(u.getPath());
        }
        if (u.getQuery() != null) {
            result.append('?");
            result.append(u.getQuery());
        }
        if (u.getRef() != null) {
            result.append("#");
            result.append(u.getRef());
        }
        return result.toString();
    
public static voidunbind(java.lang.ClassLoader cl)
Unbinds a directory context to a class loader.

        clBindings.remove(cl);
    
public static voidunbind()
Unbinds a directory context to a class loader.

        ClassLoader currentCL = 
            Thread.currentThread().getContextClassLoader();
        if (currentCL != null)
            clBindings.remove(currentCL);
    
public static voidunbindThread()
Unbinds a directory context to a thread.

        threadBindings.remove(Thread.currentThread());