001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.bean;
016    
017    import java.lang.reflect.InvocationHandler;
018    import java.lang.reflect.InvocationTargetException;
019    import java.lang.reflect.Method;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class ClassLoaderBeanHandler implements InvocationHandler {
025    
026            public ClassLoaderBeanHandler(Object bean, ClassLoader classLoader) {
027                    _bean = bean;
028                    _classLoader = classLoader;
029            }
030    
031            public Object getBean() {
032                    return _bean;
033            }
034    
035            public ClassLoader getClassLoader() {
036                    return _classLoader;
037            }
038    
039            @Override
040            public Object invoke(Object proxy, Method method, Object[] arguments)
041                    throws Throwable {
042    
043                    Thread currentThread = Thread.currentThread();
044    
045                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
046    
047                    try {
048                            if ((_classLoader != null) &&
049                                    (contextClassLoader != _classLoader)) {
050    
051                                    currentThread.setContextClassLoader(_classLoader);
052                            }
053    
054                            if (method.getDeclaringClass() == Object.class) {
055                                    String methodName = method.getName();
056    
057                                    if (methodName.equals("equals")) {
058                                            if (proxy == arguments[0]) {
059                                                    return true;
060                                            }
061                                            else {
062                                                    return false;
063                                            }
064                                    }
065                            }
066    
067                            return method.invoke(_bean, arguments);
068                    }
069                    catch (InvocationTargetException ite) {
070                            throw ite.getTargetException();
071                    }
072                    finally {
073                            if ((_classLoader != null) &&
074                                    (contextClassLoader != _classLoader)) {
075    
076                                    currentThread.setContextClassLoader(contextClassLoader);
077                            }
078                    }
079            }
080    
081            private Object _bean;
082            private ClassLoader _classLoader;
083    
084    }