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;
17
18 import org.acegisecurity.ConfigAttributeDefinition;
19
20 import org.aopalliance.intercept.MethodInvocation;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import org.aspectj.lang.JoinPoint;
26 import org.aspectj.lang.reflect.CodeSignature;
27
28 import org.springframework.util.Assert;
29
30 import java.lang.reflect.Method;
31
32
33 /**
34 * Abstract implementation of <Code>MethodDefinitionSource</code>.
35 *
36 * @author Ben Alex
37 * @version $Id: AbstractMethodDefinitionSource.java 1496 2006-05-23 13:38:33Z benalex $
38 */
39 public abstract class AbstractMethodDefinitionSource implements MethodDefinitionSource {
40 //~ Static fields/initializers =====================================================================================
41
42 private static final Log logger = LogFactory.getLog(AbstractMethodDefinitionSource.class);
43
44 //~ Methods ========================================================================================================
45
46 public ConfigAttributeDefinition getAttributes(Object object)
47 throws IllegalArgumentException {
48 Assert.notNull(object, "Object cannot be null");
49
50 if (object instanceof MethodInvocation) {
51 return this.lookupAttributes(((MethodInvocation) object).getMethod());
52 }
53
54 if (object instanceof JoinPoint) {
55 JoinPoint jp = (JoinPoint) object;
56 Class targetClazz = jp.getTarget().getClass();
57 String targetMethodName = jp.getStaticPart().getSignature().getName();
58 Class[] types = ((CodeSignature) jp.getStaticPart().getSignature()).getParameterTypes();
59
60 if (logger.isDebugEnabled()) {
61 logger.debug("Target Class: " + targetClazz);
62 logger.debug("Target Method Name: " + targetMethodName);
63
64 for (int i = 0; i < types.length; i++) {
65 if (logger.isDebugEnabled()) {
66 logger.debug("Target Method Arg #" + i + ": " + types[i]);
67 }
68 }
69 }
70
71 try {
72 return this.lookupAttributes(targetClazz.getMethod(targetMethodName, types));
73 } catch (NoSuchMethodException nsme) {
74 throw new IllegalArgumentException("Could not obtain target method from JoinPoint: " + jp);
75 }
76 }
77
78 throw new IllegalArgumentException("Object must be a MethodInvocation or JoinPoint");
79 }
80
81 /**
82 * Performs the actual lookup of the relevant <code>ConfigAttributeDefinition</code> for the specified
83 * <code>Method</code> which is subject of the method invocation.<P>Provided so subclasses need only to
84 * provide one basic method to properly interface with the <code>MethodDefinitionSource</code>.</p>
85 * <p>Returns <code>null</code> if there are no matching attributes for the method.</p>
86 *
87 * @param method the method being invoked for which configuration attributes should be looked up
88 *
89 * @return the <code>ConfigAttributeDefinition</code> that applies to the specified <code>Method</code>
90 */
91 protected abstract ConfigAttributeDefinition lookupAttributes(Method method);
92
93 public boolean supports(Class clazz) {
94 return (MethodInvocation.class.isAssignableFrom(clazz) || JoinPoint.class.isAssignableFrom(clazz));
95 }
96 }