[java-shib-shared] branch main updated: JSSH-33 - Review property placement behavior in context builder

Scott Cantor cantor.2 at osu.edu
Wed Jun 14 12:48:47 UTC 2023


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

scantor pushed a commit to branch main
in repository java-shib-shared.

View the commit online:
http://git.shibboleth.net/view/?p=java-shib-shared.git;a=commit;h=298fab2213e98a82d27ea56244d1f01ef445ac46

The following commit(s) were added to refs/heads/main by this push:
     new 298fab22 JSSH-33 - Review property placement behavior in context builder
298fab22 is described below

commit 298fab2213e98a82d27ea56244d1f01ef445ac46
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Wed Jun 14 08:48:40 2023 -0400

    JSSH-33 - Review property placement behavior in context builder
    
    https://shibboleth.atlassian.net/browse/JSSH-33
    
    Prevent duplicate injection of Property replacer.
    Auto-add replacer if not already installed.
    Inject context and environment into conveters if needed.
---
 .../spring/util/ApplicationContextBuilder.java     | 40 +++++++++++++++++-----
 1 file changed, 32 insertions(+), 8 deletions(-)

diff --git a/shib-spring/src/main/java/net/shibboleth/shared/spring/util/ApplicationContextBuilder.java b/shib-spring/src/main/java/net/shibboleth/shared/spring/util/ApplicationContextBuilder.java
index a4551e30..d37be319 100644
--- a/shib-spring/src/main/java/net/shibboleth/shared/spring/util/ApplicationContextBuilder.java
+++ b/shib-spring/src/main/java/net/shibboleth/shared/spring/util/ApplicationContextBuilder.java
@@ -20,8 +20,8 @@ package net.shibboleth.shared.spring.util;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 import javax.annotation.Nonnull;
@@ -54,6 +54,7 @@ import org.springframework.context.support.ConversionServiceFactoryBean;
 import org.springframework.context.support.GenericApplicationContext;
 import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
 import org.springframework.core.convert.ConversionService;
+import org.springframework.core.convert.converter.Converter;
 import org.springframework.core.env.PropertySource;
 import org.springframework.core.io.Resource;
 
@@ -344,24 +345,42 @@ public class ApplicationContextBuilder {
         loader.addProtocolResolver(new ConditionalResourceResolver());
         context.setResourceLoader(loader);
         
+        // With the post-processors and converters, we need to make sure to check for a need to
+        // inject the ApplicationContext or Environment. Spring won't do this since these objects
+        // were created outside and not inside the context.
+        
         if (conversionService != null) {
             context.getBeanFactory().setConversionService(conversionService);
         } else {
             final ConversionServiceFactoryBean service = new ConversionServiceFactoryBean();
-            service.setConverters(new HashSet<>(Arrays.asList(
+            final Set<Converter<?,?>> converters = CollectionSupport.setOf(
                     new StringToIPRangeConverter(),
                     new BooleanToPredicateConverter(),
                     new StringBooleanToPredicateConverter(),
                     new StringToResourceConverter(),
-                    new StringToDurationConverter())));
+                    new StringToDurationConverter());
+            for (final Converter<?,?> c : converters) {
+                if (c instanceof ApplicationContextAware aware) {
+                    aware.setApplicationContext(context);
+                }
+                if (c instanceof EnvironmentAware aware) {
+                    aware.setEnvironment(context.getEnvironment());
+                }
+            }
+            service.setConverters(converters);
             service.afterPropertiesSet();
             context.getBeanFactory().setConversionService(service.getObject());
         }
         
+        boolean needPropertyConfigurer = true;
+        
         if (factoryPostProcessors != null) {
             for (final BeanFactoryPostProcessor bfpp : factoryPostProcessors) {
                 assert bfpp != null;
                 context.addBeanFactoryPostProcessor(bfpp);
+                if (bfpp instanceof PropertySourcesPlaceholderConfigurer) {
+                    needPropertyConfigurer = false;
+                }
                 if (bfpp instanceof ApplicationContextAware aware) {
                     aware.setApplicationContext(context);
                 }
@@ -371,6 +390,16 @@ public class ApplicationContextBuilder {
             }
         }
 
+        // Auto-install property replacement if needed.
+        if (needPropertyConfigurer) {
+            final PropertySourcesPlaceholderConfigurer propertyConfigurer =
+                    new PropertySourcesPlaceholderConfigurer();
+            propertyConfigurer.setPlaceholderPrefix("%{");
+            propertyConfigurer.setPlaceholderSuffix("}");
+            propertyConfigurer.setEnvironment(context.getEnvironment());
+            context.getBeanFactoryPostProcessors().add(propertyConfigurer);
+        }
+        
         if (postProcessors != null) {
             for (final BeanPostProcessor bpp : postProcessors) {
                 assert bpp != null;
@@ -394,11 +423,6 @@ public class ApplicationContextBuilder {
             propertySources.forEach(p -> context.getEnvironment().getPropertySources().addLast(p));
             context.getEnvironment().setPlaceholderPrefix("%{");
             context.getEnvironment().setPlaceholderSuffix("}");
-            final PropertySourcesPlaceholderConfigurer propertyConfigurer = new  PropertySourcesPlaceholderConfigurer();
-            propertyConfigurer.setPlaceholderPrefix("%{");
-            propertyConfigurer.setPlaceholderSuffix("}");
-            propertyConfigurer.setEnvironment(context.getEnvironment());
-            context.getBeanFactoryPostProcessors().add(propertyConfigurer);
         }
         
         if (installShutdownHook) {

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


More information about the commits mailing list