001    /**
002     * Copyright (c) 2000-present 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    
020    import javax.servlet.ServletContext;
021    
022    /**
023     * @author Bruno Farache
024     * @author Shuyang Zhou
025     */
026    public class PortletClassInvoker {
027    
028            /**
029             * @deprecated As of 7.0.0, replaced by {@link #invoke(String, MethodKey,
030             *             Object...)}
031             */
032            @Deprecated
033            public static Object invoke(
034                            boolean newInstance, String portletId, MethodKey methodKey,
035                            Object... arguments)
036                    throws Exception {
037    
038                    return invoke(portletId, methodKey, arguments);
039            }
040    
041            public static Object invoke(
042                            String portletId, MethodKey methodKey, Object... arguments)
043                    throws Exception {
044    
045                    portletId = _getRootPortletId(portletId);
046    
047                    ClassLoader portletClassLoader = PortalClassLoaderUtil.getClassLoader();
048    
049                    PortletBag portletBag = PortletBagPool.get(portletId);
050    
051                    if (portletBag != null) {
052                            ServletContext servletContext = portletBag.getServletContext();
053    
054                            portletClassLoader = servletContext.getClassLoader();
055                    }
056    
057                    Thread currentThread = Thread.currentThread();
058    
059                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
060    
061                    try {
062                            currentThread.setContextClassLoader(portletClassLoader);
063    
064                            MethodHandler methodHandler = new MethodHandler(
065                                    methodKey, arguments);
066    
067                            return methodHandler.invoke();
068                    }
069                    finally {
070                            currentThread.setContextClassLoader(contextClassLoader);
071                    }
072            }
073    
074            /**
075             * Copied from <code>com.liferay.portal.model.PortletConstants</code>.
076             */
077            private static String _getRootPortletId(String portletId) {
078                    int pos = portletId.indexOf(_INSTANCE_SEPARATOR);
079    
080                    if (pos == -1) {
081                            return portletId;
082                    }
083                    else {
084                            return portletId.substring(0, pos);
085                    }
086            }
087    
088            private static final String _INSTANCE_SEPARATOR = "_INSTANCE_";
089    
090    }