001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.intercept.method.aopalliance;
017:
018: import org.acegisecurity.intercept.method.MethodDefinitionSource;
019:
020: import org.aopalliance.intercept.MethodInvocation;
021:
022: import org.springframework.aop.framework.AopConfigException;
023: import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
024:
025: import java.lang.reflect.AccessibleObject;
026: import java.lang.reflect.Method;
027:
028: /**
029: * Advisor driven by a {@link MethodDefinitionSource}, used to exclude a {@link MethodSecurityInterceptor} from
030: * public (ie non-secure) methods.<p>Because the AOP framework caches advice calculations, this is normally faster
031: * than just letting the <code>MethodSecurityInterceptor</code> run and find out itself that it has no work to do.</p>
032: * <p>This class also allows the use of Spring's <code>DefaultAdvisorAutoProxyCreator</code>, which makes
033: * configuration easier than setup a <code>ProxyFactoryBean</code> for each object requiring security. Note that
034: * autoproxying is not supported for BeanFactory implementations, as post-processing is automatic only for application
035: * contexts.</p>
036: * <p>Based on Spring's TransactionAttributeSourceAdvisor.</p>
037: *
038: * @author Ben Alex
039: * @version $Id: MethodDefinitionSourceAdvisor.java 1784 2007-02-24 21:00:24Z luke_t $
040: */
041: public class MethodDefinitionSourceAdvisor extends
042: StaticMethodMatcherPointcutAdvisor {
043: //~ Instance fields ================================================================================================
044:
045: private MethodDefinitionSource transactionAttributeSource;
046:
047: //~ Constructors ===================================================================================================
048:
049: public MethodDefinitionSourceAdvisor(
050: MethodSecurityInterceptor advice) {
051: super (advice);
052:
053: if (advice.getObjectDefinitionSource() == null) {
054: throw new AopConfigException(
055: "Cannot construct a MethodDefinitionSourceAdvisor using a "
056: + "MethodSecurityInterceptor that has no ObjectDefinitionSource configured");
057: }
058:
059: this .transactionAttributeSource = advice
060: .getObjectDefinitionSource();
061: }
062:
063: //~ Methods ========================================================================================================
064:
065: public boolean matches(Method m, Class targetClass) {
066: MethodInvocation methodInvocation = new InternalMethodInvocation(
067: m);
068:
069: return (this .transactionAttributeSource
070: .getAttributes(methodInvocation) != null);
071: }
072:
073: //~ Inner Classes ==================================================================================================
074:
075: /**
076: * Represents a <code>MethodInvocation</code>.
077: * <p>Required as <code>MethodDefinitionSource</code> only supports lookup of configuration attributes for
078: * <code>MethodInvocation</code>s.</p>
079: */
080: class InternalMethodInvocation implements MethodInvocation {
081: private Method method;
082:
083: public InternalMethodInvocation(Method method) {
084: this .method = method;
085: }
086:
087: protected InternalMethodInvocation() {
088: throw new UnsupportedOperationException();
089: }
090:
091: public Object[] getArguments() {
092: throw new UnsupportedOperationException();
093: }
094:
095: public Method getMethod() {
096: return this .method;
097: }
098:
099: public AccessibleObject getStaticPart() {
100: throw new UnsupportedOperationException();
101: }
102:
103: public Object getThis() {
104: throw new UnsupportedOperationException();
105: }
106:
107: public Object proceed() throws Throwable {
108: throw new UnsupportedOperationException();
109: }
110: }
111: }
|