[spring-extensions] branch master updated: IDP-1640 - Override Spring WebFlow's ApplicationContext creation logic

Scott Cantor cantor.2 at osu.edu
Thu Jul 16 18:20:41 UTC 2020


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

scantor pushed a commit to branch master
in repository spring-extensions.

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

The following commit(s) were added to refs/heads/master by this push:
       new  8d5744b   IDP-1640 - Override Spring WebFlow's ApplicationContext creation logic
8d5744b is described below

commit 8d5744bb6b05b0a1571e29397a217072f1381fa2
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu Jul 16 14:21:10 2020 -0400

    IDP-1640 - Override Spring WebFlow's ApplicationContext creation logic
    
    https://issues.shibboleth.net/jira/browse/IDP-1640
    
    Add another context override class to fill a gap.
    Enhance our bean definition reader to stop screwing up imports.
---
 .../FilesystemGenericWebApplicationContext.java    | 96 ++++++++++++++++++++++
 ...chemaTypeAwareBeanDefinitionDocumentReader.java | 41 +++++++++
 2 files changed, 137 insertions(+)

diff --git a/src/main/java/net/shibboleth/ext/spring/context/FilesystemGenericWebApplicationContext.java b/src/main/java/net/shibboleth/ext/spring/context/FilesystemGenericWebApplicationContext.java
new file mode 100644
index 0000000..17a6ce2
--- /dev/null
+++ b/src/main/java/net/shibboleth/ext/spring/context/FilesystemGenericWebApplicationContext.java
@@ -0,0 +1,96 @@
+/*
+ * 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.context;
+
+import net.shibboleth.ext.spring.resource.ConditionalResourceResolver;
+import net.shibboleth.ext.spring.util.AnnotationParameterNameDiscoverer;
+
+import javax.servlet.ServletContext;
+
+import org.springframework.beans.factory.support.DefaultListableBeanFactory;
+import org.springframework.core.io.FileSystemResource;
+import org.springframework.core.io.Resource;
+import org.springframework.web.context.support.GenericWebApplicationContext;
+
+/**
+ * An extension of {@link GenericWebApplicationContext} that is biased in favor of the filesystem such that bare
+ * resource paths are assumed to be files rather than classpath resources, and supports conditional resources.
+ * 
+ * @since 6.1.0
+ */
+public class FilesystemGenericWebApplicationContext extends GenericWebApplicationContext {
+
+    /** Constructor. */
+    public FilesystemGenericWebApplicationContext() {
+        getDefaultListableBeanFactory().setParameterNameDiscoverer(new AnnotationParameterNameDiscoverer());
+        addProtocolResolver(new ConditionalResourceResolver());
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param beanFactory bean factory
+     */
+    public FilesystemGenericWebApplicationContext(final DefaultListableBeanFactory beanFactory) {
+        super(beanFactory);
+        beanFactory.setParameterNameDiscoverer(new AnnotationParameterNameDiscoverer());
+        addProtocolResolver(new ConditionalResourceResolver());
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param context servlet context
+     */
+    public FilesystemGenericWebApplicationContext(final ServletContext context) {
+        super(context);
+        getDefaultListableBeanFactory().setParameterNameDiscoverer(new AnnotationParameterNameDiscoverer());
+        addProtocolResolver(new ConditionalResourceResolver());
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param beanFactory bean factory
+     * @param context servlet context
+     */
+    public FilesystemGenericWebApplicationContext(final DefaultListableBeanFactory beanFactory,
+            final ServletContext context) {
+        super(beanFactory, context);
+        beanFactory.setParameterNameDiscoverer(new AnnotationParameterNameDiscoverer());
+        addProtocolResolver(new ConditionalResourceResolver());
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * <p>
+     * Overrides the standard behavior of path-only resources and treats them as file paths if the path exists. Note
+     * that this differs from the ordinary Spring contexts that default to file paths because paths are treated as
+     * absolute if they are in fact absolute.
+     * </p>
+     */
+    @Override protected Resource getResourceByPath(final String path) {
+        final Resource r = new FileSystemResource(path);
+        if (r.exists()) {
+            return r;
+        }
+        return super.getResourceByPath(path);
+    }
+
+}
\ No newline at end of file
diff --git a/src/main/java/net/shibboleth/ext/spring/util/SchemaTypeAwareBeanDefinitionDocumentReader.java b/src/main/java/net/shibboleth/ext/spring/util/SchemaTypeAwareBeanDefinitionDocumentReader.java
index 6aeec34..3310f70 100644
--- a/src/main/java/net/shibboleth/ext/spring/util/SchemaTypeAwareBeanDefinitionDocumentReader.java
+++ b/src/main/java/net/shibboleth/ext/spring/util/SchemaTypeAwareBeanDefinitionDocumentReader.java
@@ -17,9 +17,15 @@
 
 package net.shibboleth.ext.spring.util;
 
+import java.util.LinkedHashSet;
+import java.util.Set;
+
 import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
 import org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader;
 import org.springframework.beans.factory.xml.XmlReaderContext;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+import org.springframework.util.StringUtils;
 import org.w3c.dom.Element;
 
 /**
@@ -28,6 +34,41 @@ import org.w3c.dom.Element;
  */
 public class SchemaTypeAwareBeanDefinitionDocumentReader extends DefaultBeanDefinitionDocumentReader {
 
+    /**
+     * {@inheritDoc}
+     * 
+     * This override prevents the default behavior from kicking in if the original resource location
+     * is directly usable by the installed {@link ResourceLoader}.
+     */
+    @Override
+    protected void importBeanDefinitionResource(final Element ele) {
+        String location = ele.getAttribute(RESOURCE_ATTRIBUTE);
+        if (!StringUtils.hasText(location)) {
+            getReaderContext().error("Resource location must not be empty", ele);
+            return;
+        }
+
+        // Resolve system properties: e.g. "${user.dir}"
+        location = getReaderContext().getEnvironment().resolveRequiredPlaceholders(location);
+
+        final Set<Resource> actualResources = new LinkedHashSet<>(4);
+
+        final Resource r = getReaderContext().getResourceLoader().getResource(location);
+        if (r.exists()) {
+            final int importCount = getReaderContext().getReader().loadBeanDefinitions(r);
+            actualResources.add(r);
+            if (logger.isTraceEnabled()) {
+                logger.trace("Imported " + importCount + " bean definitions from location [" + location + "]");
+            }
+            final Resource[] actResArray = actualResources.toArray(new Resource[0]);
+            getReaderContext().fireImportProcessed(location, actResArray, extractSource(ele));
+            return;
+        }
+        
+        logger.info("Resource location [" + location + "] does not exist, delegating to default behavior");
+        super.importBeanDefinitionResource(ele);
+    }
+
     /** {@inheritDoc} */
     @Override protected BeanDefinitionParserDelegate createDelegate(final XmlReaderContext readerContext,
             final Element root,

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


More information about the commits mailing list