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.messaging.proxy;
016    
017    import com.liferay.portal.kernel.util.AggregateClassLoader;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    
020    import java.lang.annotation.Annotation;
021    import java.lang.reflect.InvocationTargetException;
022    import java.lang.reflect.Method;
023    
024    /**
025     * @author Michael C. Han
026     */
027    public class MultiClassLoaderProxyRequest extends ProxyRequest {
028    
029            public MultiClassLoaderProxyRequest(Method method, Object[] arguments)
030                    throws Exception {
031    
032                    super(method, arguments);
033    
034                    ClassLoader[] classLoaders = inspectForClassLoaders(method);
035    
036                    _clientClassLoaders = AggregateClassLoader.getAggregateClassLoader(
037                            classLoaders);
038            }
039    
040            @Override
041            public Object execute(Object object) throws Exception {
042                    ClassLoader contextClassLoader = null;
043    
044                    if (_clientClassLoaders != null) {
045                            Thread currentThread = Thread.currentThread();
046    
047                            contextClassLoader = currentThread.getContextClassLoader();
048    
049                            ClassLoader invocationClassLoader =
050                                    AggregateClassLoader.getAggregateClassLoader(
051                                            new ClassLoader[] {
052                                                    contextClassLoader, _clientClassLoaders
053                                            });
054    
055                            currentThread.setContextClassLoader(invocationClassLoader);
056                    }
057    
058                    try {
059                            return super.execute(object);
060                    }
061                    catch (InvocationTargetException ite) {
062                            throw new Exception(ite.getTargetException());
063                    }
064                    finally {
065                            if (contextClassLoader != null) {
066                                    Thread currentThread = Thread.currentThread();
067    
068                                    currentThread.setContextClassLoader(contextClassLoader);
069                            }
070                    }
071            }
072    
073            protected ClassLoader[] inspectForClassLoaders(Method method)
074                    throws Exception {
075    
076                    Annotation[][] annotationsArray = method.getParameterAnnotations();
077    
078                    if (ArrayUtil.isEmpty(annotationsArray)) {
079                            return null;
080                    }
081    
082                    for (int i = 0; i < annotationsArray.length; i++) {
083                            Annotation[] annotations = annotationsArray[i];
084    
085                            if (ArrayUtil.isEmpty(annotations)) {
086                                    continue;
087                            }
088    
089                            for (Annotation annotation : annotations) {
090                                    if (ExecutingClassLoaders.class.isAssignableFrom(
091                                                    annotation.annotationType())) {
092    
093                                            return (ClassLoader[])getArguments()[i];
094                                    }
095                            }
096                    }
097    
098                    return null;
099            }
100    
101            private ClassLoader _clientClassLoaders;
102    
103    }