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  import com.liferay.portal.kernel.portlet.PortletBag;
26  import com.liferay.portal.kernel.portlet.PortletBagPool;
27  import com.liferay.portal.kernel.servlet.PortletServlet;
28  
29  /**
30   * <a href="PortletClassInvoker.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Bruno Farache
33   *
34   */
35  public class PortletClassInvoker {
36  
37      public static Object invoke(
38              String portletId, String className, String methodName)
39          throws Exception {
40  
41          return invoke(portletId, className, methodName, new Object[] {});
42      }
43  
44      public static Object invoke(
45              String portletId, String className, String methodName, Object arg)
46          throws Exception {
47  
48          return invoke(portletId, className, methodName, new Object[] {arg});
49      }
50  
51      public static Object invoke(
52              String portletId, String className, String methodName, Object arg1,
53              Object arg2)
54          throws Exception {
55  
56          return invoke(
57              portletId, className, methodName, new Object[] {arg1, arg2});
58      }
59  
60      public static Object invoke(
61              String portletId, String className, String methodName, Object arg1,
62              Object arg2, Object arg3)
63          throws Exception {
64  
65          return invoke(
66              portletId, className, methodName, new Object[] {arg1, arg2, arg3});
67      }
68  
69      public static Object invoke(
70              String portletId, String className, String methodName, Object arg1,
71              Object arg2, Object arg3, Object arg4)
72          throws Exception {
73  
74          return invoke(
75              portletId, className, methodName,
76              new Object[] {arg1, arg2, arg3, arg4});
77      }
78  
79      public static Object invoke(
80              String portletId, String className, String methodName,
81              Object[] args)
82          throws Exception {
83  
84          return invoke(portletId, className, methodName, args, true);
85      }
86  
87      public static Object invoke(
88          String portletId,String className, String methodName,
89          boolean newInstance)
90          throws Exception {
91  
92          return invoke(
93              portletId, className, methodName, new Object[] {}, newInstance);
94      }
95  
96      public static Object invoke(
97              String portletId, String className, String methodName, Object arg,
98              boolean newInstance)
99          throws Exception {
100 
101         return invoke(
102             portletId, className, methodName, new Object[] {arg}, newInstance);
103     }
104 
105     public static Object invoke(
106             String portletId, String className, String methodName, Object arg1,
107             Object arg2, boolean newInstance)
108         throws Exception {
109 
110         return invoke(
111             portletId, className, methodName, new Object[] {arg1, arg2},
112             newInstance);
113     }
114 
115     public static Object invoke(
116             String portletId, String className, String methodName, Object arg1,
117             Object arg2, Object arg3, boolean newInstance)
118         throws Exception {
119 
120         return invoke(
121             portletId, className, methodName, new Object[] {arg1, arg2, arg3},
122             newInstance);
123     }
124 
125     public static Object invoke(
126             String portletId, String className, String methodName, Object arg1,
127             Object arg2, Object arg3, Object arg4, boolean newInstance)
128         throws Exception {
129 
130         return invoke(
131             portletId, className, methodName,
132             new Object[] {arg1, arg2, arg3, arg4}, newInstance);
133     }
134 
135     public static Object invoke(
136             String portletId, String className, String methodName,
137             Object[] args, boolean newInstance)
138         throws Exception {
139 
140         ClassLoader portletClassLoader = PortalClassLoaderUtil.getClassLoader();
141 
142         PortletBag portletBag = PortletBagPool.get(portletId);
143 
144         if (portletBag != null) {
145             portletClassLoader =
146                 (ClassLoader)portletBag.getServletContext().getAttribute(
147                     PortletServlet.PORTLET_CLASS_LOADER);
148         }
149 
150         ClassLoader contextClassLoader =
151             Thread.currentThread().getContextClassLoader();
152 
153         try {
154             Thread.currentThread().setContextClassLoader(portletClassLoader);
155 
156             MethodWrapper methodWrapper = new MethodWrapper(
157                 className, methodName, args);
158 
159             return MethodInvoker.invoke(methodWrapper, newInstance);
160         }
161         finally {
162             Thread.currentThread().setContextClassLoader(contextClassLoader);
163         }
164     }
165 
166 }