Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
 
 package org.apache.naming.resources;
 
 import java.net.URL;
 
Stream handler to a JNDI directory context.

Author(s):
Remy Maucherat
Version:
$Revision: 1237 $
 
 public class DirContextURLStreamHandler 
     extends URLStreamHandler {
     
     
     // ----------------------------------------------------------- Constructors
     
     
     public DirContextURLStreamHandler() {
     }
     
     
     public DirContextURLStreamHandler(DirContext context) {
         this. = context;
     }
     
     
     // -------------------------------------------------------------- Variables
     
    
    
Bindings class loader - directory context. Keyed by CL id.
 
     private static Hashtable clBindings = new Hashtable();
    
    
    
Bindings thread - directory context. Keyed by thread id.
 
     private static Hashtable threadBindings = new Hashtable();
     
     
     // ----------------------------------------------------- Instance Variables
     
    
    
Directory context.
 
     protected DirContext context = null;
     
     
     // ------------------------------------------------------------- Properties
     
     
     // ----------------------------------------------- URLStreamHandler Methods
     
    
    
Opens a connection to the object referenced by the URL argument.
 
     protected URLConnection openConnection(URL u
         throws IOException {
         DirContext currentContext = this.;
         if (currentContext == null)
             currentContext = get();
         return new DirContextURLConnection(currentContextu);
     }
     
     
     // ------------------------------------------------------------ URL Methods
     
    
    
Override as part of the fix for 36534, to ensure toString is correct.
 
     protected String toExternalForm(URL u) {
        // pre-compute length of StringBuilder
        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();
        StringBuilder result = new StringBuilder(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 Methods
    
    
    
Set the java.protocol.handler.pkgs system property.
    public static void setProtocolHandler() {
        String value = System.getProperty(.);
        if (value == null) {
            value = .;
            System.setProperty(.value);
        } else if (value.indexOf(.) == -1) {
            value += "|" + .;
            System.setProperty(.value);
        }
    }
    
    
    
Returns true if the thread or the context class loader of the current thread is bound.
    public static boolean isBound() {
        return (.containsKey
                (Thread.currentThread().getContextClassLoader()))
            || (.containsKey(Thread.currentThread()));
    }
    
    
    
Binds a directory context to a class loader.
    public static void bind(DirContext dirContext) {
        ClassLoader currentCL = 
            Thread.currentThread().getContextClassLoader();
        if (currentCL != null)
            .put(currentCLdirContext);
    }
    
    
    
Unbinds a directory context to a class loader.
    public static void unbind() {
        ClassLoader currentCL = 
            Thread.currentThread().getContextClassLoader();
        if (currentCL != null)
            .remove(currentCL);
    }
    
    
    
Binds a directory context to a thread.
    public static void bindThread(DirContext dirContext) {
        .put(Thread.currentThread(), dirContext);
    }
    
    
    
Unbinds a directory context to a thread.
    public static void unbindThread() {
        .remove(Thread.currentThread());
    }
    
    
    
Get the bound context.
    public static DirContext get() {
        DirContext result = null;
        Thread currentThread = Thread.currentThread();
        ClassLoader currentCL = currentThread.getContextClassLoader();
        // Checking CL binding
        result = (DirContext.get(currentCL);
        if (result != null)
            return result;
        // Checking thread biding
        result = (DirContext.get(currentThread);
        // Checking parent CL binding
        currentCL = currentCL.getParent();
        while (currentCL != null) {
            result = (DirContext.get(currentCL);
            if (result != null)
                return result;
            currentCL = currentCL.getParent();
        }
        if (result == null)
            throw new IllegalStateException("Illegal class loader binding");
        return result;
    }
    
    
    
Binds a directory context to a class loader.
    public static void bind(ClassLoader clDirContext dirContext) {
        .put(cldirContext);
    }
    
    
    
Unbinds a directory context to a class loader.
    public static void unbind(ClassLoader cl) {
        .remove(cl);
    }
    
    
    
Get the bound context.
    public static DirContext get(ClassLoader cl) {
        return (DirContext.get(cl);
    }
    
    
    
Get the bound context.
    public static DirContext get(Thread thread) {
        return (DirContext.get(thread);
    }
    
    
New to GrepCode? Check out our FAQ X