001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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.PluginContextListener;
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                                    PluginContextListener.PLUGIN_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    }