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     * <p/>
032     * <p>
033     * <b>Dependencies</b>:
034     * <ul>
035     * <li>AOP Alliance API version 1.0 or greater</li>
036     * </ul>
037     * </p>
038     *
039     * @author James Carman
040     * @since 1.0
041     */
042    public class MethodInterceptorAdapter implements Interceptor
043    {
044    //**********************************************************************************************************************
045    // Fields
046    //**********************************************************************************************************************
047    
048        private final MethodInterceptor methodInterceptor;
049    
050    //**********************************************************************************************************************
051    // Constructors
052    //**********************************************************************************************************************
053    
054        public MethodInterceptorAdapter( MethodInterceptor methodInterceptor )
055        {
056            this.methodInterceptor = methodInterceptor;
057        }
058    
059    //**********************************************************************************************************************
060    // Interceptor Implementation
061    //**********************************************************************************************************************
062    
063    
064        public Object intercept( Invocation invocation ) throws Throwable
065        {
066            return methodInterceptor.invoke(new MethodInvocationAdapter(invocation));
067        }
068    
069    //**********************************************************************************************************************
070    // Inner Classes
071    //**********************************************************************************************************************
072    
073        private static class MethodInvocationAdapter implements MethodInvocation
074        {
075            private final Invocation invocation;
076    
077            public MethodInvocationAdapter( Invocation invocation )
078            {
079                this.invocation = invocation;
080            }
081    
082            public Method getMethod()
083            {
084                return invocation.getMethod();
085            }
086    
087            public Object[] getArguments()
088            {
089                return invocation.getArguments();
090            }
091    
092            public Object proceed() throws Throwable
093            {
094                return invocation.proceed();
095            }
096    
097            public Object getThis()
098            {
099                return invocation.getProxy();
100            }
101    
102            public AccessibleObject getStaticPart()
103            {
104                return invocation.getMethod();
105            }
106        }
107    }