View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.aspectj;
20  
21  import org.apache.shiro.aop.MethodInvocation;
22  import org.aspectj.lang.JoinPoint;
23  import org.aspectj.lang.reflect.AdviceSignature;
24  import org.aspectj.lang.reflect.MethodSignature;
25  
26  import java.lang.reflect.Method;
27  
28  /**
29   * Helper class that adapts an AspectJ {@link JoinPoint JoinPoint}.
30   *
31   * @since 1.0
32   */
33  public class BeforeAdviceMethodInvocationAdapter implements MethodInvocation {
34  
35      private Object _object;
36      private Method _method;
37      private Object[] _arguments;
38  
39      /**
40       * Factory method that creates a new {@link BeforeAdviceMethodInvocationAdapter} instance
41       * using the AspectJ {@link JoinPoint} provided. If the joint point passed in is not
42       * a method joint point, this method throws an {@link IllegalArgumentException}.
43       *
44       * @param aJoinPoint The AspectJ {@link JoinPoint} to use to adapt the advice.
45       * @return The created instance.
46       * @throws IllegalArgumentException If the join point passed in does not involve a method call.
47       */
48      public static BeforeAdviceMethodInvocationAdapter createFrom(JoinPoint aJoinPoint) {
49          if (aJoinPoint.getSignature() instanceof MethodSignature) {
50              return new BeforeAdviceMethodInvocationAdapter(aJoinPoint.getThis(),
51                      ((MethodSignature) aJoinPoint.getSignature()).getMethod(),
52                      aJoinPoint.getArgs());
53  
54          } else if (aJoinPoint.getSignature() instanceof AdviceSignature) {
55              return new BeforeAdviceMethodInvocationAdapter(aJoinPoint.getThis(),
56                      ((AdviceSignature) aJoinPoint.getSignature()).getAdvice(),
57                      aJoinPoint.getArgs());
58  
59          } else {
60              throw new IllegalArgumentException("The joint point signature is invalid: expected a MethodSignature or an AdviceSignature but was " + aJoinPoint.getSignature());
61          }
62      }
63  
64      /**
65       * Creates a new {@link BeforeAdviceMethodInvocationAdapter} instance.
66       *
67       * @param aMethod       The method to invoke.
68       * @param someArguments The arguments of the method invocation.
69       */
70      public BeforeAdviceMethodInvocationAdapter(Object anObject, Method aMethod, Object[] someArguments) {
71          _object = anObject;
72          _method = aMethod;
73          _arguments = someArguments;
74      }
75  
76      /* (non-Javadoc)
77      * @see org.apache.shiro.aop.MethodInvocation#getArguments()
78      */
79  
80      public Object[] getArguments() {
81          return _arguments;
82      }
83  
84      /* (non-Javadoc)
85      * @see org.apache.shiro.aop.MethodInvocation#getMethod()
86      */
87  
88      public Method getMethod() {
89          return _method;
90      }
91  
92      /* (non-Javadoc)
93      * @see org.apache.shiro.aop.MethodInvocation#proceed()
94      */
95  
96      public Object proceed() throws Throwable {
97          // Do nothing since this adapts a before advice
98          return null;
99      }
100 
101     /**
102      * @since 1.0
103      */
104     public Object getThis() {
105         return _object;
106     }
107 }