1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.domain.validation;
17
18 import org.springframework.aop.framework.AopConfigException;
19 import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
20
21 import org.springframework.beans.factory.InitializingBean;
22
23 import org.springframework.util.Assert;
24
25 import java.lang.reflect.Method;
26
27
28 /***
29 * Advisor for the {@link ValidationInterceptor}.
30 *
31 * <p>
32 * Intended to be used with Spring's
33 * <code>DefaultAdvisorAutoProxyCreator</code>.
34 * </p>
35 *
36 * <p>
37 * Registers {@link ValidationInterceptor} for every <code>Method</code>
38 * against a class that directly or through its superclasses implements {@link
39 * #supportsClass} and has a signature match those defined by {@link
40 * #methods}.
41 * </p>
42 *
43 * @author Ben Alex
44 * @version $Id: ValidationAdvisor.java,v 1.4 2005/11/17 00:55:50 benalex Exp $
45 */
46 public class ValidationAdvisor extends StaticMethodMatcherPointcutAdvisor
47 implements InitializingBean {
48
49
50 private Class<? extends Object> supportsClass;
51 private String[] methods = {"create", "update", "createOrUpdate"};
52
53
54
55 public ValidationAdvisor(ValidationInterceptor advice) {
56 super(advice);
57
58 if (advice == null) {
59 throw new AopConfigException(
60 "Cannot construct a BindAndValidateAdvisor using a "
61 + "null BindAndValidateInterceptor");
62 }
63 }
64
65
66
67 public void setMethods(String[] methods) {
68 this.methods = methods;
69 }
70
71 public String[] getMethods() {
72 return methods;
73 }
74
75 public void setSupportsClass(Class<? extends Object> clazz) {
76 this.supportsClass = clazz;
77 }
78
79 public Class getSupportsClass() {
80 return supportsClass;
81 }
82
83 public void afterPropertiesSet() throws Exception {
84 Assert.notNull(supportsClass, "A supportsClass is required");
85 Assert.notNull(methods, "A list of valid methods is required");
86 Assert.notEmpty(methods, "A list of valid methods is required");
87 }
88
89 public boolean matches(Method m, Class targetClass) {
90
91 if (m.getParameterTypes().length == 0) {
92 return false;
93 }
94
95
96 boolean found = false;
97
98 for (int i = 0; i < methods.length; i++) {
99 if (m.getName().equals(methods[i])) {
100 found = true;
101 }
102 }
103
104 if (!found) {
105 return false;
106 }
107
108
109 if (supportsClass.isAssignableFrom(targetClass)) {
110 return true;
111 }
112
113 return false;
114 }
115 }