1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.service;
17
18 import java.util.List;
19 import java.util.Properties;
20
21 import org.kuali.common.util.LocationUtils;
22 import org.kuali.common.util.ModeUtils;
23 import org.kuali.common.util.PropertyUtils;
24 import org.kuali.common.util.property.Constants;
25 import org.kuali.common.util.property.PropertyLoadContext;
26 import org.kuali.common.util.property.PropertyStoreContext;
27 import org.kuali.common.util.property.processor.PropertyProcessor;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.util.PropertyPlaceholderHelper;
31
32 public class DefaultPropertyService implements PropertyService {
33
34 private static final Logger logger = LoggerFactory.getLogger(DefaultPropertyService.class);
35
36 @Override
37 public Properties load(PropertyLoadContext context) {
38 Properties properties = loadProperties(context);
39 context.initialize(properties);
40 process(properties, context.getProcessors());
41 return properties;
42 }
43
44 @Override
45 public void store(PropertyStoreContext context, Properties properties) {
46 Properties duplicate = PropertyUtils.duplicate(properties);
47 context.initialize(duplicate);
48 process(duplicate, context.getProcessors());
49 PropertyUtils.store(duplicate, context.getFile(), context.getEncoding(), context.getComment());
50 }
51
52 protected void process(Properties properties, List<PropertyProcessor> processors) {
53 if (processors == null) {
54 return;
55 }
56 for (PropertyProcessor processor : processors) {
57 processor.process(properties);
58 }
59 }
60
61 protected Properties loadProperties(PropertyLoadContext context) {
62 PropertyPlaceholderHelper helper = Constants.DEFAULT_PROPERTY_PLACEHOLDER_HELPER;
63 context.initializeLoadProcessors();
64 Properties properties = new Properties();
65 for (String location : context.getLocations()) {
66 Properties duplicate = PropertyUtils.duplicate(properties);
67 process(duplicate, context.getLoadProcessors());
68 String resolvedLocation = helper.replacePlaceholders(location, duplicate);
69 if (!location.equals(resolvedLocation)) {
70 logger.debug("Resolved location [{}] -> [{}]", location, resolvedLocation);
71 }
72 if (LocationUtils.exists(resolvedLocation)) {
73 Properties newProperties = PropertyUtils.load(resolvedLocation, context.getEncoding());
74 properties.putAll(newProperties);
75 } else {
76 ModeUtils.validate(context.getMissingLocationsMode(), "Skipping non-existent location - [{}]", resolvedLocation, "Could not locate [" + resolvedLocation + "]");
77 }
78 }
79 return properties;
80 }
81 }