1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.lang.reflect.InvocationTargetException;
18  
19  /**
20   * <a href="PortalClassInvoker.java.html"><b><i>View Source</i></b></a>
21   *
22   * @author Brian Wing Shun Chan
23   * @author Shuyang Zhou
24   */
25  public class PortalClassInvoker {
26  
27      public static Object invoke(
28              boolean newInstance, MethodKey methodKey, Object... arguments)
29          throws Exception {
30  
31          Thread currentThread = Thread.currentThread();
32  
33          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
34  
35          try {
36              currentThread.setContextClassLoader(
37                  PortalClassLoaderUtil.getClassLoader());
38  
39              MethodHandler methodHandler = new MethodHandler(
40                  methodKey, arguments);
41  
42              return methodHandler.invoke(newInstance);
43          }
44          catch (InvocationTargetException ite) {
45              throw (Exception)ite.getCause();
46          }
47          finally {
48              currentThread.setContextClassLoader(contextClassLoader);
49          }
50      }
51  
52      public static Object invoke(
53              boolean newInstance, String className, String methodName,
54              String[] parameterTypeNames, Object... arguments)
55          throws Exception {
56  
57          Thread currentThread = Thread.currentThread();
58  
59          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
60  
61          try {
62              currentThread.setContextClassLoader(
63                  PortalClassLoaderUtil.getClassLoader());
64  
65              MethodKey methodKey = new MethodKey(
66                  className, methodName, parameterTypeNames);
67  
68              MethodHandler methodHandler = new MethodHandler(
69                  methodKey, arguments);
70  
71              return methodHandler.invoke(newInstance);
72          }
73          catch (InvocationTargetException ite) {
74              throw (Exception)ite.getCause();
75          }
76          finally {
77              currentThread.setContextClassLoader(contextClassLoader);
78          }
79      }
80  
81      /**
82       * @deprecated
83       */
84      public static Object invoke(String className, String methodName)
85          throws Exception {
86  
87          return invoke(className, methodName, new Object[] {});
88      }
89  
90      /**
91       * @deprecated
92       */
93      public static Object invoke(String className, String methodName, Object arg)
94          throws Exception {
95  
96          return invoke(className, methodName, new Object[] {arg});
97      }
98  
99      /**
100      * @deprecated
101      */
102     public static Object invoke(
103             String className, String methodName, Object arg1, Object arg2)
104         throws Exception {
105 
106         return invoke(className, methodName, new Object[] {arg1, arg2});
107     }
108 
109     /**
110      * @deprecated
111      */
112     public static Object invoke(
113             String className, String methodName, Object arg1, Object arg2,
114             Object arg3)
115         throws Exception {
116 
117         return invoke(className, methodName, new Object[] {arg1, arg2, arg3});
118     }
119 
120     /**
121      * @deprecated
122      */
123     public static Object invoke(
124             String className, String methodName, Object[] args)
125         throws Exception {
126 
127         return invoke(className, methodName, args, true);
128     }
129 
130     /**
131      * @deprecated
132      */
133     public static Object invoke(
134         String className, String methodName, boolean newInstance)
135         throws Exception {
136 
137         return invoke(className, methodName, new Object[] {}, newInstance);
138     }
139 
140     /**
141      * @deprecated
142      */
143     public static Object invoke(
144             String className, String methodName, Object arg,
145             boolean newInstance)
146         throws Exception {
147 
148         return invoke(className, methodName, new Object[] {arg}, newInstance);
149     }
150 
151     /**
152      * @deprecated
153      */
154     public static Object invoke(
155             String className, String methodName, Object arg1, Object arg2,
156             boolean newInstance)
157         throws Exception {
158 
159         return invoke(
160             className, methodName, new Object[] {arg1, arg2}, newInstance);
161     }
162 
163     /**
164      * @deprecated
165      */
166     public static Object invoke(
167             String className, String methodName, Object arg1, Object arg2,
168             Object arg3, boolean newInstance)
169         throws Exception {
170 
171         return invoke(
172             className, methodName, new Object[] {arg1, arg2, arg3},
173             newInstance);
174     }
175 
176     /**
177      * @deprecated
178      */
179     public static Object invoke(
180             String className, String methodName, Object arg1, Object arg2,
181             Object arg3, Object arg4, boolean newInstance)
182         throws Exception {
183 
184         return invoke(
185             className, methodName, new Object[] {arg1, arg2, arg3, arg4},
186             newInstance);
187     }
188 
189     /**
190      * @deprecated
191      */
192     public static Object invoke(
193             String className, String methodName, Object arg1, Object arg2,
194             Object arg3, Object arg4, Object arg5, boolean newInstance)
195         throws Exception {
196 
197         return invoke(
198             className, methodName, new Object[] {arg1, arg2, arg3, arg4, arg5},
199             newInstance);
200     }
201 
202     /**
203      * @deprecated
204      */
205     public static Object invoke(
206             String className, String methodName, Object[] args,
207             boolean newInstance)
208         throws Exception {
209 
210         Thread currentThread = Thread.currentThread();
211 
212         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
213 
214         try {
215             currentThread.setContextClassLoader(
216                 PortalClassLoaderUtil.getClassLoader());
217 
218             MethodWrapper methodWrapper = new MethodWrapper(
219                 className, methodName, args);
220 
221             return MethodInvoker.invoke(methodWrapper, newInstance);
222         }
223         catch (InvocationTargetException ite) {
224             throw (Exception)ite.getCause();
225         }
226         finally {
227             currentThread.setContextClassLoader(contextClassLoader);
228         }
229     }
230 
231 }