1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.util;
21  
22  import com.liferay.portal.kernel.portlet.PortletBag;
23  import com.liferay.portal.kernel.portlet.PortletBagPool;
24  import com.liferay.portal.kernel.servlet.PortletServlet;
25  
26  import javax.servlet.ServletContext;
27  
28  /**
29   * <a href="PortletClassInvoker.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Bruno Farache
32   *
33   */
34  public class PortletClassInvoker {
35  
36      public static Object invoke(
37              String portletId, String className, String methodName)
38          throws Exception {
39  
40          return invoke(portletId, className, methodName, new Object[] {});
41      }
42  
43      public static Object invoke(
44              String portletId, String className, String methodName, Object arg)
45          throws Exception {
46  
47          return invoke(portletId, className, methodName, new Object[] {arg});
48      }
49  
50      public static Object invoke(
51              String portletId, String className, String methodName, Object arg1,
52              Object arg2)
53          throws Exception {
54  
55          return invoke(
56              portletId, className, methodName, new Object[] {arg1, arg2});
57      }
58  
59      public static Object invoke(
60              String portletId, String className, String methodName, Object arg1,
61              Object arg2, Object arg3)
62          throws Exception {
63  
64          return invoke(
65              portletId, className, methodName, new Object[] {arg1, arg2, arg3});
66      }
67  
68      public static Object invoke(
69              String portletId, String className, String methodName, Object arg1,
70              Object arg2, Object arg3, Object arg4)
71          throws Exception {
72  
73          return invoke(
74              portletId, className, methodName,
75              new Object[] {arg1, arg2, arg3, arg4});
76      }
77  
78      public static Object invoke(
79              String portletId, String className, String methodName,
80              Object[] args)
81          throws Exception {
82  
83          return invoke(portletId, className, methodName, args, true);
84      }
85  
86      public static Object invoke(
87          String portletId,String className, String methodName,
88          boolean newInstance)
89          throws Exception {
90  
91          return invoke(
92              portletId, className, methodName, new Object[] {}, newInstance);
93      }
94  
95      public static Object invoke(
96              String portletId, String className, String methodName, Object arg,
97              boolean newInstance)
98          throws Exception {
99  
100         return invoke(
101             portletId, className, methodName, new Object[] {arg}, newInstance);
102     }
103 
104     public static Object invoke(
105             String portletId, String className, String methodName, Object arg1,
106             Object arg2, boolean newInstance)
107         throws Exception {
108 
109         return invoke(
110             portletId, className, methodName, new Object[] {arg1, arg2},
111             newInstance);
112     }
113 
114     public static Object invoke(
115             String portletId, String className, String methodName, Object arg1,
116             Object arg2, Object arg3, boolean newInstance)
117         throws Exception {
118 
119         return invoke(
120             portletId, className, methodName, new Object[] {arg1, arg2, arg3},
121             newInstance);
122     }
123 
124     public static Object invoke(
125             String portletId, String className, String methodName, Object arg1,
126             Object arg2, Object arg3, Object arg4, boolean newInstance)
127         throws Exception {
128 
129         return invoke(
130             portletId, className, methodName,
131             new Object[] {arg1, arg2, arg3, arg4}, newInstance);
132     }
133 
134     public static Object invoke(
135             String portletId, String className, String methodName,
136             Object[] args, boolean newInstance)
137         throws Exception {
138 
139         portletId = _getRootPortletId(portletId);
140 
141         ClassLoader portletClassLoader = PortalClassLoaderUtil.getClassLoader();
142 
143         PortletBag portletBag = PortletBagPool.get(portletId);
144 
145         if (portletBag != null) {
146             ServletContext servletContext = portletBag.getServletContext();
147 
148             portletClassLoader = (ClassLoader)servletContext.getAttribute(
149                 PortletServlet.PORTLET_CLASS_LOADER);
150         }
151 
152         Thread currentThread = Thread.currentThread();
153 
154         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
155 
156         try {
157             currentThread.setContextClassLoader(portletClassLoader);
158 
159             MethodWrapper methodWrapper = new MethodWrapper(
160                 className, methodName, args);
161 
162             return MethodInvoker.invoke(methodWrapper, newInstance);
163         }
164         finally {
165             currentThread.setContextClassLoader(contextClassLoader);
166         }
167     }
168 
169     /**
170      * Copied from <code>com.liferay.portal.model.PortletConstants</code>.
171      */
172     private static String _getRootPortletId(String portletId) {
173         int pos = portletId.indexOf(_INSTANCE_SEPARATOR);
174 
175         if (pos == -1) {
176             return portletId;
177         }
178         else {
179             return portletId.substring(0, pos);
180         }
181     }
182 
183     private static final String _INSTANCE_SEPARATOR = "_INSTANCE_";
184 
185 }