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.bean;
016    
017    import com.liferay.portal.kernel.bean.IdentifiableBean;
018    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
019    import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
020    import com.liferay.portal.kernel.util.ClassLoaderPool;
021    import com.liferay.portal.kernel.util.MethodHandler;
022    import com.liferay.portal.kernel.util.MethodKey;
023    import com.liferay.portal.util.ClassLoaderUtil;
024    import com.liferay.portal.util.PortalUtil;
025    
026    import org.aopalliance.intercept.MethodInvocation;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class IdentifiableBeanInvokerUtil {
032    
033            public static MethodHandler createMethodHandler(
034                    MethodInvocation methodInvocation) {
035    
036                    MethodHandler methodHandler = new MethodHandler(
037                            methodInvocation.getMethod(), methodInvocation.getArguments());
038    
039                    String threadContextServletContextName = ClassLoaderPool.getContextName(
040                            ClassLoaderUtil.getContextClassLoader());
041    
042                    IdentifiableBean identifiableBean =
043                            (IdentifiableBean)methodInvocation.getThis();
044    
045                    Class<?> identifiableBeanClass = identifiableBean.getClass();
046    
047                    String identifiableBeanServletContextName =
048                            ClassLoaderPool.getContextName(
049                                    identifiableBeanClass.getClassLoader());
050    
051                    String beanIdentifier = identifiableBean.getBeanIdentifier();
052    
053                    return new MethodHandler(
054                            _invokeMethodKey, methodHandler, threadContextServletContextName,
055                            identifiableBeanServletContextName, beanIdentifier);
056            }
057    
058            @SuppressWarnings("unused")
059            private static Object _invoke(
060                            MethodHandler methodHandler, String threadContextServletContextName,
061                            String identifiableBeanServletContextName, String beanIdentifier)
062                    throws Exception {
063    
064                    ClassLoader contextClassLoader =
065                            ClassLoaderUtil.getContextClassLoader();
066    
067                    ClassLoader classLoader = ClassLoaderPool.getClassLoader(
068                            threadContextServletContextName);
069    
070                    ClassLoaderUtil.setContextClassLoader(classLoader);
071    
072                    try {
073                            Object bean = null;
074    
075                            if (identifiableBeanServletContextName.equals(
076                                            PortalUtil.getServletContextName())) {
077    
078                                    bean = PortalBeanLocatorUtil.locate(beanIdentifier);
079                            }
080                            else {
081                                    bean = PortletBeanLocatorUtil.locate(
082                                            identifiableBeanServletContextName, beanIdentifier);
083                            }
084    
085                            return methodHandler.invoke(bean);
086                    }
087                    finally {
088                            ClassLoaderUtil.setContextClassLoader(contextClassLoader);
089                    }
090            }
091    
092            private static MethodKey _invokeMethodKey = new MethodKey(
093                    IdentifiableBeanInvokerUtil.class, "_invoke", MethodHandler.class,
094                    String.class, String.class, String.class);
095    
096    }