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 import com.liferay.portal.kernel.servlet.PortletServlet;
020
021 import javax.servlet.ServletContext;
022
023
026 public class PortletClassInvoker {
027
028 public static Object invoke(
029 String portletId, String className, String methodName)
030 throws Exception {
031
032 return invoke(portletId, className, methodName, new Object[] {});
033 }
034
035 public static Object invoke(
036 String portletId, String className, String methodName, Object arg)
037 throws Exception {
038
039 return invoke(portletId, className, methodName, new Object[] {arg});
040 }
041
042 public static Object invoke(
043 String portletId, String className, String methodName, Object arg1,
044 Object arg2)
045 throws Exception {
046
047 return invoke(
048 portletId, className, methodName, new Object[] {arg1, arg2});
049 }
050
051 public static Object invoke(
052 String portletId, String className, String methodName, Object arg1,
053 Object arg2, Object arg3)
054 throws Exception {
055
056 return invoke(
057 portletId, className, methodName, new Object[] {arg1, arg2, arg3});
058 }
059
060 public static Object invoke(
061 String portletId, String className, String methodName, Object arg1,
062 Object arg2, Object arg3, Object arg4)
063 throws Exception {
064
065 return invoke(
066 portletId, className, methodName,
067 new Object[] {arg1, arg2, arg3, arg4});
068 }
069
070 public static Object invoke(
071 String portletId, String className, String methodName,
072 Object[] args)
073 throws Exception {
074
075 return invoke(portletId, className, methodName, args, true);
076 }
077
078 public static Object invoke(
079 String portletId,String className, String methodName,
080 boolean newInstance)
081 throws Exception {
082
083 return invoke(
084 portletId, className, methodName, new Object[] {}, newInstance);
085 }
086
087 public static Object invoke(
088 String portletId, String className, String methodName, Object arg,
089 boolean newInstance)
090 throws Exception {
091
092 return invoke(
093 portletId, className, methodName, new Object[] {arg}, newInstance);
094 }
095
096 public static Object invoke(
097 String portletId, String className, String methodName, Object arg1,
098 Object arg2, boolean newInstance)
099 throws Exception {
100
101 return invoke(
102 portletId, className, methodName, new Object[] {arg1, arg2},
103 newInstance);
104 }
105
106 public static Object invoke(
107 String portletId, String className, String methodName, Object arg1,
108 Object arg2, Object arg3, boolean newInstance)
109 throws Exception {
110
111 return invoke(
112 portletId, className, methodName, new Object[] {arg1, arg2, arg3},
113 newInstance);
114 }
115
116 public static Object invoke(
117 String portletId, String className, String methodName, Object arg1,
118 Object arg2, Object arg3, Object arg4, boolean newInstance)
119 throws Exception {
120
121 return invoke(
122 portletId, className, methodName,
123 new Object[] {arg1, arg2, arg3, arg4}, newInstance);
124 }
125
126 public static Object invoke(
127 String portletId, String className, String methodName,
128 Object[] args, boolean newInstance)
129 throws Exception {
130
131 portletId = _getRootPortletId(portletId);
132
133 ClassLoader portletClassLoader = PortalClassLoaderUtil.getClassLoader();
134
135 PortletBag portletBag = PortletBagPool.get(portletId);
136
137 if (portletBag != null) {
138 ServletContext servletContext = portletBag.getServletContext();
139
140 portletClassLoader = (ClassLoader)servletContext.getAttribute(
141 PortletServlet.PORTLET_CLASS_LOADER);
142 }
143
144 Thread currentThread = Thread.currentThread();
145
146 ClassLoader contextClassLoader = currentThread.getContextClassLoader();
147
148 try {
149 currentThread.setContextClassLoader(portletClassLoader);
150
151 MethodWrapper methodWrapper = new MethodWrapper(
152 className, methodName, args);
153
154 return MethodInvoker.invoke(methodWrapper, newInstance);
155 }
156 finally {
157 currentThread.setContextClassLoader(contextClassLoader);
158 }
159 }
160
161
164 private static String _getRootPortletId(String portletId) {
165 int pos = portletId.indexOf(_INSTANCE_SEPARATOR);
166
167 if (pos == -1) {
168 return portletId;
169 }
170 else {
171 return portletId.substring(0, pos);
172 }
173 }
174
175 private static final String _INSTANCE_SEPARATOR = "_INSTANCE_";
176
177 }