View Javadoc

1   /*
2    * Copyright 2006 University Corporation for Advanced Internet Development, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  /** Class for loading library configuration files and retrieving the configured components. */
43  public class Configuration {
44  
45      /** Class logger. */
46      private static Logger log = LoggerFactory.getLogger(Configuration.class);
47  
48      /** Default object provider. */
49      private static QName defaultProvider = new QName(XMLConstants.XMLTOOLING_CONFIG_NS,
50              XMLConstants.XMLTOOLING_DEFAULT_OBJECT_PROVIDER);
51  
52      /** Object provider configuration elements indexed by QName. */
53      private static Map<QName, Element> configuredObjectProviders = new ConcurrentHashMap<QName, Element>(0);
54  
55      /** Validator suite configuration elements indexed by suite IDs. */
56      private static Map<String, Element> validatorSuiteConfigurations = new ConcurrentHashMap<String, Element>(0);
57  
58      /** Configured XMLObject builder factory. */
59      private static XMLObjectBuilderFactory builderFactory = new XMLObjectBuilderFactory();
60  
61      /** Configured XMLObject marshaller factory. */
62      private static MarshallerFactory marshallerFactory = new MarshallerFactory();
63  
64      /** Configured XMLObject unmarshaller factory. */
65      private static UnmarshallerFactory unmarshallerFactory = new UnmarshallerFactory();
66  
67      /** Configured ValidatorSuites. */
68      private static Map<String, ValidatorSuite> validatorSuites = new ConcurrentHashMap<String, ValidatorSuite>(5);
69  
70      /** Configured set of attribute QNames which have been globally registered as having an ID type. */
71      private static Set<QName> idAttributeNames = new CopyOnWriteArraySet<QName>();
72  
73      /** Configured global security configuration information. */
74      private static SecurityConfiguration globalSecurityConfig;
75  
76      /** Configured parser pool. */
77      private static ParserPool parserPool;
78  
79      /** Constructor. */
80      protected Configuration() {
81  
82      }
83      
84      /**
85       * Get the currently configured ParserPool instance.
86       * 
87       * @return the currently ParserPool
88       */
89      public static ParserPool getParserPool() {
90          return parserPool;
91      }
92  
93      /**
94       * Set the currently configured ParserPool instance.
95       * 
96       * @param newParserPool the new ParserPool instance to configure
97       */
98      public static void setParserPool(ParserPool newParserPool) {
99          parserPool = newParserPool;
100     }
101     
102     /**
103      * Gets the QName for the object provider that will be used for XMLObjects that do not have a registered object
104      * provider.
105      * 
106      * @return the QName for the default object provider
107      */
108     public static QName getDefaultProviderQName() {
109         return defaultProvider;
110     }
111 
112     /**
113      * Adds an object provider to this configuration.
114      * 
115      * @param providerName the name of the object provider, corresponding to the element name or type name that the
116      *            builder, marshaller, and unmarshaller operate on
117      * @param builder the builder for that given provider
118      * @param marshaller the marshaller for the provider
119      * @param unmarshaller the unmarshaller for the provider
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      * Removes the builder, marshaller, and unmarshaller registered to the given key.
131      * 
132      * @param key the key of the builder, marshaller, and unmarshaller to be removed
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      * Gets the XMLObject builder factory that has been configured with information from loaded configuration files.
144      * 
145      * @return the XMLObject builder factory
146      */
147     public static XMLObjectBuilderFactory getBuilderFactory() {
148         return builderFactory;
149     }
150 
151     /**
152      * Gets the XMLObject marshaller factory that has been configured with information from loaded configuration files.
153      * 
154      * @return the XMLObject marshaller factory
155      */
156     public static MarshallerFactory getMarshallerFactory() {
157         return marshallerFactory;
158     }
159 
160     /**
161      * Gets the XMLObject unmarshaller factory that has been configured with information from loaded configuration
162      * files.
163      * 
164      * @return the XMLObject unmarshaller factory
165      */
166     public static UnmarshallerFactory getUnmarshallerFactory() {
167         return unmarshallerFactory;
168     }
169 
170     /**
171      * Registers a configured validator suite.
172      * 
173      * @param suiteId the ID of the suite
174      * @param suite the configured suite
175      * @param configuration optional XML configuration information
176      */
177     public static void registerValidatorSuite(String suiteId, ValidatorSuite suite) {
178         validatorSuites.put(suiteId, suite);
179     }
180 
181     /**
182      * Removes a registered validator suite.
183      * 
184      * @param suiteId the ID of the suite
185      */
186     public static void deregisterValidatorSuite(String suiteId) {
187         validatorSuiteConfigurations.remove(suiteId);
188         validatorSuites.remove(suiteId);
189     }
190 
191     /**
192      * Gets a configured ValidatorSuite by its ID.
193      * 
194      * @param suiteId the suite's ID
195      * 
196      * @return the ValidatorSuite or null if no suite was registered under that ID
197      */
198     public static ValidatorSuite getValidatorSuite(String suiteId) {
199         return validatorSuites.get(suiteId);
200     }
201 
202     /**
203      * Register an attribute as having a type of ID.
204      * 
205      * @param attributeName the QName of the ID attribute to be registered
206      */
207     public static void registerIDAttribute(QName attributeName) {
208         if (!idAttributeNames.contains(attributeName)) {
209             idAttributeNames.add(attributeName);
210         }
211     }
212 
213     /**
214      * Deregister an attribute as having a type of ID.
215      * 
216      * @param attributeName the QName of the ID attribute to be de-registered
217      */
218     public static void deregisterIDAttribute(QName attributeName) {
219         if (idAttributeNames.contains(attributeName)) {
220             idAttributeNames.remove(attributeName);
221         }
222     }
223 
224     /**
225      * Determine whether a given attribute is registered as having an ID type.
226      * 
227      * @param attributeName the QName of the attribute to be checked for ID type.
228      * @return true if attribute is registered as having an ID type.
229      */
230     public static boolean isIDAttribute(QName attributeName) {
231         return idAttributeNames.contains(attributeName);
232     }
233 
234     /**
235      * Get the global security configuration.
236      * 
237      * @return the global security configuration instance
238      */
239     public static SecurityConfiguration getGlobalSecurityConfiguration() {
240         return globalSecurityConfig;
241     }
242 
243     /**
244      * Set the global security configuration.
245      * 
246      * @param config the new global security configuration instance
247      */
248     public static void setGlobalSecurityConfiguration(SecurityConfiguration config) {
249         globalSecurityConfig = config;
250     }
251 
252     /**
253      * Validates that the system is not using the horribly buggy Sun JAXP implementation.
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      * Validates that the set of security providers configured in the JVM supports required cryptographic capabilities,
273      * for example for the XML Encryption and XML Signature specifications.
274      * 
275      * Depending on the requirements of the calling code, failure to fully support encryption and signature requirements
276      * may or may not be significant, so return a status flag to let the caller make that determination.
277      * 
278      * @return false if one or more capablities are not present, otherwise true
279      */
280     public static boolean validateJCEProviders() {
281         boolean ret = true;
282 
283         // XML Encryption spec requires AES support (128 and 256).
284         // Some JRE's are known to ship with no JCE's that support
285         // the ISO10126Padding padding scheme.
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             // IBM JCE returns this as the top-level exception even for the unsupported padding case. :-(
295             // Otherwise would be nice to make the error msg more specific.
296             log.warn(errorMsgAESPadding);
297             ret = false;
298         } catch (NoSuchPaddingException e) {
299             log.warn(errorMsgAESPadding);
300             ret = false;
301         }
302 
303         // Could do more tests here as needed.
304 
305         return ret;
306     }
307 
308     /**
309      * Adds an object provider to this configuration.
310      * 
311      * @param providerName the name of the object provider, corresponding to the element name or type name that the
312      *            builder, marshaller, and unmarshaller operate on
313      * @param builder the builder for that given provider
314      * @param marshaller the marshaller for the provider
315      * @param unmarshaller the unmarshaller for the provider
316      * @param configuration optional XML configuration snippet
317      * 
318      * @deprecated this method is deprecated with no replacement
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      * Gets a clone of the configuration element for a qualified element. Note that this configuration reflects the
333      * state of things as they were when the configuration was loaded, applications may have programmatically removed
334      * builder, marshallers, and unmarshallers during runtime.
335      * 
336      * @param qualifedName the namespace qualifed element name of the schema type of the object provider
337      * 
338      * @return the object provider configuration element or null if no object provider is configured with that name
339      * 
340      * @deprecated this method is deprecated with no replacement
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      * Registers a configured validator suite.
352      * 
353      * @param suiteId the ID of the suite
354      * @param suite the configured suite
355      * @param configuration optional XML configuration information
356      * 
357      * @deprecated this method is deprecated with no replacement
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      * Gets a clone of the ValidatorSuite configuration element for the ID. Note that this configuration reflects the
368      * state of things as they were when the configuration was loaded, applications may have programmatically removed
369      * altered the suite during runtime.
370      * 
371      * @param suiteId the ID of the ValidatorSuite whose configuration is to be retrieved
372      * 
373      * @return the validator suite configuration element or null if no suite is configured with that ID
374      * 
375      * @deprecated this method is deprecated with no replacement
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         // Default to registering the xml:id attribute as an ID type for all configurations
390         registerIDAttribute(new QName(javax.xml.XMLConstants.XML_NS_URI, "id"));
391     }
392 }