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.exception.SystemException;
018    
019    import java.lang.reflect.InvocationTargetException;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     * @author Shuyang Zhou
024     */
025    public class PortalClassInvoker {
026    
027            /**
028             * @deprecated As of 7.0.0, replaced by {@link #invoke(MethodKey,
029             *             Object...)}
030             */
031            @Deprecated
032            public static Object invoke(
033                            boolean newInstance, MethodKey methodKey, Object... arguments)
034                    throws Exception {
035    
036                    return invoke(methodKey, arguments);
037            }
038    
039            public static Object invoke(MethodKey methodKey, Object... arguments)
040                    throws Exception {
041    
042                    Thread currentThread = Thread.currentThread();
043    
044                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
045    
046                    try {
047                            currentThread.setContextClassLoader(
048                                    PortalClassLoaderUtil.getClassLoader());
049    
050                            MethodHandler methodHandler = new MethodHandler(
051                                    methodKey, arguments);
052    
053                            return methodHandler.invoke();
054                    }
055                    catch (InvocationTargetException ite) {
056                            Throwable cause = ite.getCause();
057    
058                            if (cause instanceof Error) {
059                                    throw new SystemException(ite);
060                            }
061                            else {
062                                    throw (Exception)cause;
063                            }
064                    }
065                    finally {
066                            currentThread.setContextClassLoader(contextClassLoader);
067                    }
068            }
069    
070    }