001    /**
002     * Copyright (c) 2000-2010 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 java.lang.reflect.InvocationTargetException;
018    
019    /**
020     * @author Brian Wing Shun Chan
021     */
022    public class PortalClassInvoker {
023    
024            public static Object invoke(String className, String methodName)
025                    throws Exception {
026    
027                    return invoke(className, methodName, new Object[] {});
028            }
029    
030            public static Object invoke(String className, String methodName, Object arg)
031                    throws Exception {
032    
033                    return invoke(className, methodName, new Object[] {arg});
034            }
035    
036            public static Object invoke(
037                            String className, String methodName, Object arg1, Object arg2)
038                    throws Exception {
039    
040                    return invoke(className, methodName, new Object[] {arg1, arg2});
041            }
042    
043            public static Object invoke(
044                            String className, String methodName, Object arg1, Object arg2,
045                            Object arg3)
046                    throws Exception {
047    
048                    return invoke(className, methodName, new Object[] {arg1, arg2, arg3});
049            }
050    
051            public static Object invoke(
052                            String className, String methodName, Object[] args)
053                    throws Exception {
054    
055                    return invoke(className, methodName, args, true);
056            }
057    
058            public static Object invoke(
059                    String className, String methodName, boolean newInstance)
060                    throws Exception {
061    
062                    return invoke(className, methodName, new Object[] {}, newInstance);
063            }
064    
065            public static Object invoke(
066                            String className, String methodName, Object arg,
067                            boolean newInstance)
068                    throws Exception {
069    
070                    return invoke(className, methodName, new Object[] {arg}, newInstance);
071            }
072    
073            public static Object invoke(
074                            String className, String methodName, Object arg1, Object arg2,
075                            boolean newInstance)
076                    throws Exception {
077    
078                    return invoke(
079                            className, methodName, new Object[] {arg1, arg2}, newInstance);
080            }
081    
082            public static Object invoke(
083                            String className, String methodName, Object arg1, Object arg2,
084                            Object arg3, boolean newInstance)
085                    throws Exception {
086    
087                    return invoke(
088                            className, methodName, new Object[] {arg1, arg2, arg3},
089                            newInstance);
090            }
091    
092            public static Object invoke(
093                            String className, String methodName, Object arg1, Object arg2,
094                            Object arg3, Object arg4, boolean newInstance)
095                    throws Exception {
096    
097                    return invoke(
098                            className, methodName, new Object[] {arg1, arg2, arg3, arg4},
099                            newInstance);
100            }
101    
102            public static Object invoke(
103                            String className, String methodName, Object arg1, Object arg2,
104                            Object arg3, Object arg4, Object arg5, boolean newInstance)
105                    throws Exception {
106    
107                    return invoke(
108                            className, methodName, new Object[] {arg1, arg2, arg3, arg4, arg5},
109                            newInstance);
110            }
111    
112            public static Object invoke(
113                            String className, String methodName, Object[] args,
114                            boolean newInstance)
115                    throws Exception {
116    
117                    Thread currentThread = Thread.currentThread();
118    
119                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
120    
121                    try {
122                            currentThread.setContextClassLoader(
123                                    PortalClassLoaderUtil.getClassLoader());
124    
125                            MethodWrapper methodWrapper = new MethodWrapper(
126                                    className, methodName, args);
127    
128                            return MethodInvoker.invoke(methodWrapper, newInstance);
129                    }
130                    catch (InvocationTargetException ite) {
131                            throw (Exception)ite.getCause();
132                    }
133                    finally {
134                            currentThread.setContextClassLoader(contextClassLoader);
135                    }
136            }
137    
138    }