001
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
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 }