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