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     * @author Shuyang Zhou
022     */
023    public class PortalClassInvoker {
024    
025            public static Object invoke(
026                            boolean newInstance, MethodKey methodKey, Object... arguments)
027                    throws Exception {
028    
029                    Thread currentThread = Thread.currentThread();
030    
031                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
032    
033                    try {
034                            currentThread.setContextClassLoader(
035                                    PortalClassLoaderUtil.getClassLoader());
036    
037                            MethodHandler methodHandler = new MethodHandler(
038                                    methodKey, arguments);
039    
040                            return methodHandler.invoke(newInstance);
041                    }
042                    catch (InvocationTargetException ite) {
043                            throw (Exception)ite.getCause();
044                    }
045                    finally {
046                            currentThread.setContextClassLoader(contextClassLoader);
047                    }
048            }
049    
050            public static Object invoke(
051                            boolean newInstance, String className, String methodName,
052                            String[] parameterTypeNames, Object... arguments)
053                    throws Exception {
054    
055                    Thread currentThread = Thread.currentThread();
056    
057                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
058    
059                    try {
060                            currentThread.setContextClassLoader(
061                                    PortalClassLoaderUtil.getClassLoader());
062    
063                            MethodKey methodKey = new MethodKey(
064                                    className, methodName, parameterTypeNames);
065    
066                            MethodHandler methodHandler = new MethodHandler(
067                                    methodKey, arguments);
068    
069                            return methodHandler.invoke(newInstance);
070                    }
071                    catch (InvocationTargetException ite) {
072                            throw (Exception)ite.getCause();
073                    }
074                    finally {
075                            currentThread.setContextClassLoader(contextClassLoader);
076                    }
077            }
078    
079            /**
080             * @deprecated
081             */
082            public static Object invoke(String className, String methodName)
083                    throws Exception {
084    
085                    return invoke(className, methodName, new Object[] {});
086            }
087    
088            /**
089             * @deprecated
090             */
091            public static Object invoke(String className, String methodName, Object arg)
092                    throws Exception {
093    
094                    return invoke(className, methodName, new Object[] {arg});
095            }
096    
097            /**
098             * @deprecated
099             */
100            public static Object invoke(
101                            String className, String methodName, Object arg1, Object arg2)
102                    throws Exception {
103    
104                    return invoke(className, methodName, new Object[] {arg1, arg2});
105            }
106    
107            /**
108             * @deprecated
109             */
110            public static Object invoke(
111                            String className, String methodName, Object arg1, Object arg2,
112                            Object arg3)
113                    throws Exception {
114    
115                    return invoke(className, methodName, new Object[] {arg1, arg2, arg3});
116            }
117    
118            /**
119             * @deprecated
120             */
121            public static Object invoke(
122                            String className, String methodName, Object[] args)
123                    throws Exception {
124    
125                    return invoke(className, methodName, args, true);
126            }
127    
128            /**
129             * @deprecated
130             */
131            public static Object invoke(
132                    String className, String methodName, boolean newInstance)
133                    throws Exception {
134    
135                    return invoke(className, methodName, new Object[] {}, newInstance);
136            }
137    
138            /**
139             * @deprecated
140             */
141            public static Object invoke(
142                            String className, String methodName, Object arg,
143                            boolean newInstance)
144                    throws Exception {
145    
146                    return invoke(className, methodName, new Object[] {arg}, newInstance);
147            }
148    
149            /**
150             * @deprecated
151             */
152            public static Object invoke(
153                            String className, String methodName, Object arg1, Object arg2,
154                            boolean newInstance)
155                    throws Exception {
156    
157                    return invoke(
158                            className, methodName, new Object[] {arg1, arg2}, newInstance);
159            }
160    
161            /**
162             * @deprecated
163             */
164            public static Object invoke(
165                            String className, String methodName, Object arg1, Object arg2,
166                            Object arg3, boolean newInstance)
167                    throws Exception {
168    
169                    return invoke(
170                            className, methodName, new Object[] {arg1, arg2, arg3},
171                            newInstance);
172            }
173    
174            /**
175             * @deprecated
176             */
177            public static Object invoke(
178                            String className, String methodName, Object arg1, Object arg2,
179                            Object arg3, Object arg4, boolean newInstance)
180                    throws Exception {
181    
182                    return invoke(
183                            className, methodName, new Object[] {arg1, arg2, arg3, arg4},
184                            newInstance);
185            }
186    
187            /**
188             * @deprecated
189             */
190            public static Object invoke(
191                            String className, String methodName, Object arg1, Object arg2,
192                            Object arg3, Object arg4, Object arg5, boolean newInstance)
193                    throws Exception {
194    
195                    return invoke(
196                            className, methodName, new Object[] {arg1, arg2, arg3, arg4, arg5},
197                            newInstance);
198            }
199    
200            /**
201             * @deprecated
202             */
203            public static Object invoke(
204                            String className, String methodName, Object[] args,
205                            boolean newInstance)
206                    throws Exception {
207    
208                    Thread currentThread = Thread.currentThread();
209    
210                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
211    
212                    try {
213                            currentThread.setContextClassLoader(
214                                    PortalClassLoaderUtil.getClassLoader());
215    
216                            MethodWrapper methodWrapper = new MethodWrapper(
217                                    className, methodName, args);
218    
219                            return MethodInvoker.invoke(methodWrapper, newInstance);
220                    }
221                    catch (InvocationTargetException ite) {
222                            throw (Exception)ite.getCause();
223                    }
224                    finally {
225                            currentThread.setContextClassLoader(contextClassLoader);
226                    }
227            }
228    
229    }