1 /* Copyright 2004, 2005, 2006 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.aspectj;
17
18 import org.acegisecurity.intercept.AbstractSecurityInterceptor;
19 import org.acegisecurity.intercept.InterceptorStatusToken;
20 import org.acegisecurity.intercept.ObjectDefinitionSource;
21 import org.acegisecurity.intercept.method.MethodDefinitionSource;
22
23 import org.aspectj.lang.JoinPoint;
24
25
26 /**
27 * Provides security interception of AspectJ method invocations.<p>The <code>ObjectDefinitionSource</code> required
28 * by this security interceptor is of type {@link MethodDefinitionSource}. This is shared with the AOP Alliance based
29 * security interceptor (<code>MethodSecurityInterceptor</code>), since both work with Java <code>Method</code>s.</p>
30 * <p>The secure object type is <code>org.aspectj.lang.JoinPoint</code>, which is passed from the relevant
31 * <code>around()</code> advice. The <code>around()</code> advice also passes an anonymous implementation of {@link
32 * AspectJCallback} which contains the call for AspectJ to continue processing: <code>return proceed();</code>.</p>
33 * <P>Refer to {@link AbstractSecurityInterceptor} for details on the workflow.</p>
34 *
35 * @author Ben Alex
36 * @version $Id: AspectJSecurityInterceptor.java 1496 2006-05-23 13:38:33Z benalex $
37 */
38 public class AspectJSecurityInterceptor extends AbstractSecurityInterceptor {
39 //~ Instance fields ================================================================================================
40
41 private MethodDefinitionSource objectDefinitionSource;
42
43 //~ Methods ========================================================================================================
44
45 public MethodDefinitionSource getObjectDefinitionSource() {
46 return this.objectDefinitionSource;
47 }
48
49 public Class getSecureObjectClass() {
50 return JoinPoint.class;
51 }
52
53 /**
54 * This method should be used to enforce security on a <code>JoinPoint</code>.
55 *
56 * @param jp The AspectJ joint point being invoked which requires a security decision
57 * @param advisorProceed the advice-defined anonymous class that implements <code>AspectJCallback</code> containing
58 * a simple <code>return proceed();</code> statement
59 *
60 * @return The returned value from the method invocation
61 */
62 public Object invoke(JoinPoint jp, AspectJCallback advisorProceed) {
63 Object result = null;
64 InterceptorStatusToken token = super.beforeInvocation(jp);
65
66 try {
67 result = advisorProceed.proceedWithObject();
68 } finally {
69 result = super.afterInvocation(token, result);
70 }
71
72 return result;
73 }
74
75 public ObjectDefinitionSource obtainObjectDefinitionSource() {
76 return this.objectDefinitionSource;
77 }
78
79 public void setObjectDefinitionSource(MethodDefinitionSource newSource) {
80 this.objectDefinitionSource = newSource;
81 }
82 }