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.io.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    
022    import java.io.ObjectInputStream;
023    import java.io.ObjectOutputStream;
024    
025    import java.lang.reflect.InvocationTargetException;
026    import java.lang.reflect.Method;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class ClassLoaderProxy {
035    
036            public ClassLoaderProxy(Object obj, ClassLoader classLoader) {
037                    this(obj, obj.getClass().getName(), classLoader);
038            }
039    
040            public ClassLoaderProxy(
041                    Object obj, String className, ClassLoader classLoader) {
042    
043                    _obj = obj;
044                    _className = className;
045                    _classLoader = classLoader;
046            }
047    
048            public ClassLoader getClassLoader() {
049                    return _classLoader;
050            }
051    
052            public String getClassName() {
053                    return _className;
054            }
055    
056            public Object invoke(MethodHandler methodHandler) throws Throwable {
057                    Thread currentThread = Thread.currentThread();
058    
059                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
060    
061                    try {
062                            currentThread.setContextClassLoader(_classLoader);
063    
064                            return _invoke(methodHandler);
065                    }
066                    catch (InvocationTargetException ite) {
067                            throw translateThrowable(ite.getCause(), contextClassLoader);
068                    }
069                    catch (Throwable t) {
070                            _log.error(t, t);
071    
072                            throw t;
073                    }
074                    finally {
075                            currentThread.setContextClassLoader(contextClassLoader);
076                    }
077            }
078    
079            /**
080             * @deprecated
081             */
082            public Object invoke(String methodName, Object[] args) throws Throwable {
083                    Thread currentThread = Thread.currentThread();
084    
085                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
086    
087                    try {
088                            currentThread.setContextClassLoader(_classLoader);
089    
090                            Class<?> clazz = Class.forName(_className, true, _classLoader);
091    
092                            List<Class<?>> parameterTypes = new ArrayList<Class<?>>();
093    
094                            for (int i = 0; i < args.length; i++) {
095                                    Object arg = args[i];
096    
097                                    Class<?> argClass = Class.forName(
098                                            arg.getClass().getName(), true, _classLoader);
099    
100                                    if (ClassUtil.isSubclass(argClass, PrimitiveWrapper.class)) {
101                                            MethodKey methodKey = new MethodKey(
102                                                    argClass.getName(), "getValue");
103    
104                                            Method method = MethodCache.get(methodKey);
105    
106                                            args[i] = method.invoke(arg, (Object[])null);
107    
108                                            argClass = (Class<?>)argClass.getField("TYPE").get(arg);
109                                    }
110    
111                                    if (ClassUtil.isSubclass(argClass, NullWrapper.class)) {
112                                            NullWrapper nullWrapper = (NullWrapper)arg;
113    
114                                            argClass = Class.forName(
115                                                    nullWrapper.getClassName(), true, _classLoader);
116    
117                                            args[i] = null;
118                                    }
119    
120                                    parameterTypes.add(argClass);
121                            }
122    
123                            Method method = null;
124    
125                            try {
126                                    method = clazz.getMethod(
127                                            methodName,
128                                            parameterTypes.toArray(new Class[parameterTypes.size()]));
129                            }
130                            catch (NoSuchMethodException nsme) {
131                                    Method[] methods = ((Class<?>)clazz).getMethods();
132    
133                                    for (int i = 0; i < methods.length; i++) {
134                                            Class<?>[] methodParameterTypes =
135                                                    methods[i].getParameterTypes();
136    
137                                            if (methods[i].getName().equals(methodName) &&
138                                                    (methodParameterTypes.length ==
139                                                            parameterTypes.size())) {
140    
141                                                    boolean correctParams = true;
142    
143                                                    for (int j = 0; j < parameterTypes.size(); j++) {
144                                                            Class<?> a = parameterTypes.get(j);
145                                                            Class<?> b = methodParameterTypes[j];
146    
147                                                            if (!ClassUtil.isSubclass(a, b)) {
148                                                                    correctParams = false;
149    
150                                                                    break;
151                                                            }
152                                                    }
153    
154                                                    if (correctParams) {
155                                                            method = methods[i];
156    
157                                                            break;
158                                                    }
159                                            }
160                                    }
161    
162                                    if (method == null) {
163                                            throw nsme;
164                                    }
165                            }
166    
167                            return method.invoke(_obj, args);
168                    }
169                    catch (InvocationTargetException ite) {
170                            throw translateThrowable(ite.getCause(), contextClassLoader);
171                    }
172                    catch (Throwable t) {
173                            _log.error(t, t);
174    
175                            throw t;
176                    }
177                    finally {
178                            currentThread.setContextClassLoader(contextClassLoader);
179                    }
180            }
181    
182            protected Throwable translateThrowable(
183                    Throwable throwable, ClassLoader contextClassLoader) {
184    
185                    try {
186                            UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
187                                    new UnsyncByteArrayOutputStream();
188                            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
189                                    unsyncByteArrayOutputStream);
190    
191                            objectOutputStream.writeObject(throwable);
192    
193                            objectOutputStream.flush();
194                            objectOutputStream.close();
195    
196                            UnsyncByteArrayInputStream unsyncByteArrayInputStream =
197                                    new UnsyncByteArrayInputStream(
198                                            unsyncByteArrayOutputStream.unsafeGetByteArray(), 0,
199                                            unsyncByteArrayOutputStream.size());
200                            ObjectInputStream objectInputStream =
201                                    new ClassLoaderObjectInputStream(
202                                            unsyncByteArrayInputStream, contextClassLoader);
203    
204                            throwable = (Throwable)objectInputStream.readObject();
205    
206                            objectInputStream.close();
207    
208                            return throwable;
209                    }
210                    catch (Throwable throwable2) {
211                            _log.error(throwable2, throwable2);
212    
213                            return throwable2;
214                    }
215            }
216    
217            private Object _invoke(MethodHandler methodHandler) throws Exception {
218                    try {
219                            return methodHandler.invoke(_obj);
220                    }
221                    catch (NoSuchMethodException nsme) {
222                            String name = methodHandler.getMethodName();
223                            Class<?>[] parameterTypes = methodHandler.getArgumentsClasses();
224    
225                            Class<?> clazz = Class.forName(_className, true, _classLoader);
226    
227                            for (Method method : clazz.getMethods()) {
228                                    String curName = method.getName();
229                                    Class<?>[] curParameterTypes = method.getParameterTypes();
230    
231                                    if (!curName.equals(name) ||
232                                            (curParameterTypes.length != parameterTypes.length)) {
233    
234                                            continue;
235                                    }
236    
237                                    boolean correctParams = true;
238    
239                                    for (int j = 0; j < parameterTypes.length; j++) {
240                                            Class<?> a = parameterTypes[j];
241                                            Class<?> b = curParameterTypes[j];
242    
243                                            if (!ClassUtil.isSubclass(a, b.getName())) {
244                                                    correctParams = false;
245    
246                                                    break;
247                                            }
248                                    }
249    
250                                    if (correctParams) {
251                                            return method.invoke(_obj, methodHandler.getArguments());
252                                    }
253                            }
254    
255                            throw nsme;
256                    }
257            }
258    
259            private static Log _log = LogFactoryUtil.getLog(ClassLoaderProxy.class);
260    
261            private ClassLoader _classLoader;
262            private String _className;
263            private Object _obj;
264    
265    }