001    /**
002     * Copyright (c) 2000-2011 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(String className, String methodName, Object arg)
108                    throws Exception {
109    
110                    return invoke(className, methodName, new Object[] {arg});
111            }
112    
113            /**
114             * @deprecated
115             */
116            public static Object invoke(
117                            String className, String methodName, Object arg1, Object arg2)
118                    throws Exception {
119    
120                    return invoke(className, methodName, new Object[] {arg1, arg2});
121            }
122    
123            /**
124             * @deprecated
125             */
126            public static Object invoke(
127                            String className, String methodName, Object arg1, Object arg2,
128                            Object arg3)
129                    throws Exception {
130    
131                    return invoke(className, methodName, new Object[] {arg1, arg2, arg3});
132            }
133    
134            /**
135             * @deprecated
136             */
137            public static Object invoke(
138                            String className, String methodName, Object[] args)
139                    throws Exception {
140    
141                    return invoke(className, methodName, args, true);
142            }
143    
144            /**
145             * @deprecated
146             */
147            public static Object invoke(
148                    String className, String methodName, boolean newInstance)
149                    throws Exception {
150    
151                    return invoke(className, methodName, new Object[] {}, newInstance);
152            }
153    
154            /**
155             * @deprecated
156             */
157            public static Object invoke(
158                            String className, String methodName, Object arg,
159                            boolean newInstance)
160                    throws Exception {
161    
162                    return invoke(className, methodName, new Object[] {arg}, newInstance);
163            }
164    
165            /**
166             * @deprecated
167             */
168            public static Object invoke(
169                            String className, String methodName, Object arg1, Object arg2,
170                            boolean newInstance)
171                    throws Exception {
172    
173                    return invoke(
174                            className, methodName, new Object[] {arg1, arg2}, newInstance);
175            }
176    
177            /**
178             * @deprecated
179             */
180            public static Object invoke(
181                            String className, String methodName, Object arg1, Object arg2,
182                            Object arg3, boolean newInstance)
183                    throws Exception {
184    
185                    return invoke(
186                            className, methodName, new Object[] {arg1, arg2, arg3},
187                            newInstance);
188            }
189    
190            /**
191             * @deprecated
192             */
193            public static Object invoke(
194                            String className, String methodName, Object arg1, Object arg2,
195                            Object arg3, Object arg4, boolean newInstance)
196                    throws Exception {
197    
198                    return invoke(
199                            className, methodName, new Object[] {arg1, arg2, arg3, arg4},
200                            newInstance);
201            }
202    
203            /**
204             * @deprecated
205             */
206            public static Object invoke(
207                            String className, String methodName, Object arg1, Object arg2,
208                            Object arg3, Object arg4, Object arg5, boolean newInstance)
209                    throws Exception {
210    
211                    return invoke(
212                            className, methodName, new Object[] {arg1, arg2, arg3, arg4, arg5},
213                            newInstance);
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    }