Coverage Report - org.kuali.rice.core.api.util.ContextClassLoaderBinder
 
Classes in this File Line Coverage Branch Coverage Complexity
ContextClassLoaderBinder
0%
0/15
0%
0/2
1.5
ContextClassLoaderBinder$1
0%
0/2
N/A
1.5
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.core.api.util;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 /**
 23  
  * Utility class that binds/unbinds the context ClassLoader of the current Thread
 24  
  * using a ThreadLocal.
 25  
  * This class supports re-entrancy by maintaining a stack of context classloaders.
 26  
  * NOTE: maybe implement stricter checks, by matching some contextual object or original
 27  
  * classloader on bind to the unbind so that they are always matched (within a given context)
 28  
  * 
 29  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 30  
  */
 31  0
 public final class ContextClassLoaderBinder {
 32  
         
 33  
         /**
 34  
      * Stack of previous context classloaders that should be
 35  
      * restored on unbind
 36  
      */
 37  0
     private static final ThreadLocal<List<ClassLoader>> STACK = new ThreadLocal<List<ClassLoader>>() {
 38  
         protected List<ClassLoader> initialValue() {
 39  0
             return new ArrayList<ClassLoader>(5);
 40  
         }
 41  
     };
 42  
 
 43  
     private static List<ClassLoader> getStack() {
 44  0
         return STACK.get();
 45  
     }
 46  
 
 47  
     public static void bind(ClassLoader cl) {
 48  0
         List<ClassLoader> stack = getStack();
 49  0
         Thread current = Thread.currentThread();
 50  
         //log.debug("[bind] Switching CCL from " + current.getContextClassLoader() + " to " + cl);
 51  
         // push the current context classloader on the stack
 52  0
         stack.add(current.getContextClassLoader());
 53  0
         current.setContextClassLoader(cl);
 54  0
     }
 55  
 
 56  
     public static void unbind() {
 57  0
         List<ClassLoader> stack = getStack();
 58  0
         if (stack.size() == 0) {
 59  0
             throw new IllegalStateException("No context classloader to unbind!");
 60  
         }
 61  
         // pop the last context classloader off the stack
 62  0
         ClassLoader lastClassLoader = stack.get(stack.size() - 1);
 63  
         //log.debug("[unbind] Switching CCL from " + Thread.currentThread().getContextClassLoader() + " to " + lastClassLoader);
 64  0
         stack.remove(stack.size() - 1);
 65  0
         Thread.currentThread().setContextClassLoader(lastClassLoader);
 66  0
     }
 67  
 }