View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.proxy.interceptor;
19  
20  import org.aopalliance.intercept.MethodInterceptor;
21  import org.aopalliance.intercept.MethodInvocation;
22  import org.apache.commons.proxy.Interceptor;
23  import org.apache.commons.proxy.Invocation;
24  
25  import java.lang.reflect.AccessibleObject;
26  import java.lang.reflect.Method;
27  
28  /**
29   * An adapter class to adapt AOP Alliance's {@link MethodInterceptor} interface to Commons Proxy's
30   * {@link Interceptor} interface.
31   *
32   * <p>
33   * <b>Dependencies</b>:
34   * <ul>
35   *   <li>AOP Alliance API version 1.0 or greater</li>
36   * </ul>
37   * </p>
38   * @author James Carman
39   * @since 1.0
40   */
41  public class MethodInterceptorAdapter implements Interceptor
42  {
43      private final MethodInterceptor methodInterceptor;
44  
45      public MethodInterceptorAdapter( MethodInterceptor methodInterceptor )
46      {
47          this.methodInterceptor = methodInterceptor;
48      }
49  
50      public Object intercept( Invocation invocation ) throws Throwable
51      {
52          return methodInterceptor.invoke( new MethodInvocationAdapter( invocation ) );
53      }
54  
55      private static class MethodInvocationAdapter implements MethodInvocation
56      {
57          private final Invocation invocation;
58  
59          public MethodInvocationAdapter( Invocation invocation )
60          {
61              this.invocation = invocation;
62          }
63  
64          public Method getMethod()
65          {
66              return invocation.getMethod();
67          }
68  
69          public Object[] getArguments()
70          {
71              return invocation.getArguments();
72          }
73  
74          public Object proceed() throws Throwable
75          {
76              return invocation.proceed();
77          }
78  
79          public Object getThis()
80          {
81              return invocation.getProxy();
82          }
83  
84          public AccessibleObject getStaticPart()
85          {
86              return invocation.getMethod();
87          }
88      }
89  }