01: /**
02: *
03: */package org.acegisecurity.config;
04:
05: import org.acegisecurity.context.HttpSessionContextIntegrationFilter;
06: import org.springframework.beans.factory.config.BeanDefinition;
07: import org.springframework.beans.factory.support.BeanDefinitionBuilder;
08: import org.springframework.beans.factory.support.RootBeanDefinition;
09: import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
10: import org.springframework.beans.factory.xml.ParserContext;
11: import org.springframework.core.Conventions;
12: import org.springframework.util.Assert;
13: import org.springframework.util.StringUtils;
14: import org.w3c.dom.Attr;
15: import org.w3c.dom.Element;
16: import org.w3c.dom.NamedNodeMap;
17:
18: /**
19: *
20: * @author vpuri
21: *
22: */
23: public class ContextIntegrationBeanDefinitionParser extends
24: AbstractSingleBeanDefinitionParser {
25:
26: // ~ Static fields/initializers
27: // =====================================================================================
28:
29: private static final String HTTP_SESSION_CONTEXT_INTEGRATION = "session-context-integration";
30:
31: private static final String SESSION_CREATION = "sessionCreation";
32:
33: private static final String IF_REQUIRED = "ifRequired";
34:
35: private static final String ALWAYS = "always";
36:
37: private static final String NEVER = "never";
38:
39: private static final String ALLOW_SESSION_CREATION = "allowSessionCreation";
40:
41: // ~ Methods
42: // ========================================================================================================
43:
44: protected Class getBeanClass(Element element) {
45: return HttpSessionContextIntegrationFilter.class;
46: }
47:
48: protected void doParse(Element element,
49: ParserContext parserContext, BeanDefinitionBuilder builder) {
50:
51: NamedNodeMap attributes = element.getAttributes();
52:
53: for (int x = 0; x < attributes.getLength(); x++) {
54: Attr attribute = (Attr) attributes.item(x);
55: String attributeName = attribute.getLocalName();
56: if (!ID_ATTRIBUTE.equals(attributeName)) {
57: if (attributeName.equals(SESSION_CREATION)) {
58: String sessionCreation = element
59: .getAttribute(SESSION_CREATION);
60: createBeanDefinition(builder, sessionCreation);
61: } else {
62: String propertyName = Conventions
63: .attributeNameToPropertyName(attributeName);
64: Assert
65: .state(
66: StringUtils.hasText(propertyName),
67: "Illegal property name returned from 'extractPropertyName(String)': cannot be null or empty.");
68: builder.addPropertyValue(propertyName, attribute
69: .getValue());
70: }
71: }
72: }
73: }
74:
75: private void createBeanDefinition(BeanDefinitionBuilder builder,
76: String attribute) {
77: if (attribute.equals(IF_REQUIRED)) {
78: builder.addPropertyValue(ALLOW_SESSION_CREATION,
79: Boolean.TRUE);
80: } else if (attribute.equals(ALWAYS)) {
81: builder.addPropertyValue(ALLOW_SESSION_CREATION,
82: Boolean.TRUE);
83: } else if (attribute.equals(NEVER)) {
84: builder.addPropertyValue(ALLOW_SESSION_CREATION,
85: Boolean.FALSE);
86: } else {
87: createBeanDefinitionWithDefaults();
88: }
89: }
90:
91: protected static RootBeanDefinition createBeanDefinitionWithDefaults() {
92: RootBeanDefinition definition = new RootBeanDefinition(
93: HttpSessionContextIntegrationFilter.class);
94: definition.getPropertyValues().addPropertyValue(
95: ALLOW_SESSION_CREATION, Boolean.TRUE);
96: return definition;
97: }
98: }
|