001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.portlet.PortletBag;
018    import com.liferay.portal.kernel.portlet.PortletBagPool;
019    import com.liferay.portal.kernel.servlet.PortletServlet;
020    
021    import javax.servlet.ServletContext;
022    
023    /**
024     * @author Bruno Farache
025     * @author Shuyang Zhou
026     */
027    public class PortletClassInvoker {
028    
029            public static Object invoke(
030                            boolean newInstance, String portletId, MethodKey methodKey,
031                            Object... arguments)
032                    throws Exception {
033    
034                    portletId = _getRootPortletId(portletId);
035    
036                    ClassLoader portletClassLoader = PortalClassLoaderUtil.getClassLoader();
037    
038                    PortletBag portletBag = PortletBagPool.get(portletId);
039    
040                    if (portletBag != null) {
041                            ServletContext servletContext = portletBag.getServletContext();
042    
043                            portletClassLoader = (ClassLoader)servletContext.getAttribute(
044                                    PortletServlet.PORTLET_CLASS_LOADER);
045                    }
046    
047                    Thread currentThread = Thread.currentThread();
048    
049                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
050    
051                    try {
052                            currentThread.setContextClassLoader(portletClassLoader);
053    
054                            MethodHandler methodHandler = new MethodHandler(
055                                    methodKey, arguments);
056    
057                            return methodHandler.invoke(newInstance);
058                    }
059                    finally {
060                            currentThread.setContextClassLoader(contextClassLoader);
061                    }
062            }
063    
064            /**
065             * Copied from <code>com.liferay.portal.model.PortletConstants</code>.
066             */
067            private static String _getRootPortletId(String portletId) {
068                    int pos = portletId.indexOf(_INSTANCE_SEPARATOR);
069    
070                    if (pos == -1) {
071                            return portletId;
072                    }
073                    else {
074                            return portletId.substring(0, pos);
075                    }
076            }
077    
078            private static final String _INSTANCE_SEPARATOR = "_INSTANCE_";
079    
080    }