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            public Object invoke(Object proxy, Method method, Object[] arguments)
040                    throws Throwable {
041    
042                    Thread currentThread = Thread.currentThread();
043    
044                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
045    
046                    try {
047                            if ((_classLoader != null) &&
048                                    (contextClassLoader != _classLoader)) {
049    
050                                    currentThread.setContextClassLoader(_classLoader);
051                            }
052    
053                            if (method.getDeclaringClass() == Object.class) {
054                                    String methodName = method.getName();
055    
056                                    if (methodName.equals("equals")) {
057                                            if (proxy == arguments[0]) {
058                                                    return true;
059                                            }
060                                            else {
061                                                    return false;
062                                            }
063                                    }
064                            }
065    
066                            return method.invoke(_bean, arguments);
067                    }
068                    catch (InvocationTargetException ite) {
069                            throw ite.getTargetException();
070                    }
071                    finally {
072                            if ((_classLoader != null) &&
073                                    (contextClassLoader != _classLoader)) {
074    
075                                    currentThread.setContextClassLoader(contextClassLoader);
076                            }
077                    }
078            }
079    
080            private Object _bean;
081            private ClassLoader _classLoader;
082    
083    }