001
014
015 package com.liferay.portal.kernel.util;
016
017 import com.liferay.portal.kernel.portlet.PortletBag;
018 import com.liferay.portal.kernel.portlet.PortletBagPool;
019
020 import javax.servlet.ServletContext;
021
022
026 public class PortletClassInvoker {
027
028
032 @Deprecated
033 public static Object invoke(
034 boolean newInstance, String portletId, MethodKey methodKey,
035 Object... arguments)
036 throws Exception {
037
038 return invoke(portletId, methodKey, arguments);
039 }
040
041 public static Object invoke(
042 String portletId, MethodKey methodKey, Object... arguments)
043 throws Exception {
044
045 portletId = _getRootPortletId(portletId);
046
047 ClassLoader portletClassLoader = PortalClassLoaderUtil.getClassLoader();
048
049 PortletBag portletBag = PortletBagPool.get(portletId);
050
051 if (portletBag != null) {
052 ServletContext servletContext = portletBag.getServletContext();
053
054 portletClassLoader = servletContext.getClassLoader();
055 }
056
057 Thread currentThread = Thread.currentThread();
058
059 ClassLoader contextClassLoader = currentThread.getContextClassLoader();
060
061 try {
062 currentThread.setContextClassLoader(portletClassLoader);
063
064 MethodHandler methodHandler = new MethodHandler(
065 methodKey, arguments);
066
067 return methodHandler.invoke();
068 }
069 finally {
070 currentThread.setContextClassLoader(contextClassLoader);
071 }
072 }
073
074
077 private static String _getRootPortletId(String portletId) {
078 int pos = portletId.indexOf(_INSTANCE_SEPARATOR);
079
080 if (pos == -1) {
081 return portletId;
082 }
083 else {
084 return portletId.substring(0, pos);
085 }
086 }
087
088 private static final String _INSTANCE_SEPARATOR = "_INSTANCE_";
089
090 }