View Javadoc

1   package org.kuali.common.util.properties.spring;
2   
3   import java.util.Properties;
4   
5   import org.kuali.common.util.PropertyUtils;
6   import org.kuali.common.util.project.model.Project;
7   import org.kuali.common.util.project.spring.AutowiredProjectConfig;
8   import org.kuali.common.util.properties.DefaultPropertiesService;
9   import org.kuali.common.util.properties.PropertiesService;
10  import org.kuali.common.util.property.ImmutableProperties;
11  import org.kuali.common.util.property.processor.DecryptingProcessor;
12  import org.kuali.common.util.property.processor.OverridingProcessor;
13  import org.kuali.common.util.property.processor.ProcessorsProcessor;
14  import org.kuali.common.util.property.processor.PropertyProcessor;
15  import org.kuali.common.util.property.processor.ResolvingProcessor;
16  import org.kuali.common.util.property.processor.TrimmingProcessor;
17  import org.kuali.common.util.spring.env.EnvironmentService;
18  import org.kuali.common.util.spring.service.SpringServiceConfig;
19  import org.springframework.beans.factory.annotation.Autowired;
20  import org.springframework.context.annotation.Bean;
21  import org.springframework.context.annotation.Configuration;
22  import org.springframework.context.annotation.Import;
23  
24  @Configuration
25  @Import({ SpringServiceConfig.class, AutowiredProjectConfig.class })
26  public class DefaultPropertiesServiceConfig implements PropertiesServiceConfig {
27  
28  	private static final String REMOVE_SYSTEM_PROPERTY_PASSWORD_KEY = "properties.enc.password.remove";
29  
30  	@Autowired
31  	Project project;
32  
33  	@Autowired
34  	EnvironmentService env;
35  
36  	@Override
37  	@Bean
38  	public PropertiesService propertiesService() {
39  
40  		// Get a reference to system + environment properties
41  		Properties global = PropertyUtils.getGlobalProperties();
42  
43  		// Setup a properties object where system properties "win" over project properties
44  		Properties overrides = new ImmutableProperties(PropertyUtils.combine(project.getProperties(), global));
45  
46  		// This property contains the password for decrypting any encrypted property values
47  		String passwordKey = DecryptingProcessor.DEFAULT_PASSWORD_KEY;
48  
49  		// Setup a processor that gets invoked on the properties *after* they have all been loaded
50  		PropertyProcessor processor = getPostProcessor(overrides, passwordKey);
51  
52  		// Setup a service with the overrides and post processor we've configured
53  		PropertiesService service = new DefaultPropertiesService(overrides, processor);
54  
55  		// Now that the service is setup, we can remove the password as a system property (if it has been set there)
56  		if (env.getBoolean(REMOVE_SYSTEM_PROPERTY_PASSWORD_KEY, true)) {
57  			PropertyUtils.removeSystemProperty(passwordKey);
58  		}
59  
60  		// Return the configured service
61  		return service;
62  	}
63  
64  	protected PropertyProcessor getPostProcessor(Properties overrides, String passwordKey) {
65  		PropertyProcessor override = new OverridingProcessor(overrides);
66  		PropertyProcessor decrypt = new DecryptingProcessor(passwordKey);
67  		PropertyProcessor resolve = new ResolvingProcessor();
68  		PropertyProcessor trim = new TrimmingProcessor(passwordKey);
69  		return new ProcessorsProcessor(override, decrypt, resolve, trim);
70  	}
71  
72  }