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            public static Object invoke(
060                            boolean newInstance, String className, String methodName,
061                            String[] parameterTypeNames, Object... arguments)
062                    throws Exception {
063    
064                    Thread currentThread = Thread.currentThread();
065    
066                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
067    
068                    try {
069                            currentThread.setContextClassLoader(
070                                    PortalClassLoaderUtil.getClassLoader());
071    
072                            MethodKey methodKey = new MethodKey(
073                                    className, methodName, parameterTypeNames);
074    
075                            MethodHandler methodHandler = new MethodHandler(
076                                    methodKey, arguments);
077    
078                            return methodHandler.invoke(newInstance);
079                    }
080                    catch (InvocationTargetException ite) {
081                            Throwable cause = ite.getCause();
082    
083                            if (cause instanceof Error) {
084                                    throw new SystemException(ite);
085                            }
086                            else {
087                                    throw (Exception)cause;
088                            }
089                    }
090                    finally {
091                            currentThread.setContextClassLoader(contextClassLoader);
092                    }
093            }
094    
095            /**
096             * @deprecated
097             */
098            public static Object invoke(String className, String methodName)
099                    throws Exception {
100    
101                    return invoke(className, methodName, new Object[] {});
102            }
103    
104            /**
105             * @deprecated
106             */
107            public static Object invoke(
108                            String className, String methodName, boolean newInstance)
109                    throws Exception {
110    
111                    return invoke(className, methodName, new Object[] {}, newInstance);
112            }
113    
114            /**
115             * @deprecated
116             */
117            public static Object invoke(String className, String methodName, Object arg)
118                    throws Exception {
119    
120                    return invoke(className, methodName, new Object[] {arg});
121            }
122    
123            /**
124             * @deprecated
125             */
126            public static Object invoke(
127                            String className, String methodName, Object arg,
128                            boolean newInstance)
129                    throws Exception {
130    
131                    return invoke(className, methodName, new Object[] {arg}, newInstance);
132            }
133    
134            /**
135             * @deprecated
136             */
137            public static Object invoke(
138                            String className, String methodName, Object arg1, Object arg2)
139                    throws Exception {
140    
141                    return invoke(className, methodName, new Object[] {arg1, arg2});
142            }
143    
144            /**
145             * @deprecated
146             */
147            public static Object invoke(
148                            String className, String methodName, Object arg1, Object arg2,
149                            boolean newInstance)
150                    throws Exception {
151    
152                    return invoke(
153                            className, methodName, new Object[] {arg1, arg2}, newInstance);
154            }
155    
156            /**
157             * @deprecated
158             */
159            public static Object invoke(
160                            String className, String methodName, Object arg1, Object arg2,
161                            Object arg3)
162                    throws Exception {
163    
164                    return invoke(className, methodName, new Object[] {arg1, arg2, arg3});
165            }
166    
167            /**
168             * @deprecated
169             */
170            public static Object invoke(
171                            String className, String methodName, Object arg1, Object arg2,
172                            Object arg3, boolean newInstance)
173                    throws Exception {
174    
175                    return invoke(
176                            className, methodName, new Object[] {arg1, arg2, arg3},
177                            newInstance);
178            }
179    
180            /**
181             * @deprecated
182             */
183            public static Object invoke(
184                            String className, String methodName, Object arg1, Object arg2,
185                            Object arg3, Object arg4, boolean newInstance)
186                    throws Exception {
187    
188                    return invoke(
189                            className, methodName, new Object[] {arg1, arg2, arg3, arg4},
190                            newInstance);
191            }
192    
193            /**
194             * @deprecated
195             */
196            public static Object invoke(
197                            String className, String methodName, Object arg1, Object arg2,
198                            Object arg3, Object arg4, Object arg5, boolean newInstance)
199                    throws Exception {
200    
201                    return invoke(
202                            className, methodName, new Object[] {arg1, arg2, arg3, arg4, arg5},
203                            newInstance);
204            }
205    
206            /**
207             * @deprecated
208             */
209            public static Object invoke(
210                            String className, String methodName, Object[] args)
211                    throws Exception {
212    
213                    return invoke(className, methodName, args, true);
214            }
215    
216            /**
217             * @deprecated
218             */
219            public static Object invoke(
220                            String className, String methodName, Object[] args,
221                            boolean newInstance)
222                    throws Exception {
223    
224                    Thread currentThread = Thread.currentThread();
225    
226                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
227    
228                    try {
229                            currentThread.setContextClassLoader(
230                                    PortalClassLoaderUtil.getClassLoader());
231    
232                            MethodWrapper methodWrapper = new MethodWrapper(
233                                    className, methodName, args);
234    
235                            return MethodInvoker.invoke(methodWrapper, newInstance);
236                    }
237                    catch (InvocationTargetException ite) {
238                            throw (Exception)ite.getCause();
239                    }
240                    finally {
241                            currentThread.setContextClassLoader(contextClassLoader);
242                    }
243            }
244    
245    }