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