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.attribute.resolver;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.xml.namespace.QName;
25  
26  import org.opensaml.xml.util.DatatypeHelper;
27  import org.opensaml.xml.util.XMLHelper;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.beans.factory.support.AbstractBeanDefinition;
31  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
32  import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
33  import org.springframework.beans.factory.xml.ParserContext;
34  import org.w3c.dom.Element;
35  
36  /**
37   * Base class for Spring bean definition parser for Shibboleth resolver plug-ins.
38   */
39  public abstract class AbstractResolutionPlugInBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
40  
41      /** Name of resolution plug-in dependency. */
42      public static final QName DEPENDENCY_ELEMENT_NAME = new QName(AttributeResolverNamespaceHandler.NAMESPACE,
43              "Dependency");
44  
45      /** Class logger. */
46      private final Logger log = LoggerFactory.getLogger(AbstractResolutionPlugInBeanDefinitionParser.class);
47  
48      /**
49       * Parses the plugins ID and attribute definition and data connector dependencies.
50       * 
51       * {@inheritDoc}
52       */
53      protected final void doParse(Element config, ParserContext parserContext, BeanDefinitionBuilder builder) {
54          String pluginId = DatatypeHelper.safeTrimOrNullString(config.getAttributeNS(null, "id"));
55          log.info("Parsing configuration for {} plugin with ID: {}", config.getLocalName(), pluginId);
56          builder.addPropertyValue("pluginId", pluginId);
57  
58          Map<QName, List<Element>> children = XMLHelper.getChildElements(config);
59  
60          List<String> dependencyIds = parseDependencies(children.get(DEPENDENCY_ELEMENT_NAME));
61          if(dependencyIds != null && !dependencyIds.isEmpty()){
62              log.debug("Dependencies for plugin {}: {}", pluginId, dependencyIds);
63              builder.addPropertyValue("dependencyIds", dependencyIds);
64          }else{
65              log.debug("Dependencies for plugin {}: none", pluginId);
66          }
67  
68          doParse(pluginId, config, children, builder, parserContext);
69      }
70  
71      /**
72       * Parses the plugin configuration.
73       * 
74       * @param pluginId unique ID of the plugin
75       * @param pluginConfig root plugin configuration element
76       * @param pluginConfigChildren immediate children of the root configuration element (provided to save from having to
77       *            reparse them)
78       * @param pluginBuilder bean definition builder for the plugin
79       * @param parserContext current parsing context
80       */
81      protected abstract void doParse(String pluginId, Element pluginConfig,
82              Map<QName, List<Element>> pluginConfigChildren, BeanDefinitionBuilder pluginBuilder,
83              ParserContext parserContext);
84  
85      /** {@inheritDoc} */
86      protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) {
87          return element.getAttributeNS(null, "id");
88      }
89  
90      /**
91       * Parse dependency elements.
92       * 
93       * @param elements DOM elements of type <code>resolver:PluginDependencyType</code>
94       * 
95       * @return the dependency IDs
96       */
97      protected List<String> parseDependencies(List<Element> elements) {
98          if (elements == null || elements.size() == 0) {
99              return null;
100         }
101 
102         List<String> dependencyIds = new ArrayList<String>();
103         for (Element dependency : elements) {
104             dependencyIds.add(DatatypeHelper.safeTrimOrNullString(dependency.getAttributeNS(null, "ref")));
105         }
106 
107         return dependencyIds;
108     }
109 }