1
19
20 package com.liferay.portal.kernel.util;
21
22 import com.liferay.portal.kernel.portlet.PortletBag;
23 import com.liferay.portal.kernel.portlet.PortletBagPool;
24 import com.liferay.portal.kernel.servlet.PortletServlet;
25
26 import javax.servlet.ServletContext;
27
28
34 public class PortletClassInvoker {
35
36 public static Object invoke(
37 String portletId, String className, String methodName)
38 throws Exception {
39
40 return invoke(portletId, className, methodName, new Object[] {});
41 }
42
43 public static Object invoke(
44 String portletId, String className, String methodName, Object arg)
45 throws Exception {
46
47 return invoke(portletId, className, methodName, new Object[] {arg});
48 }
49
50 public static Object invoke(
51 String portletId, String className, String methodName, Object arg1,
52 Object arg2)
53 throws Exception {
54
55 return invoke(
56 portletId, className, methodName, new Object[] {arg1, arg2});
57 }
58
59 public static Object invoke(
60 String portletId, String className, String methodName, Object arg1,
61 Object arg2, Object arg3)
62 throws Exception {
63
64 return invoke(
65 portletId, className, methodName, new Object[] {arg1, arg2, arg3});
66 }
67
68 public static Object invoke(
69 String portletId, String className, String methodName, Object arg1,
70 Object arg2, Object arg3, Object arg4)
71 throws Exception {
72
73 return invoke(
74 portletId, className, methodName,
75 new Object[] {arg1, arg2, arg3, arg4});
76 }
77
78 public static Object invoke(
79 String portletId, String className, String methodName,
80 Object[] args)
81 throws Exception {
82
83 return invoke(portletId, className, methodName, args, true);
84 }
85
86 public static Object invoke(
87 String portletId,String className, String methodName,
88 boolean newInstance)
89 throws Exception {
90
91 return invoke(
92 portletId, className, methodName, new Object[] {}, newInstance);
93 }
94
95 public static Object invoke(
96 String portletId, String className, String methodName, Object arg,
97 boolean newInstance)
98 throws Exception {
99
100 return invoke(
101 portletId, className, methodName, new Object[] {arg}, newInstance);
102 }
103
104 public static Object invoke(
105 String portletId, String className, String methodName, Object arg1,
106 Object arg2, boolean newInstance)
107 throws Exception {
108
109 return invoke(
110 portletId, className, methodName, new Object[] {arg1, arg2},
111 newInstance);
112 }
113
114 public static Object invoke(
115 String portletId, String className, String methodName, Object arg1,
116 Object arg2, Object arg3, boolean newInstance)
117 throws Exception {
118
119 return invoke(
120 portletId, className, methodName, new Object[] {arg1, arg2, arg3},
121 newInstance);
122 }
123
124 public static Object invoke(
125 String portletId, String className, String methodName, Object arg1,
126 Object arg2, Object arg3, Object arg4, boolean newInstance)
127 throws Exception {
128
129 return invoke(
130 portletId, className, methodName,
131 new Object[] {arg1, arg2, arg3, arg4}, newInstance);
132 }
133
134 public static Object invoke(
135 String portletId, String className, String methodName,
136 Object[] args, boolean newInstance)
137 throws Exception {
138
139 ClassLoader portletClassLoader = PortalClassLoaderUtil.getClassLoader();
140
141 PortletBag portletBag = PortletBagPool.get(portletId);
142
143 if (portletBag != null) {
144 ServletContext servletContext = portletBag.getServletContext();
145
146 portletClassLoader = (ClassLoader)servletContext.getAttribute(
147 PortletServlet.PORTLET_CLASS_LOADER);
148 }
149
150 Thread currentThread = Thread.currentThread();
151
152 ClassLoader contextClassLoader = currentThread.getContextClassLoader();
153
154 try {
155 currentThread.setContextClassLoader(portletClassLoader);
156
157 MethodWrapper methodWrapper = new MethodWrapper(
158 className, methodName, args);
159
160 return MethodInvoker.invoke(methodWrapper, newInstance);
161 }
162 finally {
163 currentThread.setContextClassLoader(contextClassLoader);
164 }
165 }
166
167 }