View Javadoc

1   /*
2    * Copyright [2007] [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 edu.internet2.middleware.shibboleth.common.config;
18  
19  import java.util.List;
20  
21  import org.opensaml.Configuration;
22  import org.opensaml.DefaultBootstrap;
23  import org.opensaml.util.resource.Resource;
24  import org.opensaml.xml.XMLConfigurator;
25  import org.opensaml.xml.parse.ParserPool;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.springframework.beans.factory.InitializingBean;
29  
30  /**
31   * A simple bean that may be used with Spring to initialize the OpenSAML library.
32   */
33  public class OpensamlConfigBean implements InitializingBean {
34      
35      /** Class logger. */
36      private final Logger log = LoggerFactory.getLogger(OpensamlConfigBean.class);
37      
38      /** OpenSAML configuration resources. */
39      private List<Resource> configResources;
40      
41      /** Optional ParserPool to configure. */
42      private ParserPool parserPool;
43      
44      /**
45       * Get the global ParserPool to configure.
46       * 
47       * @return Returns the parserPool.
48       */
49      public ParserPool getParserPool() {
50          return parserPool;
51      }
52  
53      /**
54       * Set the global ParserPool to configure.
55       * 
56       * @param newParserPool The parserPool to set.
57       */
58      public void setParserPool(ParserPool newParserPool) {
59          parserPool = newParserPool;
60      }
61  
62      /**
63       * Constructor.
64       *
65       * @param configs OpenSAML configuration resources
66       */
67      public OpensamlConfigBean(List<Resource> configs){
68          configResources = configs;
69      }
70  
71      /** {@inheritDoc} */
72      public void afterPropertiesSet() throws Exception {
73          DefaultBootstrap.bootstrap();
74          
75          if(configResources != null && !configResources.isEmpty()){
76              XMLConfigurator configurator = new XMLConfigurator();
77              for(Resource config : configResources){
78                  try{
79                      log.debug("Loading OpenSAML configuration file: {}", config.getLocation());
80                      configurator.load(config.getInputStream());
81                  }catch(Exception e){
82                      log.error("Unable to load OpenSAML configuration file: " + config.getLocation());
83                  }
84              }
85          }
86          
87          if (getParserPool() != null) {
88              Configuration.setParserPool(getParserPool());
89          }
90          
91      }
92  }