1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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  /**
26   * <a href="PortalClassInvoker.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   *
30   */
31  public class PortalClassInvoker {
32  
33      public static Object invoke(String className, String methodName)
34          throws Exception {
35  
36          return invoke(className, methodName, new Object[] {});
37      }
38  
39      public static Object invoke(String className, String methodName, Object arg)
40          throws Exception {
41  
42          return invoke(className, methodName, new Object[] {arg});
43      }
44  
45      public static Object invoke(
46              String className, String methodName, Object arg1, Object arg2)
47          throws Exception {
48  
49          return invoke(className, methodName, new Object[] {arg1, arg2});
50      }
51  
52      public static Object invoke(
53              String className, String methodName, Object arg1, Object arg2,
54              Object arg3)
55          throws Exception {
56  
57          return invoke(className, methodName, new Object[] {arg1, arg2, arg3});
58      }
59  
60      public static Object invoke(
61              String className, String methodName, Object[] args)
62          throws Exception {
63  
64          return invoke(className, methodName, args, true);
65      }
66  
67      public static Object invoke(
68          String className, String methodName, boolean newInstance)
69          throws Exception {
70  
71          return invoke(className, methodName, new Object[] {}, newInstance);
72      }
73  
74      public static Object invoke(
75              String className, String methodName, Object arg,
76              boolean newInstance)
77          throws Exception {
78  
79          return invoke(className, methodName, new Object[] {arg}, newInstance);
80      }
81  
82      public static Object invoke(
83              String className, String methodName, Object arg1, Object arg2,
84              boolean newInstance)
85          throws Exception {
86  
87          return invoke(
88              className, methodName, new Object[] {arg1, arg2}, newInstance);
89      }
90  
91      public static Object invoke(
92              String className, String methodName, Object arg1, Object arg2,
93              Object arg3, boolean newInstance)
94          throws Exception {
95  
96          return invoke(
97              className, methodName, new Object[] {arg1, arg2, arg3},
98              newInstance);
99      }
100 
101     public static Object invoke(
102             String className, String methodName, Object[] args,
103             boolean newInstance)
104         throws Exception {
105 
106         ClassLoader contextClassLoader =
107             Thread.currentThread().getContextClassLoader();
108 
109         try {
110             Thread.currentThread().setContextClassLoader(
111                 PortalClassLoaderUtil.getClassLoader());
112 
113             MethodWrapper methodWrapper = new MethodWrapper(
114                 className, methodName, args);
115 
116             return MethodInvoker.invoke(methodWrapper, newInstance);
117         }
118         finally {
119             Thread.currentThread().setContextClassLoader(contextClassLoader);
120         }
121     }
122 
123 }