01: /**
02: *
03: */package org.acegisecurity.config;
04:
05: import org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices;
06: import org.springframework.beans.factory.config.RuntimeBeanReference;
07: import org.springframework.beans.factory.support.AbstractBeanDefinition;
08: import org.springframework.beans.factory.support.RootBeanDefinition;
09: import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
10: import org.springframework.beans.factory.xml.BeanDefinitionParser;
11: import org.springframework.beans.factory.xml.ParserContext;
12: import org.springframework.util.Assert;
13: import org.springframework.util.StringUtils;
14: import org.w3c.dom.Element;
15:
16: /**
17: * Parses
18: * @author vpuri
19: *
20: */
21: public class RememberMeServicesBeanDefinitionParser extends
22: AbstractBeanDefinitionParser implements BeanDefinitionParser {
23:
24: private static final String PRINCIPAL_REPOSITORY_BEAN_REF = "principalRepositoryBeanRef";
25:
26: private static final String USER_DETAILS_SERVICE_PROPERTY = "userDetailsService";
27:
28: /*
29: * key is optional; if unspecified, pick a rnd int and use for all
30: * unspecified key properties for acegi beans
31: */
32: private static final String KEY = "key";
33:
34: protected AbstractBeanDefinition parseInternal(Element element,
35: ParserContext parserContext) {
36: RootBeanDefinition rememberMeServicesBeanDef = createBeanDefinition(
37: element, parserContext);
38: return rememberMeServicesBeanDef;
39: }
40:
41: private RootBeanDefinition createBeanDefinition(Element element,
42: ParserContext parserContext) {
43: Assert.notNull(parserContext, "ParserContext must not be null");
44:
45: RootBeanDefinition rememberMeServicesBeanDef = new RootBeanDefinition(
46: TokenBasedRememberMeServices.class);
47:
48: String keyValue = "";
49: String rememberMeServicesRef = "";
50:
51: if (element != null) {
52: keyValue = element.getAttribute(KEY);
53:
54: if (StringUtils.hasLength(keyValue)) {
55: rememberMeServicesBeanDef.getPropertyValues()
56: .addPropertyValue(KEY, keyValue);
57: } else {
58: /*
59: * TODO: pick a rnd int and apply it whenver required in
60: * applicationcontext
61: */
62:
63: }
64:
65: // check if rememberMeServicesBeanRef is defined and if it's
66: // specified
67: // use its referred bean
68: rememberMeServicesRef = element
69: .getAttribute(PRINCIPAL_REPOSITORY_BEAN_REF);
70: if (StringUtils.hasLength(rememberMeServicesRef)) {
71: rememberMeServicesBeanDef.getPropertyValues()
72: .addPropertyValue(
73: USER_DETAILS_SERVICE_PROPERTY,
74: new RuntimeBeanReference(
75: rememberMeServicesRef));
76: }
77: }
78:
79: return rememberMeServicesBeanDef;
80: }
81:
82: protected static RootBeanDefinition createAndRegisterBeanDefintionWithDefaults(
83: ParserContext parserContext) {
84: RootBeanDefinition beanDefinition = new RootBeanDefinition(
85: TokenBasedRememberMeServices.class);
86: beanDefinition.getPropertyValues().addPropertyValue(KEY, "key");
87: parserContext.getReaderContext().registerWithGeneratedName(
88: beanDefinition);
89: return beanDefinition;
90: }
91:
92: }
|