| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.core.config.spring; |
| 17 | |
|
| 18 | |
import java.io.IOException; |
| 19 | |
import java.util.HashSet; |
| 20 | |
import java.util.Properties; |
| 21 | |
|
| 22 | |
import org.apache.log4j.Logger; |
| 23 | |
import org.kuali.rice.core.config.Config; |
| 24 | |
import org.kuali.rice.core.config.ConfigContext; |
| 25 | |
import org.kuali.rice.core.config.ConfigLogger; |
| 26 | |
import org.springframework.beans.BeansException; |
| 27 | |
import org.springframework.beans.factory.BeanDefinitionStoreException; |
| 28 | |
import org.springframework.beans.factory.BeanFactory; |
| 29 | |
import org.springframework.beans.factory.config.BeanDefinition; |
| 30 | |
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 31 | |
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; |
| 32 | |
import org.springframework.util.StringValueResolver; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | 0 | public class RiceConfigPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { |
| 51 | |
|
| 52 | 0 | protected final org.apache.log4j.Logger log = Logger.getLogger(RiceConfigPropertyPlaceholderConfigurer.class); |
| 53 | |
|
| 54 | |
|
| 55 | |
private String beanName; |
| 56 | |
private BeanFactory beanFactory; |
| 57 | |
|
| 58 | |
@Override |
| 59 | |
protected void loadProperties(Properties props) throws IOException { |
| 60 | |
|
| 61 | 0 | super.loadProperties(props); |
| 62 | |
|
| 63 | 0 | Config config = ConfigContext.getCurrentContextConfig(); |
| 64 | 0 | if (config != null) { |
| 65 | 0 | log.debug("Replacing parameters in Spring using config:\r\n" + config); |
| 66 | 0 | ConfigLogger.logConfig(config); |
| 67 | 0 | props.putAll(config.getProperties()); |
| 68 | |
} |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
@Override |
| 72 | |
public void setBeanName(String beanName) { |
| 73 | 0 | this.beanName = beanName; |
| 74 | 0 | super.setBeanName(beanName); |
| 75 | 0 | } |
| 76 | |
|
| 77 | |
@Override |
| 78 | |
public void setBeanFactory(BeanFactory beanFactory) { |
| 79 | 0 | this.beanFactory = beanFactory; |
| 80 | 0 | super.setBeanFactory(beanFactory); |
| 81 | 0 | } |
| 82 | |
|
| 83 | |
|
| 84 | |
private String nullValue; |
| 85 | |
|
| 86 | |
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { |
| 87 | |
|
| 88 | 0 | StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver(props); |
| 89 | 0 | RiceConfigBeanDefinitionVisitor visitor = new RiceConfigBeanDefinitionVisitor(valueResolver); |
| 90 | |
|
| 91 | 0 | String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames(); |
| 92 | 0 | for (int i = 0; i < beanNames.length; i++) { |
| 93 | |
|
| 94 | |
|
| 95 | 0 | if (!(beanNames[i].equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) { |
| 96 | 0 | BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanNames[i]); |
| 97 | |
try { |
| 98 | 0 | visitor.visitBeanDefinition(bd); |
| 99 | 0 | } catch (BeanDefinitionStoreException ex) { |
| 100 | 0 | throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i], ex.getMessage()); |
| 101 | 0 | } |
| 102 | |
} |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | 0 | beanFactoryToProcess.resolveAliases(valueResolver); |
| 107 | 0 | } |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | 0 | public class PlaceholderResolvingStringValueResolver implements StringValueResolver { |
| 114 | |
|
| 115 | |
private final Properties props; |
| 116 | |
|
| 117 | 0 | public PlaceholderResolvingStringValueResolver(Properties props) { |
| 118 | 0 | this.props = props; |
| 119 | 0 | } |
| 120 | |
|
| 121 | |
public String resolveStringValue(String strVal) throws BeansException { |
| 122 | 0 | String value = parseStringValue(strVal, this.props, new HashSet<String>()); |
| 123 | 0 | return (value.equals(nullValue) ? null : value); |
| 124 | |
} |
| 125 | |
|
| 126 | |
public Properties resolvePropertiesValue(String strVal) { |
| 127 | 0 | Properties prefixedProps = new Properties(); |
| 128 | |
|
| 129 | 0 | for (Object key : props.keySet()) { |
| 130 | 0 | String keyStr = (String) key; |
| 131 | 0 | if (keyStr.startsWith(strVal)) { |
| 132 | 0 | String newKeyStr = keyStr.substring(strVal.length()); |
| 133 | 0 | prefixedProps.put(newKeyStr, resolveStringValue((String) props.get(key))); |
| 134 | |
} |
| 135 | 0 | } |
| 136 | |
|
| 137 | 0 | return prefixedProps; |
| 138 | |
} |
| 139 | |
|
| 140 | |
} |
| 141 | |
|
| 142 | |
@Override |
| 143 | |
public void setNullValue(String nullValue) { |
| 144 | 0 | this.nullValue = nullValue; |
| 145 | 0 | super.setNullValue(nullValue); |
| 146 | 0 | } |
| 147 | |
|
| 148 | |
} |