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.CollectionUtils;
22 import org.kuali.common.util.PropertyUtils;
23 import org.kuali.common.util.property.PropertyLoadContext;
24 import org.kuali.common.util.property.PropertyStoreContext;
25 import org.kuali.common.util.property.processor.PropertyProcessor;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class DefaultPropertyService implements PropertyService {
30
31 private static final Logger logger = LoggerFactory.getLogger(DefaultPropertyService.class);
32
33 @Override
34 public Properties load(PropertyLoadContext context) {
35 Properties properties = loadProperties(context);
36 logger.info("Working with " + properties.size() + " properties total.");
37 context.initialize(properties);
38 List<PropertyProcessor> processors = CollectionUtils.toEmptyList(context.getProcessors());
39 logger.info("Processing " + properties.size() + " properties using " + processors.size() + " processors.");
40 PropertyUtils.process(properties, context.getProcessors());
41 logger.info("Returning " + properties.size() + " properties.");
42 if (logger.isDebugEnabled()) {
43 logger.debug(PropertyUtils.toString(properties));
44 }
45 return properties;
46 }
47
48 @Override
49 public void store(PropertyStoreContext context, Properties properties) {
50 Properties duplicate = PropertyUtils.duplicate(properties);
51 context.initialize(duplicate);
52 PropertyUtils.process(duplicate, context.getProcessors());
53 PropertyUtils.store(duplicate, context.getFile(), context.getEncoding(), context.getComment());
54 }
55
56 protected Properties loadProperties(PropertyLoadContext context) {
57 Properties properties = context.init();
58 int initialSize = properties.size();
59 List<String> locations = CollectionUtils.toEmptyList(context.getLocations());
60 logger.info("Examining " + locations.size() + " property locations.");
61 int count = 0;
62 for (String location : locations) {
63 String resolvedAndValidatedLocation = context.getLocation(location, properties);
64 if (resolvedAndValidatedLocation != null) {
65 Properties newProperties = PropertyUtils.load(resolvedAndValidatedLocation, context.getEncoding());
66 properties.putAll(newProperties);
67 count++;
68 }
69 }
70 int newSize = properties.size();
71 int skipped = locations.size() - count;
72 logger.info("Loaded " + (newSize - initialSize) + " properties from " + count + " locations. Skipped " + skipped + " locations.");
73 return properties;
74 }
75 }