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