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          log.debug("Setting the following attribute definition dependencies for plugin {}: {}", pluginId, dependencyIds);
61          builder.addPropertyValue("dependencyIds", dependencyIds);
62  
63          doParse(pluginId, config, children, builder, parserContext);
64      }
65  
66      /**
67       * Parses the plugin configuration.
68       * 
69       * @param pluginId unique ID of the plugin
70       * @param pluginConfig root plugin configuration element
71       * @param pluginConfigChildren immediate children of the root configuration element (provided to save from having to
72       *            reparse them)
73       * @param pluginBuilder bean definition builder for the plugin
74       * @param parserContext current parsing context
75       */
76      protected abstract void doParse(String pluginId, Element pluginConfig,
77              Map<QName, List<Element>> pluginConfigChildren, BeanDefinitionBuilder pluginBuilder,
78              ParserContext parserContext);
79  
80      /** {@inheritDoc} */
81      protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) {
82          return element.getAttributeNS(null, "id");
83      }
84  
85      /**
86       * Parse dependency elements.
87       * 
88       * @param elements DOM elements of type <code>resolver:PluginDependencyType</code>
89       * 
90       * @return the dependency IDs
91       */
92      protected List<String> parseDependencies(List<Element> elements) {
93          if (elements == null || elements.size() == 0) {
94              return null;
95          }
96  
97          List<String> dependencyIds = new ArrayList<String>();
98          for (Element dependency : elements) {
99              dependencyIds.add(DatatypeHelper.safeTrimOrNullString(dependency.getAttributeNS(null, "ref")));
100         }
101 
102         return dependencyIds;
103     }
104 }