1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PortletClassInvoker {
37  
38      public static Object invoke(
39              String portletId, String className, String methodName)
40          throws Exception {
41  
42          return invoke(portletId, className, methodName, new Object[] {});
43      }
44  
45      public static Object invoke(
46              String portletId, String className, String methodName, Object arg)
47          throws Exception {
48  
49          return invoke(portletId, className, methodName, new Object[] {arg});
50      }
51  
52      public static Object invoke(
53              String portletId, String className, String methodName, Object arg1,
54              Object arg2)
55          throws Exception {
56  
57          return invoke(
58              portletId, className, methodName, new Object[] {arg1, arg2});
59      }
60  
61      public static Object invoke(
62              String portletId, String className, String methodName, Object arg1,
63              Object arg2, Object arg3)
64          throws Exception {
65  
66          return invoke(
67              portletId, className, methodName, new Object[] {arg1, arg2, arg3});
68      }
69  
70      public static Object invoke(
71              String portletId, String className, String methodName, Object arg1,
72              Object arg2, Object arg3, Object arg4)
73          throws Exception {
74  
75          return invoke(
76              portletId, className, methodName,
77              new Object[] {arg1, arg2, arg3, arg4});
78      }
79  
80      public static Object invoke(
81              String portletId, String className, String methodName,
82              Object[] args)
83          throws Exception {
84  
85          return invoke(portletId, className, methodName, args, true);
86      }
87  
88      public static Object invoke(
89          String portletId,String className, String methodName,
90          boolean newInstance)
91          throws Exception {
92  
93          return invoke(
94              portletId, className, methodName, new Object[] {}, newInstance);
95      }
96  
97      public static Object invoke(
98              String portletId, String className, String methodName, Object arg,
99              boolean newInstance)
100         throws Exception {
101 
102         return invoke(
103             portletId, className, methodName, new Object[] {arg}, newInstance);
104     }
105 
106     public static Object invoke(
107             String portletId, String className, String methodName, Object arg1,
108             Object arg2, boolean newInstance)
109         throws Exception {
110 
111         return invoke(
112             portletId, className, methodName, new Object[] {arg1, arg2},
113             newInstance);
114     }
115 
116     public static Object invoke(
117             String portletId, String className, String methodName, Object arg1,
118             Object arg2, Object arg3, boolean newInstance)
119         throws Exception {
120 
121         return invoke(
122             portletId, className, methodName, new Object[] {arg1, arg2, arg3},
123             newInstance);
124     }
125 
126     public static Object invoke(
127             String portletId, String className, String methodName, Object arg1,
128             Object arg2, Object arg3, Object arg4, boolean newInstance)
129         throws Exception {
130 
131         return invoke(
132             portletId, className, methodName,
133             new Object[] {arg1, arg2, arg3, arg4}, newInstance);
134     }
135 
136     public static Object invoke(
137             String portletId, String className, String methodName,
138             Object[] args, boolean newInstance)
139         throws Exception {
140 
141         portletId = _getRootPortletId(portletId);
142 
143         ClassLoader portletClassLoader = PortalClassLoaderUtil.getClassLoader();
144 
145         PortletBag portletBag = PortletBagPool.get(portletId);
146 
147         if (portletBag != null) {
148             ServletContext servletContext = portletBag.getServletContext();
149 
150             portletClassLoader = (ClassLoader)servletContext.getAttribute(
151                 PortletServlet.PORTLET_CLASS_LOADER);
152         }
153 
154         Thread currentThread = Thread.currentThread();
155 
156         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
157 
158         try {
159             currentThread.setContextClassLoader(portletClassLoader);
160 
161             MethodWrapper methodWrapper = new MethodWrapper(
162                 className, methodName, args);
163 
164             return MethodInvoker.invoke(methodWrapper, newInstance);
165         }
166         finally {
167             currentThread.setContextClassLoader(contextClassLoader);
168         }
169     }
170 
171     /**
172      * Copied from <code>com.liferay.portal.model.PortletConstants</code>.
173      */
174     private static String _getRootPortletId(String portletId) {
175         int pos = portletId.indexOf(_INSTANCE_SEPARATOR);
176 
177         if (pos == -1) {
178             return portletId;
179         }
180         else {
181             return portletId.substring(0, pos);
182         }
183     }
184 
185     private static final String _INSTANCE_SEPARATOR = "_INSTANCE_";
186 
187 }