1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.xml;
18
19 import java.security.NoSuchAlgorithmException;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.CopyOnWriteArraySet;
24
25 import javax.crypto.Cipher;
26 import javax.crypto.NoSuchPaddingException;
27 import javax.xml.namespace.QName;
28 import javax.xml.parsers.DocumentBuilderFactory;
29
30 import org.opensaml.xml.io.Marshaller;
31 import org.opensaml.xml.io.MarshallerFactory;
32 import org.opensaml.xml.io.Unmarshaller;
33 import org.opensaml.xml.io.UnmarshallerFactory;
34 import org.opensaml.xml.parse.ParserPool;
35 import org.opensaml.xml.security.SecurityConfiguration;
36 import org.opensaml.xml.util.XMLConstants;
37 import org.opensaml.xml.validation.ValidatorSuite;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.w3c.dom.Element;
41
42
43 public class Configuration {
44
45
46 private static QName defaultProvider = new QName(XMLConstants.XMLTOOLING_CONFIG_NS,
47 XMLConstants.XMLTOOLING_DEFAULT_OBJECT_PROVIDER);
48
49
50 private static Map<QName, Element> configuredObjectProviders = new ConcurrentHashMap<QName, Element>(0);
51
52
53 private static Map<String, Element> validatorSuiteConfigurations = new ConcurrentHashMap<String, Element>(0);
54
55
56 private static XMLObjectBuilderFactory builderFactory = new XMLObjectBuilderFactory();
57
58
59 private static MarshallerFactory marshallerFactory = new MarshallerFactory();
60
61
62 private static UnmarshallerFactory unmarshallerFactory = new UnmarshallerFactory();
63
64
65 private static Map<String, ValidatorSuite> validatorSuites = new ConcurrentHashMap<String, ValidatorSuite>(5);
66
67
68 private static Set<QName> idAttributeNames = new CopyOnWriteArraySet<QName>();
69
70
71 private static SecurityConfiguration globalSecurityConfig;
72
73
74 private static ParserPool parserPool;
75
76
77 protected Configuration() {
78
79 }
80
81
82
83
84
85
86 public static ParserPool getParserPool() {
87 return parserPool;
88 }
89
90
91
92
93
94
95 public static void setParserPool(ParserPool newParserPool) {
96 parserPool = newParserPool;
97 }
98
99
100
101
102
103
104
105 public static QName getDefaultProviderQName() {
106 return defaultProvider;
107 }
108
109
110
111
112
113
114
115
116
117
118 public static void registerObjectProvider(QName providerName, XMLObjectBuilder builder, Marshaller marshaller,
119 Unmarshaller unmarshaller) {
120 Logger log = getLogger();
121 log.debug("Registering new builder, marshaller, and unmarshaller for {}", providerName);
122 builderFactory.registerBuilder(providerName, builder);
123 marshallerFactory.registerMarshaller(providerName, marshaller);
124 unmarshallerFactory.registerUnmarshaller(providerName, unmarshaller);
125 }
126
127
128
129
130
131
132 public static void deregisterObjectProvider(QName key) {
133 Logger log = getLogger();
134 log.debug("Unregistering builder, marshaller, and unmarshaller for {}", key);
135 configuredObjectProviders.remove(key);
136 builderFactory.deregisterBuilder(key);
137 marshallerFactory.deregisterMarshaller(key);
138 unmarshallerFactory.deregisterUnmarshaller(key);
139 }
140
141
142
143
144
145
146 public static XMLObjectBuilderFactory getBuilderFactory() {
147 return builderFactory;
148 }
149
150
151
152
153
154
155 public static MarshallerFactory getMarshallerFactory() {
156 return marshallerFactory;
157 }
158
159
160
161
162
163
164
165 public static UnmarshallerFactory getUnmarshallerFactory() {
166 return unmarshallerFactory;
167 }
168
169
170
171
172
173
174
175 public static void registerValidatorSuite(String suiteId, ValidatorSuite suite) {
176 validatorSuites.put(suiteId, suite);
177 }
178
179
180
181
182
183
184 public static void deregisterValidatorSuite(String suiteId) {
185 validatorSuiteConfigurations.remove(suiteId);
186 validatorSuites.remove(suiteId);
187 }
188
189
190
191
192
193
194
195
196 public static ValidatorSuite getValidatorSuite(String suiteId) {
197 return validatorSuites.get(suiteId);
198 }
199
200
201
202
203
204
205 public static void registerIDAttribute(QName attributeName) {
206 if (!idAttributeNames.contains(attributeName)) {
207 idAttributeNames.add(attributeName);
208 }
209 }
210
211
212
213
214
215
216 public static void deregisterIDAttribute(QName attributeName) {
217 if (idAttributeNames.contains(attributeName)) {
218 idAttributeNames.remove(attributeName);
219 }
220 }
221
222
223
224
225
226
227
228 public static boolean isIDAttribute(QName attributeName) {
229 return idAttributeNames.contains(attributeName);
230 }
231
232
233
234
235
236
237 public static SecurityConfiguration getGlobalSecurityConfiguration() {
238 return globalSecurityConfig;
239 }
240
241
242
243
244
245
246 public static void setGlobalSecurityConfiguration(SecurityConfiguration config) {
247 globalSecurityConfig = config;
248 }
249
250
251
252
253 public static void validateNonSunJAXP() {
254 Logger log = getLogger();
255 String builderFactoryClass = DocumentBuilderFactory.newInstance().getClass().getName();
256 log.debug("VM using JAXP parser {}", builderFactoryClass);
257
258 if (builderFactoryClass.startsWith("com.sun")) {
259 String errorMsg = "\n\n\nOpenSAML requires an xml parser that supports JAXP 1.3 and DOM3.\n"
260 + "The JVM is currently configured to use the Sun XML parser, which is known\n"
261 + "to be buggy and can not be used with OpenSAML. Please endorse a functional\n"
262 + "JAXP library(ies) such as Xerces and Xalan. For instructions on how to endorse\n"
263 + "a new parser see http://java.sun.com/j2se/1.5.0/docs/guide/standards/index.html\n\n\n";
264
265 log.error(errorMsg);
266 throw new Error(errorMsg);
267 }
268 }
269
270
271
272
273
274
275
276
277
278
279 public static boolean validateJCEProviders() {
280 Logger log = getLogger();
281 boolean ret = true;
282
283
284
285
286
287 String errorMsgAESPadding = "The JCE providers currently configured in the JVM do not support\n"
288 + "required capabilities for XML Encryption, either the 'AES' cipher algorithm\n"
289 + "or the 'ISO10126Padding' padding scheme\n";
290
291 try {
292 Cipher.getInstance("AES/CBC/ISO10126Padding");
293 } catch (NoSuchAlgorithmException e) {
294
295
296 log.warn(errorMsgAESPadding);
297 ret = false;
298 } catch (NoSuchPaddingException e) {
299 log.warn(errorMsgAESPadding);
300 ret = false;
301 }
302
303
304
305 return ret;
306 }
307
308
309
310
311
312
313
314
315
316
317
318
319
320 public static void registerObjectProvider(QName providerName, XMLObjectBuilder builder, Marshaller marshaller,
321 Unmarshaller unmarshaller, Element configuration) {
322 Logger log = getLogger();
323 log.debug("Registering new builder, marshaller, and unmarshaller for {}", providerName);
324 if (configuration != null) {
325 configuredObjectProviders.put(providerName, configuration);
326 }
327 builderFactory.registerBuilder(providerName, builder);
328 marshallerFactory.registerMarshaller(providerName, marshaller);
329 unmarshallerFactory.registerUnmarshaller(providerName, unmarshaller);
330 }
331
332
333
334
335
336
337
338
339
340
341
342
343 public static Element getObjectProviderConfiguration(QName qualifedName) {
344 Element configElement = configuredObjectProviders.get(qualifedName);
345 if (configElement != null) {
346 return (Element) configElement.cloneNode(true);
347 }
348 return null;
349 }
350
351
352
353
354
355
356
357
358
359
360 public static void registerValidatorSuite(String suiteId, ValidatorSuite suite, Element configuration) {
361 if (configuration != null) {
362 validatorSuiteConfigurations.put(suiteId, configuration);
363 }
364 validatorSuites.put(suiteId, suite);
365 }
366
367
368
369
370
371
372
373
374
375
376
377
378 public static Element getValidatorSuiteConfiguration(String suiteId) {
379 Element configElement = validatorSuiteConfigurations.get(suiteId);
380 if (configElement != null) {
381 return (Element) configElement.cloneNode(true);
382 }
383
384 return null;
385 }
386
387
388
389
390
391
392 private static Logger getLogger() {
393 return LoggerFactory.getLogger(Configuration.class);
394 }
395
396 static {
397 validateJCEProviders();
398
399
400 registerIDAttribute(new QName(javax.xml.XMLConstants.XML_NS_URI, "id"));
401 }
402 }