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