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.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    }