001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.shiro.aspectj;
020    
021    import org.apache.shiro.aop.MethodInvocation;
022    import org.aspectj.lang.JoinPoint;
023    import org.aspectj.lang.reflect.AdviceSignature;
024    import org.aspectj.lang.reflect.MethodSignature;
025    
026    import java.lang.reflect.Method;
027    
028    /**
029     * Helper class that adapts an AspectJ {@link JoinPoint JoinPoint}.
030     *
031     * @author J-C Desrochers
032     * @since 1.0
033     */
034    public class BeforeAdviceMethodInvocationAdapter implements MethodInvocation {
035    
036        private Object _object;
037        private Method _method;
038        private Object[] _arguments;
039    
040        /**
041         * Factory method that creates a new {@link BeforeAdviceMethodInvocationAdapter} instance
042         * using the AspectJ {@link JoinPoint} provided. If the joint point passed in is not
043         * a method joint point, this method throws an {@link IllegalArgumentException}.
044         *
045         * @param aJoinPoint The AspectJ {@link JoinPoint} to use to adapt the advice.
046         * @return The created instance.
047         * @throws IllegalArgumentException If the join point passed in does not involve a method call.
048         */
049        public static BeforeAdviceMethodInvocationAdapter createFrom(JoinPoint aJoinPoint) {
050            if (aJoinPoint.getSignature() instanceof MethodSignature) {
051                return new BeforeAdviceMethodInvocationAdapter(aJoinPoint.getThis(),
052                        ((MethodSignature) aJoinPoint.getSignature()).getMethod(),
053                        aJoinPoint.getArgs());
054    
055            } else if (aJoinPoint.getSignature() instanceof AdviceSignature) {
056                return new BeforeAdviceMethodInvocationAdapter(aJoinPoint.getThis(),
057                        ((AdviceSignature) aJoinPoint.getSignature()).getAdvice(),
058                        aJoinPoint.getArgs());
059    
060            } else {
061                throw new IllegalArgumentException("The joint point signature is invalid: expected a MethodSignature or an AdviceSignature but was " + aJoinPoint.getSignature());
062            }
063        }
064    
065        /**
066         * Creates a new {@link BeforeAdviceMethodInvocationAdapter} instance.
067         *
068         * @param aMethod       The method to invoke.
069         * @param someArguments The arguments of the method invocation.
070         */
071        public BeforeAdviceMethodInvocationAdapter(Object anObject, Method aMethod, Object[] someArguments) {
072            _object = anObject;
073            _method = aMethod;
074            _arguments = someArguments;
075        }
076    
077        /* (non-Javadoc)
078        * @see org.apache.shiro.aop.MethodInvocation#getArguments()
079        */
080    
081        public Object[] getArguments() {
082            return _arguments;
083        }
084    
085        /* (non-Javadoc)
086        * @see org.apache.shiro.aop.MethodInvocation#getMethod()
087        */
088    
089        public Method getMethod() {
090            return _method;
091        }
092    
093        /* (non-Javadoc)
094        * @see org.apache.shiro.aop.MethodInvocation#proceed()
095        */
096    
097        public Object proceed() throws Throwable {
098            // Do nothing since this adapts a before advice
099            return null;
100        }
101    
102        /**
103         * @since 1.0
104         */
105        public Object getThis() {
106            return _object;
107        }
108    }