001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.core.api.util;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    import java.util.concurrent.Callable;
021    
022    /**
023     * Utility class that binds/unbinds the context ClassLoader of the current Thread
024     * using a ThreadLocal.
025     * NOTE: {@link #doInContextClassLoader(ClassLoader, java.util.concurrent.Callable)} is the only safe way to use this class,
026     * do not use deprecated {@link #bind(ClassLoader)} or {@link #unbind()} methods.
027     * 
028     * @author Kuali Rice Team (rice.collab@kuali.org)
029     */
030    public final class ContextClassLoaderBinder {
031            
032            /**
033         * Stack of previous context classloaders that should be
034         * restored on unbind
035         * @deprecated since 2.1, use #doInContextClassLoader. storing references to classloaders is an incredibly bad idea!
036         */
037        private static final ThreadLocal<List<ClassLoader>> STACK = new ThreadLocal<List<ClassLoader>>() {
038            protected List<ClassLoader> initialValue() {
039                return new ArrayList<ClassLoader>(5);
040            }
041        };
042    
043        /**
044         * @deprecated use #doInContextClassLoader
045         */
046        private static List<ClassLoader> getStack() {
047            return STACK.get();
048        }
049    
050        /**
051         * @deprecated use #doInContextClassLoader
052         */
053        public static void bind(ClassLoader cl) {
054            List<ClassLoader> stack = getStack();
055            Thread current = Thread.currentThread();
056            //log.debug("[bind] Switching CCL from " + current.getContextClassLoader() + " to " + cl);
057            // push the current context classloader on the stack
058            stack.add(current.getContextClassLoader());
059            current.setContextClassLoader(cl);
060        }
061    
062        /**
063         * @deprecated use #doInContextClassLoader
064         */
065        public static void unbind() {
066            List<ClassLoader> stack = getStack();
067            if (stack.size() == 0) {
068                throw new IllegalStateException("No context classloader to unbind!");
069            }
070            // pop the last context classloader off the stack
071            ClassLoader lastClassLoader = stack.get(stack.size() - 1);
072            //log.debug("[unbind] Switching CCL from " + Thread.currentThread().getContextClassLoader() + " to " + lastClassLoader);
073            stack.remove(stack.size() - 1);
074            Thread.currentThread().setContextClassLoader(lastClassLoader);
075        }
076    
077        /**
078         * Execute a runnable under a given context classloader
079         * @param cl the classloader to set as the thread context classloader
080         * @param callable the callable
081         */
082        public static <T> T doInContextClassLoader(ClassLoader cl, Callable<T> callable) throws Exception {
083            Thread current = Thread.currentThread();
084            ClassLoader prev = current.getContextClassLoader();
085            try {
086                current.setContextClassLoader(cl);
087                return callable.call();
088            } finally {
089                current.setContextClassLoader(prev);
090            }
091        }
092    }