1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.vote;
17
18 import java.util.Iterator;
19 import java.util.List;
20
21 import org.acegisecurity.AccessDecisionManager;
22 import org.acegisecurity.AcegiMessageSource;
23 import org.acegisecurity.ConfigAttribute;
24 import org.springframework.beans.factory.InitializingBean;
25 import org.springframework.context.MessageSource;
26 import org.springframework.context.MessageSourceAware;
27 import org.springframework.context.support.MessageSourceAccessor;
28 import org.springframework.util.Assert;
29
30
31 /***
32 * Abstract implementation of {@link AccessDecisionManager}.
33 *
34 * <p>
35 * Handles configuration of a bean context defined list of {@link
36 * AccessDecisionVoter}s and the access control behaviour if all voters
37 * abstain from voting (defaults to deny access).
38 * </p>
39 */
40 public abstract class AbstractAccessDecisionManager
41 implements AccessDecisionManager, InitializingBean, MessageSourceAware {
42
43
44 private List decisionVoters;
45 protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor();
46 private boolean allowIfAllAbstainDecisions = false;
47
48
49
50 public void afterPropertiesSet() throws Exception {
51 checkIfValidList(this.decisionVoters);
52 Assert.notNull(this.messages, "A message source must be set");
53 }
54
55 private void checkIfValidList(List listToCheck) {
56 if ((listToCheck == null) || (listToCheck.size() == 0)) {
57 throw new IllegalArgumentException(
58 "A list of AccessDecisionVoters is required");
59 }
60 }
61
62 public List getDecisionVoters() {
63 return this.decisionVoters;
64 }
65
66 public boolean isAllowIfAllAbstainDecisions() {
67 return allowIfAllAbstainDecisions;
68 }
69
70 public void setAllowIfAllAbstainDecisions(
71 boolean allowIfAllAbstainDecisions) {
72 this.allowIfAllAbstainDecisions = allowIfAllAbstainDecisions;
73 }
74
75 public void setDecisionVoters(List newList) {
76 checkIfValidList(newList);
77
78 Iterator iter = newList.iterator();
79
80 while (iter.hasNext()) {
81 Object currentObject = null;
82
83 try {
84 currentObject = iter.next();
85
86 AccessDecisionVoter attemptToCast = (AccessDecisionVoter) currentObject;
87 } catch (ClassCastException cce) {
88 throw new IllegalArgumentException("AccessDecisionVoter "
89 + currentObject.getClass().getName()
90 + " must implement AccessDecisionVoter");
91 }
92 }
93
94 this.decisionVoters = newList;
95 }
96
97 public void setMessageSource(MessageSource messageSource) {
98 this.messages = new MessageSourceAccessor(messageSource);
99 }
100
101 public boolean supports(ConfigAttribute attribute) {
102 Iterator iter = this.decisionVoters.iterator();
103
104 while (iter.hasNext()) {
105 AccessDecisionVoter voter = (AccessDecisionVoter) iter.next();
106
107 if (voter.supports(attribute)) {
108 return true;
109 }
110 }
111
112 return false;
113 }
114
115 /***
116 * Iterates through all <code>AccessDecisionVoter</code>s and ensures each
117 * can support the presented class.
118 *
119 * <p>
120 * If one or more voters cannot support the presented class,
121 * <code>false</code> is returned.
122 * </p>
123 *
124 * @param clazz DOCUMENT ME!
125 *
126 * @return DOCUMENT ME!
127 */
128 public boolean supports(Class clazz) {
129 Iterator iter = this.decisionVoters.iterator();
130
131 while (iter.hasNext()) {
132 AccessDecisionVoter voter = (AccessDecisionVoter) iter.next();
133
134 if (!voter.supports(clazz)) {
135 return false;
136 }
137 }
138
139 return true;
140 }
141 }