1 | |
package org.kuali.spring.util; |
2 | |
|
3 | |
import java.util.Properties; |
4 | |
|
5 | |
import org.kuali.spring.util.event.DefaultVisitListener; |
6 | |
import org.kuali.spring.util.event.VisitListener; |
7 | |
import org.springframework.beans.factory.BeanDefinitionStoreException; |
8 | |
import org.springframework.beans.factory.config.BeanDefinition; |
9 | |
import org.springframework.beans.factory.config.BeanDefinitionVisitor; |
10 | |
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
11 | |
import org.springframework.util.StringValueResolver; |
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | 1 | public class PropertiesPlaceholderConfigurer extends PlaceholderConfigurer { |
17 | |
|
18 | 1 | private PropertiesLoader loader = new PropertiesLoader(); |
19 | 1 | private PropertiesConverter converter = new PropertiesConverter(); |
20 | 1 | private PropertiesResolver resolver = new PropertiesResolver(); |
21 | |
private Properties properties; |
22 | |
private String nullValue; |
23 | |
|
24 | |
protected boolean currentBeanIsMe(String currentBean, ConfigurableListableBeanFactory beanFactoryToProcess) { |
25 | 14 | if (!currentBean.equals(getBeanName())) { |
26 | 13 | return false; |
27 | |
} |
28 | 1 | if (!beanFactoryToProcess.equals(getBeanFactory())) { |
29 | 0 | return false; |
30 | |
} |
31 | 1 | return true; |
32 | |
} |
33 | |
|
34 | |
protected BeanDefinitionVisitor getBeanDefinitionVisitor(StringValueResolver valueResolver) { |
35 | 1 | EnhancedBeanDefinitionVisitor visitor = new EnhancedBeanDefinitionVisitor(); |
36 | 1 | visitor.setValueResolver(valueResolver); |
37 | 1 | VisitListener listener = new DefaultVisitListener(); |
38 | 1 | visitor.addListener(listener); |
39 | 1 | return visitor; |
40 | |
} |
41 | |
|
42 | |
protected void processBeans(ConfigurableListableBeanFactory beanFactory, StringValueResolver valueResolver) { |
43 | 1 | BeanDefinitionVisitor visitor = getBeanDefinitionVisitor(valueResolver); |
44 | 1 | String[] beanNames = beanFactory.getBeanDefinitionNames(); |
45 | 15 | for (String curName : beanNames) { |
46 | |
|
47 | 14 | if (currentBeanIsMe(curName, beanFactory)) { |
48 | 1 | continue; |
49 | |
} |
50 | 13 | BeanDefinition bd = beanFactory.getBeanDefinition(curName); |
51 | |
try { |
52 | 13 | visitor.visitBeanDefinition(bd); |
53 | 0 | } catch (Exception e) { |
54 | 0 | throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, e.getMessage(), e); |
55 | 13 | } |
56 | |
} |
57 | 1 | } |
58 | |
|
59 | |
protected StringValueResolver getStringValueResolver() { |
60 | 1 | ValueRetriever retriever = new PropertiesRetriever(this.properties); |
61 | 1 | DefaultStringValueResolver dsvr = new DefaultStringValueResolver(); |
62 | 1 | dsvr.setResolver(this.resolver); |
63 | 1 | dsvr.setNullValue(this.nullValue); |
64 | 1 | dsvr.setRetriever(retriever); |
65 | 1 | return dsvr; |
66 | |
} |
67 | |
|
68 | |
@Override |
69 | |
protected void processPlaceholders(ConfigurableListableBeanFactory beanFactoryToProcess) { |
70 | 1 | this.properties = this.loader.loadProperties(); |
71 | 1 | this.converter.convert(this.properties); |
72 | 1 | this.resolver.resolve(this.properties); |
73 | |
|
74 | 1 | StringValueResolver valueResolver = getStringValueResolver(); |
75 | |
|
76 | 1 | processBeans(beanFactoryToProcess, valueResolver); |
77 | |
|
78 | |
|
79 | 1 | beanFactoryToProcess.resolveAliases(valueResolver); |
80 | |
|
81 | |
|
82 | 1 | beanFactoryToProcess.addEmbeddedValueResolver(valueResolver); |
83 | |
|
84 | 1 | } |
85 | |
|
86 | |
public PropertiesLoader getLoader() { |
87 | 0 | return loader; |
88 | |
} |
89 | |
|
90 | |
public void setLoader(PropertiesLoader loader) { |
91 | 1 | this.loader = loader; |
92 | 1 | } |
93 | |
|
94 | |
public PropertiesConverter getConverter() { |
95 | 0 | return converter; |
96 | |
} |
97 | |
|
98 | |
public void setConverter(PropertiesConverter converter) { |
99 | 1 | this.converter = converter; |
100 | 1 | } |
101 | |
|
102 | |
public Properties getProperties() { |
103 | 0 | return properties; |
104 | |
} |
105 | |
|
106 | |
public void setProperties(Properties properties) { |
107 | 0 | this.properties = properties; |
108 | 0 | } |
109 | |
|
110 | |
public PropertiesResolver getResolver() { |
111 | 0 | return resolver; |
112 | |
} |
113 | |
|
114 | |
public void setResolver(PropertiesResolver resolver) { |
115 | 1 | this.resolver = resolver; |
116 | 1 | } |
117 | |
|
118 | |
public String getNullValue() { |
119 | 0 | return nullValue; |
120 | |
} |
121 | |
|
122 | |
public void setNullValue(String nullValue) { |
123 | 0 | this.nullValue = nullValue; |
124 | 0 | } |
125 | |
|
126 | |
} |