1
|
package eu.dnetlib.springutils.condbean;
|
2
|
|
3
|
import org.apache.commons.logging.Log;
|
4
|
import org.apache.commons.logging.LogFactory;
|
5
|
import org.springframework.beans.factory.config.BeanDefinition;
|
6
|
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
7
|
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
8
|
import org.springframework.beans.factory.xml.BeanDefinitionParser;
|
9
|
import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
|
10
|
import org.springframework.beans.factory.xml.ParserContext;
|
11
|
import org.springframework.core.io.ResourceLoader;
|
12
|
import org.springframework.core.io.support.ResourcePatternResolver;
|
13
|
import org.springframework.core.io.support.ResourcePatternUtils;
|
14
|
import org.springframework.util.xml.DomUtils;
|
15
|
import org.w3c.dom.Element;
|
16
|
|
17
|
import com.google.common.collect.Lists;
|
18
|
|
19
|
import eu.dnetlib.springutils.condbean.parser.CondBeanParser;
|
20
|
import eu.dnetlib.springutils.condbean.parser.RunccExpressionParser;
|
21
|
|
22
|
/**
|
23
|
* Taken from http://robertmaldon.blogspot.com/2007/04/conditionally-defining-spring-beans.html .
|
24
|
*
|
25
|
* modified to fix a problem caused by http://jira.springframework.org/browse/SPR-2955 improved expression parsing
|
26
|
*
|
27
|
* @author marko
|
28
|
*
|
29
|
*/
|
30
|
public class ConditionalBeanDefinitionParser implements BeanDefinitionParser {
|
31
|
|
32
|
/**
|
33
|
* logger.
|
34
|
*/
|
35
|
@SuppressWarnings("unused")
|
36
|
// NOPMD by marko on 11/24/08 5:02 PM
|
37
|
private static final Log log = LogFactory.getLog(ConditionalBeanDefinitionParser.class);
|
38
|
|
39
|
/**
|
40
|
* expression parser.
|
41
|
*/
|
42
|
private transient RunccExpressionParser expressionParser = null;
|
43
|
|
44
|
public void init(ResourceLoader resourceLoader) {
|
45
|
if(expressionParser != null)
|
46
|
return;
|
47
|
|
48
|
final ResourcePatternResolver resolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
|
49
|
|
50
|
ChainPropertyFinder chain = new ChainPropertyFinder();
|
51
|
|
52
|
ResourcePropertyFinder res1 = new ResourcePropertyFinder();
|
53
|
res1.setResource(resolver.getResource("classpath:/eu/dnetlib/cnr-default.properties"));
|
54
|
|
55
|
ResourcePropertyFinder res2 = new ResourcePropertyFinder();
|
56
|
res2.setResource(resolver.getResource("classpath:/eu/dnetlib/cnr-site.properties"));
|
57
|
|
58
|
ResourcePropertyFinder res3 = new ResourcePropertyFinder();
|
59
|
res3.setResource(resolver.getResource("classpath:/cnr-override.properties"));
|
60
|
|
61
|
chain.setFinders(Lists.newArrayList(new SystemPropertiesFinder(), res3, res2, res1));
|
62
|
|
63
|
CondBeanParser condBeanParser = new CondBeanParser();
|
64
|
condBeanParser.setPropertyFinder(chain);
|
65
|
|
66
|
expressionParser = new RunccExpressionParser();
|
67
|
expressionParser.setCondBeanParser(condBeanParser);
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* Parse the "cond" element and check the mandatory "test" attribute. If the system property named by test is null
|
72
|
* or empty (i.e. not defined) then return null, which is the same as not defining the bean.
|
73
|
*
|
74
|
* @param element
|
75
|
* element to parse
|
76
|
* @param parserContext
|
77
|
* spring parser context
|
78
|
* @return registered bean or null.
|
79
|
*/
|
80
|
@Override
|
81
|
public BeanDefinition parse(final Element element, final ParserContext parserContext) {
|
82
|
init(parserContext.getReaderContext().getResourceLoader());
|
83
|
|
84
|
if (DomUtils.nodeNameEquals(element, "cond")) {
|
85
|
final String test = element.getAttribute("test");
|
86
|
if (expressionParser.expressionValue(test)) {
|
87
|
final Element beanElement = DomUtils.getChildElementByTagName(element, "bean");
|
88
|
return parseAndRegisterBean(beanElement, parserContext);
|
89
|
}
|
90
|
}
|
91
|
|
92
|
return null;
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* the real job of registering the child element of this conditional bean is performed here.
|
97
|
*
|
98
|
* @param element
|
99
|
* element
|
100
|
* @param parserContext
|
101
|
* spring parser context
|
102
|
* @return registered bean.
|
103
|
*/
|
104
|
private BeanDefinition parseAndRegisterBean(final Element element, final ParserContext parserContext) {
|
105
|
if (element == null)
|
106
|
throw new IllegalStateException("trying to register a null bean");
|
107
|
|
108
|
final BeanDefinitionParserDelegate delegate = parserContext.getDelegate();
|
109
|
BeanDefinitionHolder holder = delegate.parseBeanDefinitionElement(element);
|
110
|
BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry());
|
111
|
|
112
|
// necessary because of http://jira.springframework.org/browse/SPR-2955
|
113
|
holder = delegate.decorateBeanDefinitionIfRequired(element, holder);
|
114
|
return holder.getBeanDefinition();
|
115
|
}
|
116
|
|
117
|
}
|