| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ContextClassLoaderBinder |
|
| 1.5;1.5 | ||||
| ContextClassLoaderBinder$1 |
|
| 1.5;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.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 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger |
| 34 | .getLogger(ContextClassLoaderBinder.class); | |
| 35 | ||
| 36 | /** | |
| 37 | * Stack of previous context classloaders that should be | |
| 38 | * restored on unbind | |
| 39 | */ | |
| 40 | 0 | private static final ThreadLocal<List<ClassLoader>> STACK = new ThreadLocal<List<ClassLoader>>() { |
| 41 | protected List<ClassLoader> initialValue() { | |
| 42 | 0 | return new ArrayList<ClassLoader>(5); |
| 43 | } | |
| 44 | }; | |
| 45 | ||
| 46 | private static List<ClassLoader> getStack() { | |
| 47 | 0 | return STACK.get(); |
| 48 | } | |
| 49 | ||
| 50 | public static void bind(ClassLoader cl) { | |
| 51 | 0 | List<ClassLoader> stack = getStack(); |
| 52 | 0 | Thread current = Thread.currentThread(); |
| 53 | //log.debug("[bind] Switching CCL from " + current.getContextClassLoader() + " to " + cl); | |
| 54 | // push the current context classloader on the stack | |
| 55 | 0 | stack.add(current.getContextClassLoader()); |
| 56 | 0 | current.setContextClassLoader(cl); |
| 57 | 0 | } |
| 58 | ||
| 59 | public static void unbind() { | |
| 60 | 0 | List<ClassLoader> stack = getStack(); |
| 61 | 0 | if (stack.size() == 0) { |
| 62 | 0 | throw new IllegalStateException("No context classloader to unbind!"); |
| 63 | } | |
| 64 | // pop the last context classloader off the stack | |
| 65 | 0 | ClassLoader lastClassLoader = stack.get(stack.size() - 1); |
| 66 | //log.debug("[unbind] Switching CCL from " + Thread.currentThread().getContextClassLoader() + " to " + lastClassLoader); | |
| 67 | 0 | stack.remove(stack.size() - 1); |
| 68 | 0 | Thread.currentThread().setContextClassLoader(lastClassLoader); |
| 69 | 0 | } |
| 70 | } |