1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import java.lang.reflect.InvocationTargetException;
26  
27  /**
28   * <a href="PortalClassInvoker.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class PortalClassInvoker {
33  
34      public static Object invoke(String className, String methodName)
35          throws Exception {
36  
37          return invoke(className, methodName, new Object[] {});
38      }
39  
40      public static Object invoke(String className, String methodName, Object arg)
41          throws Exception {
42  
43          return invoke(className, methodName, new Object[] {arg});
44      }
45  
46      public static Object invoke(
47              String className, String methodName, Object arg1, Object arg2)
48          throws Exception {
49  
50          return invoke(className, methodName, new Object[] {arg1, arg2});
51      }
52  
53      public static Object invoke(
54              String className, String methodName, Object arg1, Object arg2,
55              Object arg3)
56          throws Exception {
57  
58          return invoke(className, methodName, new Object[] {arg1, arg2, arg3});
59      }
60  
61      public static Object invoke(
62              String className, String methodName, Object[] args)
63          throws Exception {
64  
65          return invoke(className, methodName, args, true);
66      }
67  
68      public static Object invoke(
69          String className, String methodName, boolean newInstance)
70          throws Exception {
71  
72          return invoke(className, methodName, new Object[] {}, newInstance);
73      }
74  
75      public static Object invoke(
76              String className, String methodName, Object arg,
77              boolean newInstance)
78          throws Exception {
79  
80          return invoke(className, methodName, new Object[] {arg}, newInstance);
81      }
82  
83      public static Object invoke(
84              String className, String methodName, Object arg1, Object arg2,
85              boolean newInstance)
86          throws Exception {
87  
88          return invoke(
89              className, methodName, new Object[] {arg1, arg2}, newInstance);
90      }
91  
92      public static Object invoke(
93              String className, String methodName, Object arg1, Object arg2,
94              Object arg3, boolean newInstance)
95          throws Exception {
96  
97          return invoke(
98              className, methodName, new Object[] {arg1, arg2, arg3},
99              newInstance);
100     }
101 
102     public static Object invoke(
103             String className, String methodName, Object[] args,
104             boolean newInstance)
105         throws Exception {
106 
107         Thread currentThread = Thread.currentThread();
108 
109         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
110 
111         try {
112             currentThread.setContextClassLoader(
113                 PortalClassLoaderUtil.getClassLoader());
114 
115             MethodWrapper methodWrapper = new MethodWrapper(
116                 className, methodName, args);
117 
118             return MethodInvoker.invoke(methodWrapper, newInstance);
119         }
120         catch (InvocationTargetException ite) {
121             throw (Exception)ite.getCause();
122         }
123         finally {
124             currentThread.setContextClassLoader(contextClassLoader);
125         }
126     }
127 
128 }