View Javadoc

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