001    /**
002     * Copyright (c) 2000-present 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 As of 6.1.0
081             */
082            @Deprecated
083            public Object invoke(String methodName, Object[] args) throws Throwable {
084                    Thread currentThread = Thread.currentThread();
085    
086                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
087    
088                    try {
089                            currentThread.setContextClassLoader(_classLoader);
090    
091                            Class<?> clazz = Class.forName(_className, true, _classLoader);
092    
093                            List<Class<?>> parameterTypes = new ArrayList<>();
094    
095                            for (int i = 0; i < args.length; i++) {
096                                    Object arg = args[i];
097    
098                                    Class<?> argClass = Class.forName(
099                                            arg.getClass().getName(), true, _classLoader);
100    
101                                    if (ClassUtil.isSubclass(argClass, PrimitiveWrapper.class)) {
102                                            MethodKey methodKey = new MethodKey(argClass, "getValue");
103    
104                                            Method method = methodKey.getMethod();
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    
189                            try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(
190                                            unsyncByteArrayOutputStream)) {
191    
192                                    objectOutputStream.writeObject(throwable);
193    
194                                    objectOutputStream.flush();
195                            }
196    
197                            UnsyncByteArrayInputStream unsyncByteArrayInputStream =
198                                    new UnsyncByteArrayInputStream(
199                                            unsyncByteArrayOutputStream.unsafeGetByteArray(), 0,
200                                            unsyncByteArrayOutputStream.size());
201    
202                            try (ObjectInputStream objectInputStream =
203                                            new ClassLoaderObjectInputStream(
204                                                    unsyncByteArrayInputStream, contextClassLoader)) {
205    
206                                    return (Throwable)objectInputStream.readObject();
207                            }
208                    }
209                    catch (Throwable throwable2) {
210                            _log.error(throwable2, throwable2);
211    
212                            return throwable2;
213                    }
214            }
215    
216            private Object _invoke(MethodHandler methodHandler) throws Exception {
217                    try {
218                            return methodHandler.invoke(_obj);
219                    }
220                    catch (NoSuchMethodException nsme) {
221                            MethodKey methodKey = methodHandler.getMethodKey();
222    
223                            String name = methodKey.getMethodName();
224    
225                            Class<?>[] parameterTypes = methodKey.getParameterTypes();
226    
227                            Class<?> clazz = Class.forName(_className, true, _classLoader);
228    
229                            for (Method method : clazz.getMethods()) {
230                                    String curName = method.getName();
231                                    Class<?>[] curParameterTypes = method.getParameterTypes();
232    
233                                    if (!curName.equals(name) ||
234                                            (curParameterTypes.length != parameterTypes.length)) {
235    
236                                            continue;
237                                    }
238    
239                                    boolean correctParams = true;
240    
241                                    for (int j = 0; j < parameterTypes.length; j++) {
242                                            Class<?> a = parameterTypes[j];
243                                            Class<?> b = curParameterTypes[j];
244    
245                                            if (!ClassUtil.isSubclass(a, b.getName())) {
246                                                    correctParams = false;
247    
248                                                    break;
249                                            }
250                                    }
251    
252                                    if (correctParams) {
253                                            return method.invoke(_obj, methodHandler.getArguments());
254                                    }
255                            }
256    
257                            throw nsme;
258                    }
259            }
260    
261            private static final Log _log = LogFactoryUtil.getLog(
262                    ClassLoaderProxy.class);
263    
264            private final ClassLoader _classLoader;
265            private final String _className;
266            private final Object _obj;
267    
268    }