Fix broken test caused by null application context in AbtractAccessDecisionManager when auto-detection of voters is called.

This commit is contained in:
Luke Taylor 2007-07-24 16:48:49 +00:00
parent c5cc42e16c
commit aea1148ffb
1 changed files with 107 additions and 118 deletions

View File

@ -38,7 +38,7 @@ import org.springframework.util.Assert;
/**
* Abstract implementation of {@link AccessDecisionManager}.
* <p>
* <p/>
* Handles configuration of a bean context defined list of
* {@link AccessDecisionVoter}s and the access control behaviour if all voters
* abstain from voting (defaults to deny access).
@ -55,15 +55,13 @@ public abstract class AbstractAccessDecisionManager implements AccessDecisionMan
private boolean allowIfAllAbstainDecisions = false;
private boolean isSetDecisionVotersInvoked = false;
private ApplicationContext applicationContext;
// ~ Methods
// ========================================================================================================
public void afterPropertiesSet() throws Exception {
if (!isSetDecisionVotersInvoked) {
if (decisionVoters == null || decisionVoters.isEmpty()) {
autoDetectVoters();
}
Assert.notEmpty(this.decisionVoters, "A list of AccessDecisionVoters is required");
@ -71,8 +69,10 @@ public abstract class AbstractAccessDecisionManager implements AccessDecisionMan
}
private void autoDetectVoters() {
Assert.notNull(applicationContext, "Auto-detection of voters requires an application context");
Map map = this.applicationContext.getBeansOfType(AccessDecisionVoter.class);
List list = new ArrayList();
for (Iterator it = map.values().iterator(); it.hasNext();) {
list.add((it.next()));
}
@ -100,24 +100,15 @@ public abstract class AbstractAccessDecisionManager implements AccessDecisionMan
}
public void setDecisionVoters(List newList) {
isSetDecisionVotersInvoked = true;
Assert.notEmpty(newList);
Iterator iter = newList.iterator();
while (iter.hasNext()) {
Object currentObject = null;
try {
currentObject = iter.next();
AccessDecisionVoter attemptToCast = (AccessDecisionVoter) currentObject;
}
catch (ClassCastException cce) {
throw new IllegalArgumentException("AccessDecisionVoter " + currentObject.getClass().getName()
Object currentObject = iter.next();
Assert.isInstanceOf(AccessDecisionVoter.class, currentObject, "AccessDecisionVoter " + currentObject.getClass().getName()
+ " must implement AccessDecisionVoter");
}
}
this.decisionVoters = newList;
}
@ -143,13 +134,12 @@ public abstract class AbstractAccessDecisionManager implements AccessDecisionMan
/**
* Iterates through all <code>AccessDecisionVoter</code>s and ensures
* each can support the presented class.
* <p>
* <p/>
* If one or more voters cannot support the presented class,
* <code>false</code> is returned.
* </p>
*
* @param clazz DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
public boolean supports(Class clazz) {
@ -169,5 +159,4 @@ public abstract class AbstractAccessDecisionManager implements AccessDecisionMan
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}