View Javadoc

1   /* Copyright 2004 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.intercept.method.aopalliance;
17  
18  import org.acegisecurity.intercept.method.MethodDefinitionSource;
19  
20  import org.aopalliance.intercept.MethodInvocation;
21  
22  import org.springframework.aop.framework.AopConfigException;
23  import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
24  
25  import java.lang.reflect.AccessibleObject;
26  import java.lang.reflect.Method;
27  
28  
29  /***
30   * Advisor driven by a {@link MethodDefinitionSource}, used to exclude a {@link
31   * MethodSecurityInterceptor} from public (ie non-secure) methods.
32   * 
33   * <p>
34   * Because the AOP framework caches advice calculations, this is normally
35   * faster than just letting the <code>MethodSecurityInterceptor</code> run and
36   * find out itself that it has no work to do.
37   * </p>
38   * 
39   * <p>
40   * This class also allows the use of Spring's
41   * <code>DefaultAdvisorAutoProxyCreator</code>, which makes configuration
42   * easier than setup a <code>ProxyFactoryBean</code> for each object requiring
43   * security. Note that autoproxying is not supported for BeanFactory
44   * implementations, as post-processing is automatic only for application
45   * contexts.
46   * </p>
47   * 
48   * <p>
49   * Based on Spring's TransactionAttributeSourceAdvisor.
50   * </p>
51   *
52   * @author Ben Alex
53   * @version $Id: MethodDefinitionSourceAdvisor.java,v 1.3 2005/11/17 00:55:49 benalex Exp $
54   */
55  public class MethodDefinitionSourceAdvisor
56      extends StaticMethodMatcherPointcutAdvisor {
57      //~ Instance fields ========================================================
58  
59      private MethodDefinitionSource transactionAttributeSource;
60  
61      //~ Constructors ===========================================================
62  
63      public MethodDefinitionSourceAdvisor(MethodSecurityInterceptor advice) {
64          super(advice);
65  
66          if (advice.getObjectDefinitionSource() == null) {
67              throw new AopConfigException(
68                  "Cannot construct a MethodDefinitionSourceAdvisor using a "
69                  + "MethodSecurityInterceptor that has no ObjectDefinitionSource configured");
70          }
71  
72          this.transactionAttributeSource = advice.getObjectDefinitionSource();
73      }
74  
75      //~ Methods ================================================================
76  
77      public boolean matches(Method m, Class targetClass) {
78          MethodInvocation methodInvocation = new InternalMethodInvocation(m);
79  
80          return (this.transactionAttributeSource.getAttributes(methodInvocation) != null);
81      }
82  
83      //~ Inner Classes ==========================================================
84  
85      /***
86       * Represents a <code>MethodInvocation</code>.
87       * 
88       * <p>
89       * Required as <code>MethodDefinitionSource</code> only supports lookup of
90       * configuration attributes for <code>MethodInvocation</code>s.
91       * </p>
92       */
93      class InternalMethodInvocation implements MethodInvocation {
94          Method method;
95  
96          public InternalMethodInvocation(Method method) {
97              this.method = method;
98          }
99  
100         protected InternalMethodInvocation() {
101             throw new UnsupportedOperationException();
102         }
103 
104         public Object[] getArguments() {
105             throw new UnsupportedOperationException();
106         }
107 
108         public Method getMethod() {
109             return this.method;
110         }
111 
112         public AccessibleObject getStaticPart() {
113             throw new UnsupportedOperationException();
114         }
115 
116         public Object getThis() {
117             throw new UnsupportedOperationException();
118         }
119 
120         public Object proceed() throws Throwable {
121             throw new UnsupportedOperationException();
122         }
123     }
124 }