001: /**
002: *
003: */package org.acegisecurity.config;
004:
005: import java.util.Collections;
006: import java.util.List;
007:
008: import org.acegisecurity.ui.logout.LogoutFilter;
009: import org.acegisecurity.ui.logout.LogoutHandler;
010: import org.acegisecurity.ui.logout.SecurityContextLogoutHandler;
011: import org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices;
012: import org.springframework.beans.BeansException;
013: import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
014: import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
015: import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder;
016: import org.springframework.beans.factory.support.ManagedList;
017: import org.springframework.beans.factory.support.RootBeanDefinition;
018: import org.springframework.core.OrderComparator;
019: import org.springframework.core.Ordered;
020:
021: /**
022: * @author vpuri
023: * @since
024: */
025: public class LogoutHandlerOrderResolver implements
026: BeanFactoryPostProcessor {
027:
028: // ~ Methods
029: // ================================================================================================
030:
031: public void postProcessBeanFactory(
032: ConfigurableListableBeanFactory beanFactory)
033: throws BeansException {
034: // If LogoutFilter does not have setHandlers populated, introspect app
035: // ctx for LogoutHandlers, using Ordered (if present, otherwise assume
036: // Integer.MAX_VALUE)
037: String[] names = beanFactory
038: .getBeanNamesForType(LogoutFilter.class);
039: RootBeanDefinition definition = (RootBeanDefinition) beanFactory
040: .getBeanDefinition(names[0]);
041: ValueHolder holder = getHandlersIfConfigured(beanFactory,
042: definition);
043: if (holder == null) {
044: // intropect the appcontext for registerd LogoutHandler
045: List logoutHandlers = retrieveAllLogoutHandlers(beanFactory);
046: definition.getConstructorArgumentValues()
047: .addIndexedArgumentValue(1, logoutHandlers);
048: }
049: }
050:
051: /**
052: *
053: * @param beanFactory
054: * @param definition
055: * @return
056: */
057: private ValueHolder getHandlersIfConfigured(
058: ConfigurableListableBeanFactory beanFactory,
059: RootBeanDefinition definition) {
060: // there should be only one LogoutFilter
061: return definition.getConstructorArgumentValues()
062: .getArgumentValue(1, null);
063:
064: }
065:
066: /**
067: *
068: * @param beanFactory
069: * @return
070: */
071: private List retrieveAllLogoutHandlers(
072: ConfigurableListableBeanFactory beanFactory) {
073: String[] names = beanFactory
074: .getBeanNamesForType(LogoutHandler.class);
075: ManagedList list = new ManagedList();
076:
077: for (int i = 0, n = names.length; i < n; i++) {
078: RootBeanDefinition definition = (RootBeanDefinition) beanFactory
079: .getBeanDefinition(names[i]);
080:
081: if (definition.hasBeanClass()) {
082: if (Ordered.class.isAssignableFrom(definition
083: .getBeanClass())) {
084: definition.getPropertyValues().addPropertyValue(
085: "order",
086: Integer.valueOf(getOrder(definition
087: .getBeanClass())));
088: } else {
089: definition.getPropertyValues()
090: .addPropertyValue("order",
091: Integer.valueOf(Integer.MAX_VALUE));
092: }
093: }
094: list.add(definition);
095: }
096: Collections.sort(list, new OrderComparator());
097: return list;
098: }
099:
100: private int getOrder(Class clazz) {
101: if (clazz.getName().equals(
102: TokenBasedRememberMeServices.class.getName())) {
103: return 100;
104: }
105: if (clazz.getName().equals(
106: SecurityContextLogoutHandler.class.getName())) {
107: return 200;
108: }
109: return Integer.MAX_VALUE;
110: }
111:
112: }
|