01: /**
02: *
03: */package org.acegisecurity.util;
04:
05: import org.springframework.beans.factory.config.RuntimeBeanNameReference;
06: import org.springframework.beans.factory.config.RuntimeBeanReference;
07: import org.springframework.beans.factory.support.RootBeanDefinition;
08: import org.springframework.beans.factory.xml.ParserContext;
09: import org.springframework.util.StringUtils;
10: import org.w3c.dom.Element;
11:
12: /**
13: * The convenience methods for the parsing of bean definition xml file.
14: *
15: * @author Vishal Puri
16: *
17: */
18: public class BeanDefinitionParserUtils {
19: // ~ Constructor
20: // ================================================================================================
21:
22: /**
23: * Prevents instantiation
24: */
25: private BeanDefinitionParserUtils() {
26: }
27:
28: // ~ Method
29: // ================================================================================================
30:
31: public static void setConstructorArgumentIfAvailable(int index,
32: Element element, String attribute,
33: boolean isRunTimeBeanReference,
34: RootBeanDefinition definition) {
35: String propertyValue = element.getAttribute(attribute);
36: if (StringUtils.hasText(propertyValue)) {
37: if (!isRunTimeBeanReference) {
38: definition.getConstructorArgumentValues()
39: .addIndexedArgumentValue(index, propertyValue);
40: } else {
41: definition.getConstructorArgumentValues()
42: .addIndexedArgumentValue(
43: index,
44: new RuntimeBeanNameReference(
45: propertyValue));
46: }
47: }
48: }
49:
50: /**
51: * <p>
52: * Configure a <code>BeanDefinition</code>with the property value
53: * retrieved from xml attribute. If the attribute is like a standard spring
54: * 'ref' attribute as indicated by 'isRunTimeBeanReference', the property
55: * will be resolved as a reference to the spring bean.
56: * </p>
57: *
58: * @param element The parent element.
59: * @param attribute The child attribute.
60: * @param property The configuration property for the BeanDefinition
61: * @param isRunTimeBeanReference Indicates if the property is like a
62: * standard spring 'ref' attribute.
63: * @param definition The BeanDefinition to configure with the property
64: * provided.
65: * @return boolean To indicate if BeanDefinition was configured with a
66: * property.
67: */
68: public static boolean setPropertyIfAvailable(Element element,
69: String attribute, String property,
70: boolean isRunTimeBeanReference,
71: RootBeanDefinition definition) {
72: String propertyValue = element.getAttribute(attribute);
73: if (StringUtils.hasText(propertyValue)) {
74: if (!isRunTimeBeanReference) {
75: definition.getPropertyValues().addPropertyValue(
76: property, propertyValue);
77: return true;
78: } else {
79: definition.getPropertyValues().addPropertyValue(
80: property,
81: new RuntimeBeanReference(propertyValue));
82: return true;
83: }
84: }
85: return false;
86: }
87:
88: /**
89: * @param parserContext
90: * @param defintion
91: */
92: public static void registerBeanDefinition(
93: ParserContext parserContext, RootBeanDefinition defintion) {
94: parserContext.getRegistry().registerBeanDefinition(
95: parserContext.getReaderContext().generateBeanName(
96: defintion), defintion);
97: }
98: }
|