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 */
019package org.apache.shiro.aspectj;
020
021import org.apache.shiro.aop.MethodInvocation;
022import org.aspectj.lang.JoinPoint;
023import org.aspectj.lang.reflect.AdviceSignature;
024import org.aspectj.lang.reflect.MethodSignature;
025
026import java.lang.reflect.Method;
027
028/**
029 * Helper class that adapts an AspectJ {@link JoinPoint JoinPoint}.
030 *
031 * @since 1.0
032 */
033public class BeforeAdviceMethodInvocationAdapter implements MethodInvocation {
034
035    private Object _object;
036    private Method _method;
037    private Object[] _arguments;
038
039    /**
040     * Factory method that creates a new {@link BeforeAdviceMethodInvocationAdapter} instance
041     * using the AspectJ {@link JoinPoint} provided. If the joint point passed in is not
042     * a method joint point, this method throws an {@link IllegalArgumentException}.
043     *
044     * @param aJoinPoint The AspectJ {@link JoinPoint} to use to adapt the advice.
045     * @return The created instance.
046     * @throws IllegalArgumentException If the join point passed in does not involve a method call.
047     */
048    public static BeforeAdviceMethodInvocationAdapter createFrom(JoinPoint aJoinPoint) {
049        if (aJoinPoint.getSignature() instanceof MethodSignature) {
050            return new BeforeAdviceMethodInvocationAdapter(aJoinPoint.getThis(),
051                    ((MethodSignature) aJoinPoint.getSignature()).getMethod(),
052                    aJoinPoint.getArgs());
053
054        } else if (aJoinPoint.getSignature() instanceof AdviceSignature) {
055            return new BeforeAdviceMethodInvocationAdapter(aJoinPoint.getThis(),
056                    ((AdviceSignature) aJoinPoint.getSignature()).getAdvice(),
057                    aJoinPoint.getArgs());
058
059        } else {
060            throw new IllegalArgumentException("The joint point signature is invalid: expected a MethodSignature or an AdviceSignature but was " + aJoinPoint.getSignature());
061        }
062    }
063
064    /**
065     * Creates a new {@link BeforeAdviceMethodInvocationAdapter} instance.
066     *
067     * @param aMethod       The method to invoke.
068     * @param someArguments The arguments of the method invocation.
069     */
070    public BeforeAdviceMethodInvocationAdapter(Object anObject, Method aMethod, Object[] someArguments) {
071        _object = anObject;
072        _method = aMethod;
073        _arguments = someArguments;
074    }
075
076    /* (non-Javadoc)
077    * @see org.apache.shiro.aop.MethodInvocation#getArguments()
078    */
079
080    public Object[] getArguments() {
081        return _arguments;
082    }
083
084    /* (non-Javadoc)
085    * @see org.apache.shiro.aop.MethodInvocation#getMethod()
086    */
087
088    public Method getMethod() {
089        return _method;
090    }
091
092    /* (non-Javadoc)
093    * @see org.apache.shiro.aop.MethodInvocation#proceed()
094    */
095
096    public Object proceed() throws Throwable {
097        // Do nothing since this adapts a before advice
098        return null;
099    }
100
101    /**
102     * @since 1.0
103     */
104    public Object getThis() {
105        return _object;
106    }
107}