1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.xml.ingest;
17
18 import java.io.IOException;
19 import java.util.Properties;
20 import java.util.SortedSet;
21
22 import javax.servlet.ServletContext;
23
24 import org.kuali.common.util.PropertyUtils;
25 import org.kuali.common.util.Str;
26 import org.kuali.common.util.log.LoggerUtils;
27 import org.kuali.common.util.property.ImmutableProperties;
28 import org.kuali.rice.core.api.config.property.Config;
29 import org.kuali.rice.core.api.config.property.ConfigContext;
30 import org.kuali.rice.core.impl.config.property.ConfigLogger;
31 import org.kuali.rice.core.impl.config.property.JAXBConfigImpl;
32 import org.kuali.rice.core.web.util.PropertySources;
33 import org.slf4j.Logger;
34
35 import com.google.common.base.Preconditions;
36 import com.google.common.collect.Sets;
37
38
39
40
41
42
43 public class RiceConfigUtils {
44
45 private static final Logger logger = LoggerUtils.make();
46
47 public static Config getRootConfig(Properties loaded, String location, ServletContext servletContext) {
48
49 Config config = ConfigContext.getCurrentContextConfig();
50 Preconditions.checkNotNull(config, "'config' cannot be null");
51 Properties listenerProperties = getProperties(config);
52
53
54 JAXBConfigImpl parsed = parseConfig(location, listenerProperties);
55
56
57 addAndOverride(loaded, parsed.getRawProperties());
58
59
60
61 Properties servlet = PropertySources.convert(servletContext);
62 Properties global = PropertyUtils.getGlobalProperties(servlet);
63 addAndOverride(loaded, global);
64 logger.info("Using {} distinct properties", loaded.size());
65
66
67 return new JAXBConfigImpl(loaded);
68
69 }
70
71
72
73
74 public static void putProperties(Config config, Properties properties) {
75 SortedSet<String> keys = Sets.newTreeSet(properties.stringPropertyNames());
76 for (String key : keys) {
77 config.putProperty(key, properties.getProperty(key));
78 }
79 }
80
81
82
83
84 public static JAXBConfigImpl parseConfig(String location) {
85 return parseConfig(location, ImmutableProperties.of());
86 }
87
88
89
90
91 public static JAXBConfigImpl parseConfig(String location, Properties properties) {
92 try {
93 JAXBConfigImpl config = new JAXBConfigImpl(location, properties);
94 config.parseConfig();
95 return config;
96 } catch (IOException e) {
97 throw new IllegalStateException("Unexpected error parsing config", e);
98 }
99 }
100
101
102
103
104 public static JAXBConfigImpl parseAndInit(String location) {
105 JAXBConfigImpl config = parseConfig(location);
106 ConfigContext.init(config);
107 return config;
108 }
109
110 public static Properties getProperties(Config config) {
111 if (config instanceof JAXBConfigImpl) {
112 JAXBConfigImpl jci = (JAXBConfigImpl) config;
113 return jci.getRawProperties();
114 } else {
115 logger.warn("Unable to access raw Rice config properties.");
116 return config.getProperties();
117 }
118 }
119
120 public static void addAndOverride(Properties oldProps, Properties newProps) {
121 add(oldProps, newProps);
122 override(oldProps, newProps);
123 }
124
125 public static void override(Properties oldProps, Properties newProps) {
126 SortedSet<String> commonKeys = Sets.newTreeSet(Sets.intersection(newProps.stringPropertyNames(), oldProps.stringPropertyNames()));
127 if (commonKeys.size() == 0) {
128 return;
129 }
130 logger.debug("{} keys in common", commonKeys.size());
131 for (String commonKey : commonKeys) {
132 String oldValue = oldProps.getProperty(commonKey);
133 String newValue = newProps.getProperty(commonKey);
134 if (!newValue.equals(oldValue)) {
135 Object[] args = { commonKey, toLogMsg(commonKey, oldValue), toLogMsg(commonKey, newValue) };
136 logger.info("Overriding - [{}]=[{}]->[{}]", args);
137 oldProps.setProperty(commonKey, newValue);
138 }
139 }
140 }
141
142 public static void add(Properties oldProps, Properties newProps) {
143 SortedSet<String> newKeys = Sets.newTreeSet(Sets.difference(newProps.stringPropertyNames(), oldProps.stringPropertyNames()));
144 if (newKeys.size() == 0) {
145 return;
146 }
147 logger.info("Adding {} properties", newKeys.size());
148 for (String newKey : newKeys) {
149 String value = newProps.getProperty(newKey);
150 logger.debug("Adding - [{}]=[{}]", newKey, toLogMsg(newKey, value));
151 oldProps.setProperty(newKey, value);
152 }
153 }
154
155 protected static String toLogMsg(String key, String value) {
156 return Str.flatten(ConfigLogger.getDisplaySafeValue(key, value));
157 }
158 }