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;
18  
19  import org.apache.velocity.app.Velocity;
20  import org.apache.velocity.runtime.RuntimeConstants;
21  import org.apache.xml.security.Init;
22  import org.opensaml.saml1.binding.artifact.SAML1ArtifactBuilderFactory;
23  import org.opensaml.saml2.binding.artifact.SAML2ArtifactBuilderFactory;
24  import org.opensaml.xml.ConfigurationException;
25  import org.opensaml.xml.XMLConfigurator;
26  import org.opensaml.xml.security.DefaultSecurityConfigurationBootstrap;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * This class can be used to bootstrap the OpenSAML library with the default configurations that ship with the library.
32   */
33  public class DefaultBootstrap {
34  
35      /** Class logger. */
36      private static Logger log = LoggerFactory.getLogger(DefaultBootstrap.class);
37  
38      /** List of default XMLTooling configuration files. */
39      private static String[] xmlToolingConfigs = { 
40          "/default-config.xml", 
41          "/schema-config.xml", 
42          "/signature-config.xml",
43          "/signature-validation-config.xml", 
44          "/encryption-config.xml", 
45          "/encryption-validation-config.xml",
46          "/soap11-config.xml", 
47          "/wsfed11-protocol-config.xml",
48          "/saml1-assertion-config.xml", 
49          "/saml1-protocol-config.xml",
50          "/saml1-core-validation-config.xml", 
51          "/saml2-assertion-config.xml", 
52          "/saml2-protocol-config.xml",
53          "/saml2-core-validation-config.xml", 
54          "/saml1-metadata-config.xml", 
55          "/saml2-metadata-config.xml",
56          "/saml2-metadata-validation-config.xml", 
57          "/saml2-metadata-idp-discovery-config.xml",
58          "/saml2-protocol-thirdparty-config.xml",
59          "/saml2-metadata-query-config.xml", 
60          "/saml2-assertion-delegation-restriction-config.xml",    
61          "/saml2-ecp-config.xml",
62          "/xacml10-saml2-profile-config.xml",
63          "/xacml11-saml2-profile-config.xml",
64          "/xacml20-context-config.xml",
65          "/xacml20-policy-config.xml",
66          "/xacml2-saml2-profile-config.xml",
67          "/xacml3-saml2-profile-config.xml",    
68          "/wsaddressing-config.xml",
69          "/wssecurity-config.xml",
70      };
71  
72      /** Constructor. */
73      protected DefaultBootstrap() {
74  
75      }
76  
77      /**
78       * Initializes the OpenSAML library, loading default configurations.
79       * 
80       * @throws ConfigurationException thrown if there is a problem initializing the OpenSAML library
81       */
82      public static synchronized void bootstrap() throws ConfigurationException {
83  
84          initializeXMLSecurity();
85  
86          initializeVelocity();
87  
88          initializeXMLTooling(xmlToolingConfigs);
89  
90          initializeArtifactBuilderFactories();
91  
92          initializeGlobalSecurityConfiguration();
93      }
94  
95      /**
96       * Initializes the default global security configuration.
97       */
98      protected static void initializeGlobalSecurityConfiguration() {
99          Configuration.setGlobalSecurityConfiguration(DefaultSecurityConfigurationBootstrap.buildDefaultConfig());
100     }
101 
102     /**
103      * Initializes the Apache XMLSecurity libary.
104      * 
105      * @throws ConfigurationException thrown is there is a problem initializing the library
106      */
107     protected static void initializeXMLSecurity() throws ConfigurationException {
108         if (!Init.isInitialized()) {
109             log.debug("Initializing Apache XMLSecurity library");
110             Init.init();
111         }
112     }
113 
114     /**
115      * Intializes the Apache Velocity template engine.
116      * 
117      * @throws ConfigurationException thrown if there is a problem initializing Velocity
118      */
119     protected static void initializeVelocity() throws ConfigurationException {
120         try {
121             log.debug("Initializing Velocity template engine");
122             Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
123                     "org.apache.velocity.runtime.log.NullLogChute");
124             Velocity.setProperty(RuntimeConstants.ENCODING_DEFAULT, "UTF-8");
125             Velocity.setProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8");
126             Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
127             Velocity.setProperty("classpath.resource.loader.class",
128                     "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
129             Velocity.init();
130         } catch (Exception e) {
131             throw new ConfigurationException("Unable to initialize Velocity template engine", e);
132         }
133     }
134 
135     /**
136      * Initializes the XMLTooling library with a default set of object providers.
137      * 
138      * @param providerConfigs list of provider configuration files located on the classpath
139      * 
140      * @throws ConfigurationException thrown if there is a problem loading the configuration files
141      */
142     protected static void initializeXMLTooling(String[] providerConfigs) throws ConfigurationException {
143         Class clazz = Configuration.class;
144         XMLConfigurator configurator = new XMLConfigurator();
145 
146         for (String config : providerConfigs) {
147             log.debug("Loading XMLTooling configuration {}", config);
148             configurator.load(clazz.getResourceAsStream(config));
149         }
150     }
151 
152     /**
153      * Initializes the artifact factories for SAML 1 and SAML 2 artifacts.
154      * 
155      * @throws ConfigurationException thrown if there is a problem initializing the artifact factory
156      */
157     protected static void initializeArtifactBuilderFactories() throws ConfigurationException {
158         log.debug("Initializing SAML Artifact builder factories");
159         Configuration.setSAML1ArtifactBuilderFactory(new SAML1ArtifactBuilderFactory());
160         Configuration.setSAML2ArtifactBuilderFactory(new SAML2ArtifactBuilderFactory());
161     }
162 }