001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }