001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.proxy.interceptor;
019    
020    import org.aopalliance.intercept.MethodInterceptor;
021    import org.aopalliance.intercept.MethodInvocation;
022    import org.apache.commons.proxy.Interceptor;
023    import org.apache.commons.proxy.Invocation;
024    
025    import java.lang.reflect.AccessibleObject;
026    import java.lang.reflect.Method;
027    
028    /**
029     * An adapter class to adapt AOP Alliance's {@link MethodInterceptor} interface to Commons Proxy's
030     * {@link Interceptor} interface.
031     *
032     * <p>
033     * <b>Dependencies</b>:
034     * <ul>
035     *   <li>AOP Alliance API version 1.0 or greater</li>
036     * </ul>
037     * </p>
038     * @author James Carman
039     * @since 1.0
040     */
041    public class MethodInterceptorAdapter implements Interceptor
042    {
043        private final MethodInterceptor methodInterceptor;
044    
045        public MethodInterceptorAdapter( MethodInterceptor methodInterceptor )
046        {
047            this.methodInterceptor = methodInterceptor;
048        }
049    
050        public Object intercept( Invocation invocation ) throws Throwable
051        {
052            return methodInterceptor.invoke( new MethodInvocationAdapter( invocation ) );
053        }
054    
055        private static class MethodInvocationAdapter implements MethodInvocation
056        {
057            private final Invocation invocation;
058    
059            public MethodInvocationAdapter( Invocation invocation )
060            {
061                this.invocation = invocation;
062            }
063    
064            public Method getMethod()
065            {
066                return invocation.getMethod();
067            }
068    
069            public Object[] getArguments()
070            {
071                return invocation.getArguments();
072            }
073    
074            public Object proceed() throws Throwable
075            {
076                return invocation.proceed();
077            }
078    
079            public Object getThis()
080            {
081                return invocation.getProxy();
082            }
083    
084            public AccessibleObject getStaticPart()
085            {
086                return invocation.getMethod();
087            }
088        }
089    }