[spring-extensions] 01/02: JSE-49 Allow NamespaceHandler base class to sub-delegate to additional handlers

Rod Widdowson rdw at steadingsoftware.com
Sun Jun 19 14:19:52 UTC 2022


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch main
in repository spring-extensions.

View the commit online:
http://git.shibboleth.net/view/?p=spring-extensions.git;a=commit;h=e66bb6a369f0656ae1d6bd78c70c1d24178cba1b

commit e66bb6a369f0656ae1d6bd78c70c1d24178cba1b
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Sun Jun 19 15:16:54 2022 +0100

    JSE-49 Allow NamespaceHandler base class to sub-delegate to additional handlers
    
    https://shibboleth.atlassian.net/browse/JSE-49
    
    Initial implementation.
---
 .../util/AnnotationParameterNameDiscoverer.java    |  2 +-
 .../spring/util/BaseSpringNamespaceHandler.java    | 47 +++++++++++++++-
 .../ext/spring/util/SecondaryNamespaceHandler.java | 62 ++++++++++++++++++++++
 3 files changed, 108 insertions(+), 3 deletions(-)

diff --git a/src/main/java/net/shibboleth/ext/spring/util/AnnotationParameterNameDiscoverer.java b/src/main/java/net/shibboleth/ext/spring/util/AnnotationParameterNameDiscoverer.java
index 5bd7a1d..b4335bf 100644
--- a/src/main/java/net/shibboleth/ext/spring/util/AnnotationParameterNameDiscoverer.java
+++ b/src/main/java/net/shibboleth/ext/spring/util/AnnotationParameterNameDiscoverer.java
@@ -75,7 +75,7 @@ public class AnnotationParameterNameDiscoverer extends DefaultParameterNameDisco
         }
 
         final String className = ctor.getDeclaringClass().getName();
-        final boolean isOurs = (className != null) && 
+        final boolean isOurs = className != null &&
                 (className.startsWith("org.opensaml") || className.startsWith("net.shibboleth"));
         if (!isOurs) {
             return super.getParameterNames(ctor);
diff --git a/src/main/java/net/shibboleth/ext/spring/util/BaseSpringNamespaceHandler.java b/src/main/java/net/shibboleth/ext/spring/util/BaseSpringNamespaceHandler.java
index 4f70d11..4e095dd 100644
--- a/src/main/java/net/shibboleth/ext/spring/util/BaseSpringNamespaceHandler.java
+++ b/src/main/java/net/shibboleth/ext/spring/util/BaseSpringNamespaceHandler.java
@@ -17,6 +17,14 @@
 
 package net.shibboleth.ext.spring.util;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Collections;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -24,6 +32,8 @@ import javax.xml.namespace.QName;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.BeanCreationException;
 import org.springframework.beans.factory.config.BeanDefinition;
 import org.springframework.beans.factory.config.BeanDefinitionHolder;
 import org.springframework.beans.factory.xml.BeanDefinitionParser;
@@ -44,6 +54,11 @@ import net.shibboleth.utilities.java.support.xml.QNameSupport;
  */
 public abstract class BaseSpringNamespaceHandler implements NamespaceHandler {
 
+    /**
+     * The base location to look for the secondary mapping files. Can be present in multiple JAR files.
+     */
+    public static final String DEFAULT_SECONDARY_HANDLER_BASE_LOCATION = "META-INF/net/shibboleth/spring/handlers/";
+
     /** Class logger. */
     private final Logger log = LoggerFactory.getLogger(BaseSpringNamespaceHandler.class);
 
@@ -51,7 +66,7 @@ public abstract class BaseSpringNamespaceHandler implements NamespaceHandler {
      * Stores the {@link BeanDefinitionParser} implementations keyed by the local name of the {@link Element Elements}
      * they handle.
      */
-    private Map<QName, BeanDefinitionParser> parsers = new HashMap<>();
+    private Map<QName, BeanDefinitionParser> parsers = Collections.synchronizedMap(new HashMap<>());
 
     /**
      * A noop decorator.  Returns the input.
@@ -126,4 +141,32 @@ public abstract class BaseSpringNamespaceHandler implements NamespaceHandler {
     protected void registerBeanDefinitionParser(final QName elementNameOrType, final BeanDefinitionParser parser) {
         parsers.put(elementNameOrType, parser);
     }
-}
\ No newline at end of file
+
+    /** Call back to initialize any registered secondary handlers.  This is added to the
+     * {@link #init()} call if the handler knows that there will be other handlers.
+     * @param name a unique name (derived from the namespace URN) to locate the classes to use
+     */
+    protected void initializeOtherHandlers(final String name) {
+        try {
+            final Enumeration<URL> urls = ClassLoader.getSystemResources(
+                    DEFAULT_SECONDARY_HANDLER_BASE_LOCATION + name);
+            while (urls.hasMoreElements()) {
+                final URL url = urls.nextElement();
+                final URLConnection con = url.openConnection();
+                try (final InputStream is = con.getInputStream();
+                     final InputStreamReader isr = new InputStreamReader(is);
+                     final BufferedReader br = new BufferedReader(isr)) {
+                     final String className = br.readLine();
+                     @SuppressWarnings("unchecked")
+                     final Class<SecondaryNamespaceHandler> clazz =
+                             (Class<SecondaryNamespaceHandler>) Class.forName(className);
+                     final SecondaryNamespaceHandler namespaceHandler = BeanUtils.instantiateClass(clazz);
+                     namespaceHandler.init(parsers);
+                }
+            }
+        } catch (final IOException | ClassNotFoundException e) {
+            log.error("Secondary initialization failed for namespace {}", name, e);
+            throw new BeanCreationException("Secondary initialization failed for namespace '"+ name +"", e);
+        }
+    }
+}
diff --git a/src/main/java/net/shibboleth/ext/spring/util/SecondaryNamespaceHandler.java b/src/main/java/net/shibboleth/ext/spring/util/SecondaryNamespaceHandler.java
new file mode 100644
index 0000000..9903208
--- /dev/null
+++ b/src/main/java/net/shibboleth/ext/spring/util/SecondaryNamespaceHandler.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.ext.spring.util;
+
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+
+import org.springframework.beans.factory.xml.BeanDefinitionParser;
+import org.w3c.dom.Element;
+
+/** A secondary namespace handler to allow us to stack multiple parsers.  */
+public abstract class SecondaryNamespaceHandler {
+    
+    /**
+     * Stores the {@link BeanDefinitionParser} implementations keyed by the local name of the {@link Element Elements}
+     * they handle.
+     */
+    private Map<QName, BeanDefinitionParser> parsers; 
+    
+    /** Initialize the handler, called when a {@link BaseSpringNamespaceHandler}
+     * calls {@link BaseSpringNamespaceHandler#initializeOtherHandlers(String)}.
+     *  
+     * @param theParsers 
+     */
+    protected void init(final Map<QName, BeanDefinitionParser> theParsers) {
+        parsers = theParsers;
+        doInit();
+    }
+    
+    /**
+     * Subclasses call this to register the supplied {@link BeanDefinitionParser} to handle the specified element.
+     * The element name is the local (non-namespace qualified) name.
+     * 
+     * @param elementNameOrType the element name or schema type the parser is for
+     * @param parser the parser to register
+     */
+    protected void registerBeanDefinitionParser(final QName elementNameOrType, final BeanDefinitionParser parser) {
+        parsers.put(elementNameOrType, parser);
+    }
+
+    /**
+     * Subclasses implement this call and in it register the {@link BeanDefinitionParser}s 
+     * via calls to {@link #registerBeanDefinitionParser(QName, BeanDefinitionParser) }.
+     */
+    public abstract void doInit();
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list