1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.messaging.proxy;
24  
25  import com.liferay.portal.kernel.util.AggregateClassLoader;
26  
27  import java.lang.annotation.Annotation;
28  import java.lang.reflect.InvocationTargetException;
29  import java.lang.reflect.Method;
30  
31  /**
32   * <a href="MultiClassLoaderProxyRequest.java.html"><b><i>View Source</i></b>
33   * </a>
34   *
35   * @author Michael C. Han
36   */
37  public class MultiClassLoaderProxyRequest extends ProxyRequest {
38  
39      public MultiClassLoaderProxyRequest(
40              Method method, Object[] arguments)
41          throws Exception {
42  
43          super(method, arguments);
44  
45          ClassLoader[] classLoaders = inspectForClassLoaders(method);
46  
47          _clientClassLoaders = AggregateClassLoader.getAggregateClassLoader(
48              classLoaders);
49      }
50  
51      public Object execute(Object object) throws Exception {
52          ClassLoader contextClassLoader = null;
53  
54          if (_clientClassLoaders != null) {
55              Thread currentThread = Thread.currentThread();
56  
57              contextClassLoader = currentThread.getContextClassLoader();
58  
59              ClassLoader invocationClassLoader =
60                  AggregateClassLoader.getAggregateClassLoader(
61                      new ClassLoader[] {
62                          contextClassLoader, _clientClassLoaders
63                      });
64  
65              currentThread.setContextClassLoader(invocationClassLoader);
66          }
67  
68          try {
69              return super.execute(object);
70          }
71          catch (InvocationTargetException ite) {
72              throw new Exception(ite.getTargetException());
73          }
74          finally {
75              if (contextClassLoader != null) {
76                  Thread currentThread = Thread.currentThread();
77  
78                  currentThread.setContextClassLoader(contextClassLoader);
79              }
80          }
81      }
82  
83      protected ClassLoader[] inspectForClassLoaders(Method method)
84          throws Exception {
85  
86          Annotation[][] annotationsArray = method.getParameterAnnotations();
87  
88          if ((annotationsArray == null) || (annotationsArray.length == 0)) {
89              return null;
90          }
91  
92          for (int i = 0; i < annotationsArray.length; i++) {
93              Annotation[] annotations = annotationsArray[i];
94  
95              if ((annotations == null) || (annotations.length == 0)) {
96                  continue;
97              }
98  
99              for (Annotation annotation : annotations) {
100                 if (ExecutingClassLoaders.class.isAssignableFrom(
101                         annotation.annotationType())) {
102 
103                     return (ClassLoader[])getMethodWrapper().getArguments()[i];
104                 }
105             }
106         }
107 
108         return null;
109     }
110 
111     private ClassLoader _clientClassLoaders;
112 
113 }