1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.util;
21  
22  /**
23   * <a href="PortalClassInvoker.java.html"><b><i>View Source</i></b></a>
24   *
25   * @author Brian Wing Shun Chan
26   *
27   */
28  public class PortalClassInvoker {
29  
30      public static Object invoke(String className, String methodName)
31          throws Exception {
32  
33          return invoke(className, methodName, new Object[] {});
34      }
35  
36      public static Object invoke(String className, String methodName, Object arg)
37          throws Exception {
38  
39          return invoke(className, methodName, new Object[] {arg});
40      }
41  
42      public static Object invoke(
43              String className, String methodName, Object arg1, Object arg2)
44          throws Exception {
45  
46          return invoke(className, methodName, new Object[] {arg1, arg2});
47      }
48  
49      public static Object invoke(
50              String className, String methodName, Object arg1, Object arg2,
51              Object arg3)
52          throws Exception {
53  
54          return invoke(className, methodName, new Object[] {arg1, arg2, arg3});
55      }
56  
57      public static Object invoke(
58              String className, String methodName, Object[] args)
59          throws Exception {
60  
61          return invoke(className, methodName, args, true);
62      }
63  
64      public static Object invoke(
65          String className, String methodName, boolean newInstance)
66          throws Exception {
67  
68          return invoke(className, methodName, new Object[] {}, newInstance);
69      }
70  
71      public static Object invoke(
72              String className, String methodName, Object arg,
73              boolean newInstance)
74          throws Exception {
75  
76          return invoke(className, methodName, new Object[] {arg}, newInstance);
77      }
78  
79      public static Object invoke(
80              String className, String methodName, Object arg1, Object arg2,
81              boolean newInstance)
82          throws Exception {
83  
84          return invoke(
85              className, methodName, new Object[] {arg1, arg2}, newInstance);
86      }
87  
88      public static Object invoke(
89              String className, String methodName, Object arg1, Object arg2,
90              Object arg3, boolean newInstance)
91          throws Exception {
92  
93          return invoke(
94              className, methodName, new Object[] {arg1, arg2, arg3},
95              newInstance);
96      }
97  
98      public static Object invoke(
99              String className, String methodName, Object[] args,
100             boolean newInstance)
101         throws Exception {
102 
103         Thread currentThread = Thread.currentThread();
104 
105         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
106 
107         try {
108             currentThread.setContextClassLoader(
109                 PortalClassLoaderUtil.getClassLoader());
110 
111             MethodWrapper methodWrapper = new MethodWrapper(
112                 className, methodName, args);
113 
114             return MethodInvoker.invoke(methodWrapper, newInstance);
115         }
116         finally {
117             currentThread.setContextClassLoader(contextClassLoader);
118         }
119     }
120 
121 }