View Javadoc

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  public class PropertiesPlaceholderConfigurer extends PlaceholderConfigurer {
17  
18  	private PropertiesLoader loader = new PropertiesLoader();
19  	private PropertiesConverter converter = new PropertiesConverter();
20  	private PropertiesResolver resolver = new PropertiesResolver();
21  	private Properties properties;
22  	private String nullValue;
23  
24  	protected boolean currentBeanIsMe(String currentBean, ConfigurableListableBeanFactory beanFactoryToProcess) {
25  		if (!currentBean.equals(getBeanName())) {
26  			return false;
27  		}
28  		if (!beanFactoryToProcess.equals(getBeanFactory())) {
29  			return false;
30  		}
31  		return true;
32  	}
33  
34  	protected BeanDefinitionVisitor getBeanDefinitionVisitor(StringValueResolver valueResolver) {
35  		EnhancedBeanDefinitionVisitor visitor = new EnhancedBeanDefinitionVisitor();
36  		visitor.setValueResolver(valueResolver);
37  		VisitListener listener = new DefaultVisitListener();
38  		visitor.addListener(listener);
39  		return visitor;
40  	}
41  
42  	protected void processBeans(ConfigurableListableBeanFactory beanFactory, StringValueResolver valueResolver) {
43  		BeanDefinitionVisitor visitor = getBeanDefinitionVisitor(valueResolver);
44  		String[] beanNames = beanFactory.getBeanDefinitionNames();
45  		for (String curName : beanNames) {
46  			// Skip processing our own bean definition
47  			if (currentBeanIsMe(curName, beanFactory)) {
48  				continue;
49  			}
50  			BeanDefinition bd = beanFactory.getBeanDefinition(curName);
51  			try {
52  				visitor.visitBeanDefinition(bd);
53  			} catch (Exception e) {
54  				throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, e.getMessage(), e);
55  			}
56  		}
57  	}
58  
59  	protected StringValueResolver getStringValueResolver() {
60  		ValueRetriever retriever = new PropertiesRetriever(this.properties);
61  		DefaultStringValueResolver dsvr = new DefaultStringValueResolver();
62  		dsvr.setResolver(this.resolver);
63  		dsvr.setNullValue(this.nullValue);
64  		dsvr.setRetriever(retriever);
65  		return dsvr;
66  	}
67  
68  	@Override
69  	protected void processPlaceholders(ConfigurableListableBeanFactory beanFactoryToProcess) {
70  		this.properties = this.loader.loadProperties();
71  		this.converter.convert(this.properties);
72  		this.resolver.resolve(this.properties);
73  
74  		StringValueResolver valueResolver = getStringValueResolver();
75  
76  		processBeans(beanFactoryToProcess, valueResolver);
77  
78  		// New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
79  		beanFactoryToProcess.resolveAliases(valueResolver);
80  
81  		// New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.
82  		beanFactoryToProcess.addEmbeddedValueResolver(valueResolver);
83  
84  	}
85  
86  	public PropertiesLoader getLoader() {
87  		return loader;
88  	}
89  
90  	public void setLoader(PropertiesLoader loader) {
91  		this.loader = loader;
92  	}
93  
94  	public PropertiesConverter getConverter() {
95  		return converter;
96  	}
97  
98  	public void setConverter(PropertiesConverter converter) {
99  		this.converter = converter;
100 	}
101 
102 	public Properties getProperties() {
103 		return properties;
104 	}
105 
106 	public void setProperties(Properties properties) {
107 		this.properties = properties;
108 	}
109 
110 	public PropertiesResolver getResolver() {
111 		return resolver;
112 	}
113 
114 	public void setResolver(PropertiesResolver resolver) {
115 		this.resolver = resolver;
116 	}
117 
118 	public String getNullValue() {
119 		return nullValue;
120 	}
121 
122 	public void setNullValue(String nullValue) {
123 		this.nullValue = nullValue;
124 	}
125 
126 }