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   */
24  public class PortalClassInvoker {
25  
26      public static Object invoke(String className, String methodName)
27          throws Exception {
28  
29          return invoke(className, methodName, new Object[] {});
30      }
31  
32      public static Object invoke(String className, String methodName, Object arg)
33          throws Exception {
34  
35          return invoke(className, methodName, new Object[] {arg});
36      }
37  
38      public static Object invoke(
39              String className, String methodName, Object arg1, Object arg2)
40          throws Exception {
41  
42          return invoke(className, methodName, new Object[] {arg1, arg2});
43      }
44  
45      public static Object invoke(
46              String className, String methodName, Object arg1, Object arg2,
47              Object arg3)
48          throws Exception {
49  
50          return invoke(className, methodName, new Object[] {arg1, arg2, arg3});
51      }
52  
53      public static Object invoke(
54              String className, String methodName, Object[] args)
55          throws Exception {
56  
57          return invoke(className, methodName, args, true);
58      }
59  
60      public static Object invoke(
61          String className, String methodName, boolean newInstance)
62          throws Exception {
63  
64          return invoke(className, methodName, new Object[] {}, newInstance);
65      }
66  
67      public static Object invoke(
68              String className, String methodName, Object arg,
69              boolean newInstance)
70          throws Exception {
71  
72          return invoke(className, methodName, new Object[] {arg}, newInstance);
73      }
74  
75      public static Object invoke(
76              String className, String methodName, Object arg1, Object arg2,
77              boolean newInstance)
78          throws Exception {
79  
80          return invoke(
81              className, methodName, new Object[] {arg1, arg2}, newInstance);
82      }
83  
84      public static Object invoke(
85              String className, String methodName, Object arg1, Object arg2,
86              Object arg3, boolean newInstance)
87          throws Exception {
88  
89          return invoke(
90              className, methodName, new Object[] {arg1, arg2, arg3},
91              newInstance);
92      }
93  
94      public static Object invoke(
95              String className, String methodName, Object arg1, Object arg2,
96              Object arg3, Object arg4, boolean newInstance)
97          throws Exception {
98  
99          return invoke(
100             className, methodName, new Object[] {arg1, arg2, arg3, arg4},
101             newInstance);
102     }
103 
104     public static Object invoke(
105             String className, String methodName, Object arg1, Object arg2,
106             Object arg3, Object arg4, Object arg5, boolean newInstance)
107         throws Exception {
108 
109         return invoke(
110             className, methodName, new Object[] {arg1, arg2, arg3, arg4, arg5},
111             newInstance);
112     }
113 
114     public static Object invoke(
115             String className, String methodName, Object[] args,
116             boolean newInstance)
117         throws Exception {
118 
119         Thread currentThread = Thread.currentThread();
120 
121         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
122 
123         try {
124             currentThread.setContextClassLoader(
125                 PortalClassLoaderUtil.getClassLoader());
126 
127             MethodWrapper methodWrapper = new MethodWrapper(
128                 className, methodName, args);
129 
130             return MethodInvoker.invoke(methodWrapper, newInstance);
131         }
132         catch (InvocationTargetException ite) {
133             throw (Exception)ite.getCause();
134         }
135         finally {
136             currentThread.setContextClassLoader(contextClassLoader);
137         }
138     }
139 
140 }