[java-shib-shared] branch main updated: JSSH-34 - Process nested beans in RelocatedBeanFactoryPostProcessor

Ian Young ian at iay.org.uk
Thu Jun 29 15:30:30 UTC 2023


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

iay 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=b440fd5cbb91cdcee5d46fc21e21b59a5ca6bc65

The following commit(s) were added to refs/heads/main by this push:
     new b440fd5c JSSH-34 - Process nested beans in RelocatedBeanFactoryPostProcessor
b440fd5c is described below

commit b440fd5cbb91cdcee5d46fc21e21b59a5ca6bc65
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Thu Jun 29 16:30:26 2023 +0100

    JSSH-34 - Process nested beans in RelocatedBeanFactoryPostProcessor
    
    https://shibboleth.atlassian.net/browse/JSSH-34
---
 .../config/RelocatedBeanFactoryPostProcessor.java  | 45 ++++++++++++++++------
 .../RelocatedBeanFactoryPostProcessorTest.java     | 39 ++++++++++++++++++-
 .../shibboleth/shared/spring/config/relocated.xml  | 26 ++++++++++++-
 3 files changed, 96 insertions(+), 14 deletions(-)

diff --git a/shib-spring/src/main/java/net/shibboleth/shared/spring/config/RelocatedBeanFactoryPostProcessor.java b/shib-spring/src/main/java/net/shibboleth/shared/spring/config/RelocatedBeanFactoryPostProcessor.java
index 5be0d6e3..7839bacc 100644
--- a/shib-spring/src/main/java/net/shibboleth/shared/spring/config/RelocatedBeanFactoryPostProcessor.java
+++ b/shib-spring/src/main/java/net/shibboleth/shared/spring/config/RelocatedBeanFactoryPostProcessor.java
@@ -23,7 +23,9 @@ import java.util.Map;
 import javax.annotation.Nonnull;
 
 import org.springframework.beans.BeansException;
+import org.springframework.beans.PropertyValue;
 import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.config.BeanDefinitionHolder;
 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
 
@@ -84,6 +86,34 @@ public class RelocatedBeanFactoryPostProcessor implements BeanFactoryPostProcess
         });
     }
 
+    /**
+     * Process a bean definition by relocating its class or parent if appropriate.
+     *
+     * @param def bean definition to be processed
+     * @param name name of the bean definition
+     */
+    private void processBeanDefinition(@Nonnull final BeanDefinition def, @Nonnull final String name) {
+        final String className = def.getBeanClassName();
+        if (className != null && movedClasses.containsKey(className)) {
+            DeprecationSupport.warn(ObjectType.CLASS, className, "Bean ID: " + name, movedClasses.get(className));
+            def.setBeanClassName(movedClasses.get(className));
+        }
+        
+        final String parentName = def.getParentName();
+        if (parentName != null && movedBeans.containsKey(parentName)) {
+            DeprecationSupport.warn(ObjectType.BEAN, parentName, "Bean ID: " + name, movedBeans.get(parentName));
+            def.setParentName(movedBeans.get(parentName));
+        }
+
+        // Look recursively inside any property values for nested and potentially un-named bean definitions
+        for (final PropertyValue property : def.getPropertyValues()) {
+            if (property.getValue() instanceof BeanDefinitionHolder defHolder) {
+                // Handle potentially unnamed bean definitions
+                processBeanDefinition(defHolder.getBeanDefinition(), defHolder.getBeanName());
+            }
+        }
+    }
+
     /** {@inheritDoc} */
     public void postProcessBeanFactory(@Nonnull final ConfigurableListableBeanFactory beanFactory)
             throws BeansException {
@@ -91,18 +121,9 @@ public class RelocatedBeanFactoryPostProcessor implements BeanFactoryPostProcess
         for (final String name : beanFactory.getBeanDefinitionNames()) {
             assert name != null;
             final BeanDefinition def = beanFactory.getBeanDefinition(name);
-            
-            final String className = def.getBeanClassName();
-            if (className != null && movedClasses.containsKey(className)) {
-                DeprecationSupport.warn(ObjectType.CLASS, className, "Bean ID: " + name, movedClasses.get(className));
-                def.setBeanClassName(movedClasses.get(className));
-            }
-            
-            final String parentName = def.getParentName();
-            if (parentName != null && movedBeans.containsKey(parentName)) {
-                DeprecationSupport.warn(ObjectType.BEAN, parentName, "Bean ID: " + name, movedBeans.get(parentName));
-                def.setParentName(movedBeans.get(parentName));
-            }
+
+            // Handle the named bean definition
+            processBeanDefinition(def, name);
         }
     }
 
diff --git a/shib-spring/src/test/java/net/shibboleth/shared/spring/config/RelocatedBeanFactoryPostProcessorTest.java b/shib-spring/src/test/java/net/shibboleth/shared/spring/config/RelocatedBeanFactoryPostProcessorTest.java
index e575c327..49570269 100644
--- a/shib-spring/src/test/java/net/shibboleth/shared/spring/config/RelocatedBeanFactoryPostProcessorTest.java
+++ b/shib-spring/src/test/java/net/shibboleth/shared/spring/config/RelocatedBeanFactoryPostProcessorTest.java
@@ -17,6 +17,8 @@
 
 package net.shibboleth.shared.spring.config;
 
+import javax.annotation.Nonnull;
+
 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.GenericApplicationContext;
@@ -30,6 +32,32 @@ import net.shibboleth.shared.spring.custom.SchemaTypeAwareXMLBeanDefinitionReade
  */
 public class RelocatedBeanFactoryPostProcessorTest {
 
+    /**
+     * Wraps a {@link String}.
+     */
+    public static class OuterBean {
+        private String innerString;
+        public void setInnerString(@Nonnull final String str) {
+            innerString = str;
+        }
+        public String getInnerString() {
+            return innerString;
+        }
+    }
+
+    /**
+     * Wraps an {@link OuterBean}.
+     */
+    public static class OuterBeanWrapper {
+        private OuterBean bean;
+        public void setBean(@Nonnull final OuterBean b) {
+            bean = b;
+        }
+        public OuterBean getBean() {
+            return bean;
+        }
+    }
+
     /**
      * Test replacement of bean's class.
      */
@@ -41,7 +69,16 @@ public class RelocatedBeanFactoryPostProcessorTest {
 
         final String frobnitz = (String) context.getBean("frobnitz");
         Assert.assertEquals(frobnitz, "bar");
-}
+
+        final var outerBean = context.getBean("outerBean", OuterBean.class);
+        Assert.assertEquals(outerBean.getInnerString(), "missing-nested");
+
+        final var outerBean2 = context.getBean("outerBean2", OuterBean.class);
+        Assert.assertEquals(outerBean2.getInnerString(), "missing-parent-nested");
+
+        final var layeredBeans = context.getBean("layeredBeans", OuterBeanWrapper.class);
+        Assert.assertEquals(layeredBeans.getBean().getInnerString(), "doubly-nested");
+    }
 
     private ApplicationContext getContext(final String config) {
         final GenericApplicationContext context = new GenericApplicationContext();
diff --git a/shib-spring/src/test/resources/net/shibboleth/shared/spring/config/relocated.xml b/shib-spring/src/test/resources/net/shibboleth/shared/spring/config/relocated.xml
index b078cd54..9ee0738c 100644
--- a/shib-spring/src/test/resources/net/shibboleth/shared/spring/config/relocated.xml
+++ b/shib-spring/src/test/resources/net/shibboleth/shared/spring/config/relocated.xml
@@ -28,4 +28,28 @@
     <!-- BFPP will rewrite as a String. -->
     <bean id="frobnitz" parent="missing" />
 
-</beans>
\ No newline at end of file
+    <!-- Test nested bean whose class should be rewritten as String. -->
+    <bean id="outerBean" class="net.shibboleth.shared.spring.config.RelocatedBeanFactoryPostProcessorTest.OuterBean">
+        <property name="innerString">
+            <bean class="org.example.missing.Foo" c:_0="missing-nested"/>
+        </property>
+    </bean>
+    
+    <!-- Test nested bean whose parent needs to be rewritten. -->
+    <bean id="outerBean2" class="net.shibboleth.shared.spring.config.RelocatedBeanFactoryPostProcessorTest.OuterBean">
+        <property name="innerString">
+            <bean parent="missing" c:_0="missing-parent-nested"/>
+        </property>
+    </bean>
+    
+    <!-- Dig down multiple layers. -->
+    <bean id="layeredBeans" class="net.shibboleth.shared.spring.config.RelocatedBeanFactoryPostProcessorTest.OuterBeanWrapper">
+        <property name="bean">
+            <bean class="net.shibboleth.shared.spring.config.RelocatedBeanFactoryPostProcessorTest.OuterBean">
+                <property name="innerString">
+                    <bean class="org.example.missing.Foo" c:_0="doubly-nested"/>
+                </property>
+            </bean>
+        </property>
+    </bean>
+</beans>

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


More information about the commits mailing list