View Javadoc

1   /*
2    * Licensed to the University Corporation for Advanced Internet Development, 
3    * Inc. (UCAID) under one or more contributor license agreements.  See the 
4    * NOTICE file distributed with this work for additional information regarding
5    * copyright ownership. The UCAID licenses this file to You under the Apache 
6    * License, Version 2.0 (the "License"); you may not use this file except in 
7    * compliance with the License.  You may obtain a copy of the License at
8    *
9    *    http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package edu.internet2.middleware.shibboleth.common.config;
19  
20  import java.util.List;
21  
22  import org.opensaml.Configuration;
23  import org.opensaml.DefaultBootstrap;
24  import org.opensaml.util.resource.Resource;
25  import org.opensaml.xml.XMLConfigurator;
26  import org.opensaml.xml.parse.ParserPool;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.springframework.beans.factory.InitializingBean;
30  
31  /**
32   * A simple bean that may be used with Spring to initialize the OpenSAML library.
33   */
34  public class OpensamlConfigBean implements InitializingBean {
35      
36      /** Class logger. */
37      private final Logger log = LoggerFactory.getLogger(OpensamlConfigBean.class);
38      
39      /** OpenSAML configuration resources. */
40      private List<Resource> configResources;
41      
42      /** Optional ParserPool to configure. */
43      private ParserPool parserPool;
44      
45      /**
46       * Get the global ParserPool to configure.
47       * 
48       * @return Returns the parserPool.
49       */
50      public ParserPool getParserPool() {
51          return parserPool;
52      }
53  
54      /**
55       * Set the global ParserPool to configure.
56       * 
57       * @param newParserPool The parserPool to set.
58       */
59      public void setParserPool(ParserPool newParserPool) {
60          parserPool = newParserPool;
61      }
62  
63      /**
64       * Constructor.
65       *
66       * @param configs OpenSAML configuration resources
67       */
68      public OpensamlConfigBean(List<Resource> configs){
69          configResources = configs;
70      }
71  
72      /** {@inheritDoc} */
73      public void afterPropertiesSet() throws Exception {
74          DefaultBootstrap.bootstrap();
75          
76          if(configResources != null && !configResources.isEmpty()){
77              XMLConfigurator configurator = new XMLConfigurator();
78              for(Resource config : configResources){
79                  try{
80                      log.debug("Loading OpenSAML configuration file: {}", config.getLocation());
81                      configurator.load(config.getInputStream());
82                  }catch(Exception e){
83                      log.error("Unable to load OpenSAML configuration file: " + config.getLocation());
84                  }
85              }
86          }
87          
88          if (getParserPool() != null) {
89              Configuration.setParserPool(getParserPool());
90          }
91          
92      }
93  }