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 private RiceConfigUtils() {}
48
49
50
51
52
53
54
55
56
57
58
59 public static Config getRootConfig(Properties loaded, String location, ServletContext servletContext) {
60
61 Config config = ConfigContext.getCurrentContextConfig();
62 Preconditions.checkNotNull(config, "'config' cannot be null");
63 Properties listenerProperties = getProperties(config);
64
65
66 JAXBConfigImpl parsed = parseConfig(location, listenerProperties);
67
68
69 addAndOverride(loaded, parsed.getRawProperties());
70
71
72
73 Properties servlet = PropertySources.convert(servletContext);
74 Properties global = PropertyUtils.getGlobalProperties(servlet);
75 addAndOverride(loaded, global);
76 logger.info("Using {} distinct properties", Integer.valueOf(loaded.size()));
77
78
79 return new JAXBConfigImpl(loaded);
80
81 }
82
83
84
85
86
87
88
89
90 public static JAXBConfigImpl parseConfig(String location) {
91 return parseConfig(location, ImmutableProperties.of());
92 }
93
94
95
96
97
98
99
100
101
102 public static JAXBConfigImpl parseConfig(String location, Properties properties) {
103 try {
104 JAXBConfigImpl config = new JAXBConfigImpl(location, properties);
105 config.parseConfig();
106 return config;
107 } catch (IOException e) {
108 throw new IllegalStateException("Unexpected error parsing config", e);
109 }
110 }
111
112
113
114
115
116
117
118
119 public static JAXBConfigImpl parseAndInit(String location) {
120 JAXBConfigImpl config = parseConfig(location);
121 ConfigContext.init(config);
122 return config;
123 }
124
125
126
127
128
129
130
131
132 public static Properties getProperties(Config config) {
133 if (config instanceof JAXBConfigImpl) {
134 JAXBConfigImpl jci = (JAXBConfigImpl) config;
135 return jci.getRawProperties();
136 } else {
137 logger.warn("Unable to access raw Rice config properties.");
138 return config.getProperties();
139 }
140 }
141
142
143
144
145
146
147
148 public static void putProperties(Config config, Properties properties) {
149 SortedSet<String> keys = Sets.newTreeSet(properties.stringPropertyNames());
150 for (String key : keys) {
151 config.putProperty(key, properties.getProperty(key));
152 }
153 }
154
155 private static void add(Properties oldProperties, Properties newProperties) {
156 SortedSet<String> newKeys = Sets.newTreeSet(Sets.difference(newProperties.stringPropertyNames(), oldProperties.stringPropertyNames()));
157
158 if (newKeys.isEmpty()) {
159 return;
160 }
161
162 logger.info("Adding {} properties", Integer.valueOf(newKeys.size()));
163
164 for (String newKey : newKeys) {
165 String value = newProperties.getProperty(newKey);
166 logger.debug("Adding - [{}]=[{}]", newKey, toLogMsg(newKey, value));
167 oldProperties.setProperty(newKey, value);
168 }
169 }
170
171 private static void override(Properties oldProperties, Properties newProperties) {
172 SortedSet<String> commonKeys = Sets.newTreeSet(Sets.intersection(newProperties.stringPropertyNames(), oldProperties.stringPropertyNames()));
173
174 if (commonKeys.isEmpty()) {
175 return;
176 }
177
178 logger.debug("{} keys in common", Integer.valueOf(commonKeys.size()));
179
180 for (String commonKey : commonKeys) {
181 String oldValue = oldProperties.getProperty(commonKey);
182 String newValue = newProperties.getProperty(commonKey);
183
184 if (!newValue.equals(oldValue)) {
185 Object[] args = { commonKey, toLogMsg(commonKey, oldValue), toLogMsg(commonKey, newValue) };
186 logger.info("Overriding - [{}]=[{}]->[{}]", args);
187 oldProperties.setProperty(commonKey, newValue);
188 }
189 }
190 }
191
192 private static void addAndOverride(Properties oldProperties, Properties newProperties) {
193 add(oldProperties, newProperties);
194 override(oldProperties, newProperties);
195 }
196
197 private static String toLogMsg(String key, String value) {
198 return Str.flatten(ConfigLogger.getDisplaySafeValue(key, value));
199 }
200
201 }