1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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 }