1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.messaging.proxy;
16  
17  import com.liferay.portal.kernel.util.AggregateClassLoader;
18  
19  import java.lang.annotation.Annotation;
20  import java.lang.reflect.InvocationTargetException;
21  import java.lang.reflect.Method;
22  
23  /**
24   * <a href="MultiClassLoaderProxyRequest.java.html"><b><i>View Source</i></b>
25   * </a>
26   *
27   * @author Michael C. Han
28   */
29  public class MultiClassLoaderProxyRequest extends ProxyRequest {
30  
31      public MultiClassLoaderProxyRequest(
32              Method method, Object[] arguments)
33          throws Exception {
34  
35          super(method, arguments);
36  
37          ClassLoader[] classLoaders = inspectForClassLoaders(method);
38  
39          _clientClassLoaders = AggregateClassLoader.getAggregateClassLoader(
40              classLoaders);
41      }
42  
43      public Object execute(Object object) throws Exception {
44          ClassLoader contextClassLoader = null;
45  
46          if (_clientClassLoaders != null) {
47              Thread currentThread = Thread.currentThread();
48  
49              contextClassLoader = currentThread.getContextClassLoader();
50  
51              ClassLoader invocationClassLoader =
52                  AggregateClassLoader.getAggregateClassLoader(
53                      new ClassLoader[] {
54                          contextClassLoader, _clientClassLoaders
55                      });
56  
57              currentThread.setContextClassLoader(invocationClassLoader);
58          }
59  
60          try {
61              return super.execute(object);
62          }
63          catch (InvocationTargetException ite) {
64              throw new Exception(ite.getTargetException());
65          }
66          finally {
67              if (contextClassLoader != null) {
68                  Thread currentThread = Thread.currentThread();
69  
70                  currentThread.setContextClassLoader(contextClassLoader);
71              }
72          }
73      }
74  
75      protected ClassLoader[] inspectForClassLoaders(Method method)
76          throws Exception {
77  
78          Annotation[][] annotationsArray = method.getParameterAnnotations();
79  
80          if ((annotationsArray == null) || (annotationsArray.length == 0)) {
81              return null;
82          }
83  
84          for (int i = 0; i < annotationsArray.length; i++) {
85              Annotation[] annotations = annotationsArray[i];
86  
87              if ((annotations == null) || (annotations.length == 0)) {
88                  continue;
89              }
90  
91              for (Annotation annotation : annotations) {
92                  if (ExecutingClassLoaders.class.isAssignableFrom(
93                          annotation.annotationType())) {
94  
95                      return (ClassLoader[])getMethodWrapper().getArguments()[i];
96                  }
97              }
98          }
99  
100         return null;
101     }
102 
103     private ClassLoader _clientClassLoaders;
104 
105 }