1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.env.beans;
18
19 import java.io.IOException;
20 import java.util.Properties;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.springframework.beans.BeansException;
25 import org.springframework.beans.factory.BeanDefinitionStoreException;
26 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
27 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
28 import org.springframework.beans.factory.config.PropertyOverrideConfigurer;
29 import org.springframework.context.ApplicationContext;
30 import org.springframework.context.ApplicationContextAware;
31 import org.springframework.core.io.Resource;
32
33 import ch.elca.el4j.core.context.ModuleApplicationContext;
34 import ch.elca.el4j.env.xml.EnvXml;
35 import ch.elca.el4j.services.monitoring.notification.CoreNotificationHelper;
36 import ch.elca.el4j.util.encryption.AbstractPropertyEncryptor;
37 import ch.elca.el4j.util.encryption.EncryptionException;
38 import ch.elca.el4j.util.encryption.PasswordSource;
39 import ch.elca.el4j.util.env.PropertyEncryptionUtil;
40
41
42
43
44
45
46
47
48
49 public class EnvPropertyOverrideConfigurer extends PropertyOverrideConfigurer
50 implements ApplicationContextAware {
51
52
53
54
55 public static final String ENV_BEAN_PROPERTY_PROPERTIES_LOCATION
56 = "classpath:env-bean-property.properties";
57
58
59
60
61
62 protected static final Logger s_logger
63 = LoggerFactory.getLogger(ModuleApplicationContext.EL4J_DEBUGGING_LOGGER);
64
65
66
67
68 protected ApplicationContext m_applicationContext;
69
70
71
72
73 protected boolean m_ignoreBeanNameNotFound = false;
74
75
76
77
78
79
80 protected PropertyEncryptionUtil m_util = new PropertyEncryptionUtil();
81
82
83
84
85 private AbstractPropertyEncryptor m_cryptor;
86
87
88
89
90 private String m_cryptorFile = null;
91
92
93
94
95 @Override
96 public void postProcessBeanFactory(
97 ConfigurableListableBeanFactory beanFactory) throws BeansException {
98
99
100 boolean envXmlFound = false;
101 EnvXml envXmlConfigLoader;
102 if (m_applicationContext instanceof ModuleApplicationContext) {
103 ModuleApplicationContext mac = (ModuleApplicationContext) m_applicationContext;
104 envXmlConfigLoader = new EnvXml(m_applicationContext, mac.isMostSpecificResourceLast());
105 } else {
106 envXmlConfigLoader = new EnvXml();
107 }
108 if (envXmlConfigLoader.hasValidConfigurations()) {
109 super.setProperties((Properties) envXmlConfigLoader.getGroupConfiguration(EnvXml.ENV_GROUP_BEAN_OVERRIDES));
110 super.setLocalOverride(true);
111 envXmlFound = true;
112 }
113
114
115 Resource envBeanPropertyLocation = m_applicationContext
116 .getResource(ENV_BEAN_PROPERTY_PROPERTIES_LOCATION);
117
118 String envBeanPropertyLocationUrl = "no url";
119 if (envBeanPropertyLocation.exists()) {
120 try {
121 envBeanPropertyLocationUrl = envBeanPropertyLocation.getURL()
122 .toString();
123 } catch (IOException e) {
124 envBeanPropertyLocationUrl = "unknown url";
125 }
126 }
127
128 if (!envXmlFound) {
129 if (envBeanPropertyLocation.exists()) {
130 s_logger.debug("The used env bean property properties file is '"
131 + envBeanPropertyLocationUrl + "'.");
132 super.setLocation(envBeanPropertyLocation);
133 } else {
134 s_logger.warn("No env bean property properties file could be found. The"
135 + " correct location for this file is '"
136 + ENV_BEAN_PROPERTY_PROPERTIES_LOCATION + "'.");
137 }
138 }
139
140 super.postProcessBeanFactory(beanFactory);
141 }
142
143
144 @Override
145 protected void applyPropertyValue(ConfigurableListableBeanFactory factory,
146 String beanName, String property, String value) {
147 try {
148 super.applyPropertyValue(factory, beanName, property, value);
149 } catch (NoSuchBeanDefinitionException e) {
150 if (m_ignoreBeanNameNotFound) {
151 s_logger
152 .warn("No bean with name '" + beanName
153 + "' found. Therefore "
154 + "no properties could be applied.");
155 } else {
156 s_logger
157 .error("No bean with name '" + beanName
158 + "' found. Therefore "
159 + "no properties could be applied.");
160 throw e;
161 }
162 }
163 }
164
165
166
167
168
169
170 @Override
171 public void setLocation(Resource location) {
172 CoreNotificationHelper.notifyMisconfiguration(
173 "It is not allowed to set the location manually!");
174 }
175
176
177
178
179
180
181 @Override
182 public void setLocations(Resource[] locations) {
183 CoreNotificationHelper.notifyMisconfiguration(
184 "It is not allowed to set the locations manually!");
185 }
186
187
188
189
190 public void setApplicationContext(ApplicationContext applicationContext)
191 throws BeansException {
192 m_applicationContext = applicationContext;
193 }
194
195
196
197
198 public boolean isIgnoreBeanNameNotFound() {
199 return m_ignoreBeanNameNotFound;
200 }
201
202
203
204
205
206 public void setIgnoreBeanNameNotFound(boolean ignoreBeanNameNotFound) {
207 this.m_ignoreBeanNameNotFound = ignoreBeanNameNotFound;
208 }
209
210
211
212
213
214 public void setNoEncryption() {
215 m_util.deactivate();
216 }
217
218
219
220
221
222
223 public void setPasswordSource(PasswordSource source) {
224 m_util.setSource(source);
225 }
226
227
228
229
230 public void setCryptorFile(String file) {
231 m_cryptorFile = file;
232 }
233
234
235
236
237
238
239
240
241
242
243 protected String convertPropertyValue(String originalValue) {
244 String value = originalValue;
245
246 if (!m_util.isInited()) {
247 if (m_cryptorFile != null) {
248 m_util.init(m_applicationContext, m_cryptorFile);
249 } else {
250 m_util.init(m_applicationContext);
251 }
252 if (m_util.isActive()) {
253 m_cryptor = m_util.getCryptor();
254 }
255 }
256 if (m_util.isActive()) {
257 try {
258 value = m_cryptor.processString(originalValue);
259 } catch (EncryptionException e) {
260 throw new BeanDefinitionStoreException(
261 "Error during decryption.");
262 }
263 }
264
265 return super.convertPropertyValue(value);
266 }
267 }