001
014
015 package com.liferay.portal.kernel.util;
016
017 import com.liferay.portal.kernel.exception.SystemException;
018
019 import java.lang.reflect.InvocationTargetException;
020
021
025 public class PortalClassInvoker {
026
027 public static Object invoke(
028 boolean newInstance, MethodKey methodKey, Object... arguments)
029 throws Exception {
030
031 Thread currentThread = Thread.currentThread();
032
033 ClassLoader contextClassLoader = currentThread.getContextClassLoader();
034
035 try {
036 currentThread.setContextClassLoader(
037 PortalClassLoaderUtil.getClassLoader());
038
039 MethodHandler methodHandler = new MethodHandler(
040 methodKey, arguments);
041
042 return methodHandler.invoke(newInstance);
043 }
044 catch (InvocationTargetException ite) {
045 Throwable cause = ite.getCause();
046
047 if (cause instanceof Error) {
048 throw new SystemException(ite);
049 }
050 else {
051 throw (Exception)cause;
052 }
053 }
054 finally {
055 currentThread.setContextClassLoader(contextClassLoader);
056 }
057 }
058
059 public static Object invoke(
060 boolean newInstance, String className, String methodName,
061 String[] parameterTypeNames, Object... arguments)
062 throws Exception {
063
064 Thread currentThread = Thread.currentThread();
065
066 ClassLoader contextClassLoader = currentThread.getContextClassLoader();
067
068 try {
069 currentThread.setContextClassLoader(
070 PortalClassLoaderUtil.getClassLoader());
071
072 MethodKey methodKey = new MethodKey(
073 className, methodName, parameterTypeNames);
074
075 MethodHandler methodHandler = new MethodHandler(
076 methodKey, arguments);
077
078 return methodHandler.invoke(newInstance);
079 }
080 catch (InvocationTargetException ite) {
081 Throwable cause = ite.getCause();
082
083 if (cause instanceof Error) {
084 throw new SystemException(ite);
085 }
086 else {
087 throw (Exception)cause;
088 }
089 }
090 finally {
091 currentThread.setContextClassLoader(contextClassLoader);
092 }
093 }
094
095
098 public static Object invoke(String className, String methodName)
099 throws Exception {
100
101 return invoke(className, methodName, new Object[] {});
102 }
103
104
107 public static Object invoke(String className, String methodName, Object arg)
108 throws Exception {
109
110 return invoke(className, methodName, new Object[] {arg});
111 }
112
113
116 public static Object invoke(
117 String className, String methodName, Object arg1, Object arg2)
118 throws Exception {
119
120 return invoke(className, methodName, new Object[] {arg1, arg2});
121 }
122
123
126 public static Object invoke(
127 String className, String methodName, Object arg1, Object arg2,
128 Object arg3)
129 throws Exception {
130
131 return invoke(className, methodName, new Object[] {arg1, arg2, arg3});
132 }
133
134
137 public static Object invoke(
138 String className, String methodName, Object[] args)
139 throws Exception {
140
141 return invoke(className, methodName, args, true);
142 }
143
144
147 public static Object invoke(
148 String className, String methodName, boolean newInstance)
149 throws Exception {
150
151 return invoke(className, methodName, new Object[] {}, newInstance);
152 }
153
154
157 public static Object invoke(
158 String className, String methodName, Object arg,
159 boolean newInstance)
160 throws Exception {
161
162 return invoke(className, methodName, new Object[] {arg}, newInstance);
163 }
164
165
168 public static Object invoke(
169 String className, String methodName, Object arg1, Object arg2,
170 boolean newInstance)
171 throws Exception {
172
173 return invoke(
174 className, methodName, new Object[] {arg1, arg2}, newInstance);
175 }
176
177
180 public static Object invoke(
181 String className, String methodName, Object arg1, Object arg2,
182 Object arg3, boolean newInstance)
183 throws Exception {
184
185 return invoke(
186 className, methodName, new Object[] {arg1, arg2, arg3},
187 newInstance);
188 }
189
190
193 public static Object invoke(
194 String className, String methodName, Object arg1, Object arg2,
195 Object arg3, Object arg4, boolean newInstance)
196 throws Exception {
197
198 return invoke(
199 className, methodName, new Object[] {arg1, arg2, arg3, arg4},
200 newInstance);
201 }
202
203
206 public static Object invoke(
207 String className, String methodName, Object arg1, Object arg2,
208 Object arg3, Object arg4, Object arg5, boolean newInstance)
209 throws Exception {
210
211 return invoke(
212 className, methodName, new Object[] {arg1, arg2, arg3, arg4, arg5},
213 newInstance);
214 }
215
216
219 public static Object invoke(
220 String className, String methodName, Object[] args,
221 boolean newInstance)
222 throws Exception {
223
224 Thread currentThread = Thread.currentThread();
225
226 ClassLoader contextClassLoader = currentThread.getContextClassLoader();
227
228 try {
229 currentThread.setContextClassLoader(
230 PortalClassLoaderUtil.getClassLoader());
231
232 MethodWrapper methodWrapper = new MethodWrapper(
233 className, methodName, args);
234
235 return MethodInvoker.invoke(methodWrapper, newInstance);
236 }
237 catch (InvocationTargetException ite) {
238 throw (Exception)ite.getCause();
239 }
240 finally {
241 currentThread.setContextClassLoader(contextClassLoader);
242 }
243 }
244
245 }