001    /**
002     * Copyright (c) 2000-2013 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.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            public static Object invoke(
028                            boolean newInstance, MethodKey methodKey, Object... arguments)
029                    throws Exception {
030    
031                    Thread currentThread = Thread.currentThread();
032    
033                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
034    
035                    try {
036                            currentThread.setContextClassLoader(
037                                    PortalClassLoaderUtil.getClassLoader());
038    
039                            MethodHandler methodHandler = new MethodHandler(
040                                    methodKey, arguments);
041    
042                            return methodHandler.invoke(newInstance);
043                    }
044                    catch (InvocationTargetException ite) {
045                            Throwable cause = ite.getCause();
046    
047                            if (cause instanceof Error) {
048                                    throw new SystemException(ite);
049                            }
050                            else {
051                                    throw (Exception)cause;
052                            }
053                    }
054                    finally {
055                            currentThread.setContextClassLoader(contextClassLoader);
056                    }
057            }
058    
059    }