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 Logger log = LoggerFactory.getLogger(Configuration.class);
47
48
49 private static QName defaultProvider = new QName(XMLConstants.XMLTOOLING_CONFIG_NS,
50 XMLConstants.XMLTOOLING_DEFAULT_OBJECT_PROVIDER);
51
52
53 private static Map<QName, Element> configuredObjectProviders = new ConcurrentHashMap<QName, Element>(0);
54
55
56 private static Map<String, Element> validatorSuiteConfigurations = new ConcurrentHashMap<String, Element>(0);
57
58
59 private static XMLObjectBuilderFactory builderFactory = new XMLObjectBuilderFactory();
60
61
62 private static MarshallerFactory marshallerFactory = new MarshallerFactory();
63
64
65 private static UnmarshallerFactory unmarshallerFactory = new UnmarshallerFactory();
66
67
68 private static Map<String, ValidatorSuite> validatorSuites = new ConcurrentHashMap<String, ValidatorSuite>(5);
69
70
71 private static Set<QName> idAttributeNames = new CopyOnWriteArraySet<QName>();
72
73
74 private static SecurityConfiguration globalSecurityConfig;
75
76
77 private static ParserPool parserPool;
78
79
80 protected Configuration() {
81
82 }
83
84
85
86
87
88
89 public static ParserPool getParserPool() {
90 return parserPool;
91 }
92
93
94
95
96
97
98 public static void setParserPool(ParserPool newParserPool) {
99 parserPool = newParserPool;
100 }
101
102
103
104
105
106
107
108 public static QName getDefaultProviderQName() {
109 return defaultProvider;
110 }
111
112
113
114
115
116
117
118
119
120
121 public static void registerObjectProvider(QName providerName, XMLObjectBuilder builder, Marshaller marshaller,
122 Unmarshaller unmarshaller) {
123 log.debug("Registering new builder, marshaller, and unmarshaller for {}", providerName);
124 builderFactory.registerBuilder(providerName, builder);
125 marshallerFactory.registerMarshaller(providerName, marshaller);
126 unmarshallerFactory.registerUnmarshaller(providerName, unmarshaller);
127 }
128
129
130
131
132
133
134 public static void deregisterObjectProvider(QName key) {
135 log.debug("Unregistering builder, marshaller, and unmarshaller for {}", key);
136 configuredObjectProviders.remove(key);
137 builderFactory.deregisterBuilder(key);
138 marshallerFactory.deregisterMarshaller(key);
139 unmarshallerFactory.deregisterUnmarshaller(key);
140 }
141
142
143
144
145
146
147 public static XMLObjectBuilderFactory getBuilderFactory() {
148 return builderFactory;
149 }
150
151
152
153
154
155
156 public static MarshallerFactory getMarshallerFactory() {
157 return marshallerFactory;
158 }
159
160
161
162
163
164
165
166 public static UnmarshallerFactory getUnmarshallerFactory() {
167 return unmarshallerFactory;
168 }
169
170
171
172
173
174
175
176
177 public static void registerValidatorSuite(String suiteId, ValidatorSuite suite) {
178 validatorSuites.put(suiteId, suite);
179 }
180
181
182
183
184
185
186 public static void deregisterValidatorSuite(String suiteId) {
187 validatorSuiteConfigurations.remove(suiteId);
188 validatorSuites.remove(suiteId);
189 }
190
191
192
193
194
195
196
197
198 public static ValidatorSuite getValidatorSuite(String suiteId) {
199 return validatorSuites.get(suiteId);
200 }
201
202
203
204
205
206
207 public static void registerIDAttribute(QName attributeName) {
208 if (!idAttributeNames.contains(attributeName)) {
209 idAttributeNames.add(attributeName);
210 }
211 }
212
213
214
215
216
217
218 public static void deregisterIDAttribute(QName attributeName) {
219 if (idAttributeNames.contains(attributeName)) {
220 idAttributeNames.remove(attributeName);
221 }
222 }
223
224
225
226
227
228
229
230 public static boolean isIDAttribute(QName attributeName) {
231 return idAttributeNames.contains(attributeName);
232 }
233
234
235
236
237
238
239 public static SecurityConfiguration getGlobalSecurityConfiguration() {
240 return globalSecurityConfig;
241 }
242
243
244
245
246
247
248 public static void setGlobalSecurityConfiguration(SecurityConfiguration config) {
249 globalSecurityConfig = config;
250 }
251
252
253
254
255 public static void validateNonSunJAXP() {
256 String builderFactoryClass = DocumentBuilderFactory.newInstance().getClass().getName();
257 log.debug("VM using JAXP parser {}", builderFactoryClass);
258
259 if (builderFactoryClass.startsWith("com.sun")) {
260 String errorMsg = "\n\n\nOpenSAML requires an xml parser that supports JAXP 1.3 and DOM3.\n"
261 + "The JVM is currently configured to use the Sun XML parser, which is known\n"
262 + "to be buggy and can not be used with OpenSAML. Please endorse a functional\n"
263 + "JAXP library(ies) such as Xerces and Xalan. For instructions on how to endorse\n"
264 + "a new parser see http://java.sun.com/j2se/1.5.0/docs/guide/standards/index.html\n\n\n";
265
266 log.error(errorMsg);
267 throw new Error(errorMsg);
268 }
269 }
270
271
272
273
274
275
276
277
278
279
280 public static boolean validateJCEProviders() {
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 log.debug("Registering new builder, marshaller, and unmarshaller for {}", providerName);
323 if (configuration != null) {
324 configuredObjectProviders.put(providerName, configuration);
325 }
326 builderFactory.registerBuilder(providerName, builder);
327 marshallerFactory.registerMarshaller(providerName, marshaller);
328 unmarshallerFactory.registerUnmarshaller(providerName, unmarshaller);
329 }
330
331
332
333
334
335
336
337
338
339
340
341
342 public static Element getObjectProviderConfiguration(QName qualifedName) {
343 Element configElement = configuredObjectProviders.get(qualifedName);
344 if (configElement != null) {
345 return (Element) configElement.cloneNode(true);
346 }
347 return null;
348 }
349
350
351
352
353
354
355
356
357
358
359 public static void registerValidatorSuite(String suiteId, ValidatorSuite suite, Element configuration) {
360 if (configuration != null) {
361 validatorSuiteConfigurations.put(suiteId, configuration);
362 }
363 validatorSuites.put(suiteId, suite);
364 }
365
366
367
368
369
370
371
372
373
374
375
376
377 public static Element getValidatorSuiteConfiguration(String suiteId) {
378 Element configElement = validatorSuiteConfigurations.get(suiteId);
379 if (configElement != null) {
380 return (Element) configElement.cloneNode(true);
381 }
382
383 return null;
384 }
385
386 static {
387 validateJCEProviders();
388
389
390 registerIDAttribute(new QName(javax.xml.XMLConstants.XML_NS_URI, "id"));
391 }
392 }