001
014
015 package com.liferay.portal.kernel.util;
016
017 import java.lang.reflect.InvocationTargetException;
018
019
023 public class PortalClassInvoker {
024
025 public static Object invoke(
026 boolean newInstance, MethodKey methodKey, Object... arguments)
027 throws Exception {
028
029 Thread currentThread = Thread.currentThread();
030
031 ClassLoader contextClassLoader = currentThread.getContextClassLoader();
032
033 try {
034 currentThread.setContextClassLoader(
035 PortalClassLoaderUtil.getClassLoader());
036
037 MethodHandler methodHandler = new MethodHandler(
038 methodKey, arguments);
039
040 return methodHandler.invoke(newInstance);
041 }
042 catch (InvocationTargetException ite) {
043 throw (Exception)ite.getCause();
044 }
045 finally {
046 currentThread.setContextClassLoader(contextClassLoader);
047 }
048 }
049
050 public static Object invoke(
051 boolean newInstance, String className, String methodName,
052 String[] parameterTypeNames, Object... arguments)
053 throws Exception {
054
055 Thread currentThread = Thread.currentThread();
056
057 ClassLoader contextClassLoader = currentThread.getContextClassLoader();
058
059 try {
060 currentThread.setContextClassLoader(
061 PortalClassLoaderUtil.getClassLoader());
062
063 MethodKey methodKey = new MethodKey(
064 className, methodName, parameterTypeNames);
065
066 MethodHandler methodHandler = new MethodHandler(
067 methodKey, arguments);
068
069 return methodHandler.invoke(newInstance);
070 }
071 catch (InvocationTargetException ite) {
072 throw (Exception)ite.getCause();
073 }
074 finally {
075 currentThread.setContextClassLoader(contextClassLoader);
076 }
077 }
078
079
082 public static Object invoke(String className, String methodName)
083 throws Exception {
084
085 return invoke(className, methodName, new Object[] {});
086 }
087
088
091 public static Object invoke(String className, String methodName, Object arg)
092 throws Exception {
093
094 return invoke(className, methodName, new Object[] {arg});
095 }
096
097
100 public static Object invoke(
101 String className, String methodName, Object arg1, Object arg2)
102 throws Exception {
103
104 return invoke(className, methodName, new Object[] {arg1, arg2});
105 }
106
107
110 public static Object invoke(
111 String className, String methodName, Object arg1, Object arg2,
112 Object arg3)
113 throws Exception {
114
115 return invoke(className, methodName, new Object[] {arg1, arg2, arg3});
116 }
117
118
121 public static Object invoke(
122 String className, String methodName, Object[] args)
123 throws Exception {
124
125 return invoke(className, methodName, args, true);
126 }
127
128
131 public static Object invoke(
132 String className, String methodName, boolean newInstance)
133 throws Exception {
134
135 return invoke(className, methodName, new Object[] {}, newInstance);
136 }
137
138
141 public static Object invoke(
142 String className, String methodName, Object arg,
143 boolean newInstance)
144 throws Exception {
145
146 return invoke(className, methodName, new Object[] {arg}, newInstance);
147 }
148
149
152 public static Object invoke(
153 String className, String methodName, Object arg1, Object arg2,
154 boolean newInstance)
155 throws Exception {
156
157 return invoke(
158 className, methodName, new Object[] {arg1, arg2}, newInstance);
159 }
160
161
164 public static Object invoke(
165 String className, String methodName, Object arg1, Object arg2,
166 Object arg3, boolean newInstance)
167 throws Exception {
168
169 return invoke(
170 className, methodName, new Object[] {arg1, arg2, arg3},
171 newInstance);
172 }
173
174
177 public static Object invoke(
178 String className, String methodName, Object arg1, Object arg2,
179 Object arg3, Object arg4, boolean newInstance)
180 throws Exception {
181
182 return invoke(
183 className, methodName, new Object[] {arg1, arg2, arg3, arg4},
184 newInstance);
185 }
186
187
190 public static Object invoke(
191 String className, String methodName, Object arg1, Object arg2,
192 Object arg3, Object arg4, Object arg5, boolean newInstance)
193 throws Exception {
194
195 return invoke(
196 className, methodName, new Object[] {arg1, arg2, arg3, arg4, arg5},
197 newInstance);
198 }
199
200
203 public static Object invoke(
204 String className, String methodName, Object[] args,
205 boolean newInstance)
206 throws Exception {
207
208 Thread currentThread = Thread.currentThread();
209
210 ClassLoader contextClassLoader = currentThread.getContextClassLoader();
211
212 try {
213 currentThread.setContextClassLoader(
214 PortalClassLoaderUtil.getClassLoader());
215
216 MethodWrapper methodWrapper = new MethodWrapper(
217 className, methodName, args);
218
219 return MethodInvoker.invoke(methodWrapper, newInstance);
220 }
221 catch (InvocationTargetException ite) {
222 throw (Exception)ite.getCause();
223 }
224 finally {
225 currentThread.setContextClassLoader(contextClassLoader);
226 }
227 }
228
229 }