[java-shib-shared] branch main updated: BeanFactoryPostProcessor that rewrites moved classes and parent beans.

Scott Cantor cantor.2 at osu.edu
Tue Sep 13 14:19:24 UTC 2022


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=7fb4ffa617ff2448037cfb90aac57339ba698416

The following commit(s) were added to refs/heads/main by this push:
     new 7fb4ffa6 BeanFactoryPostProcessor that rewrites moved classes and parent beans.
7fb4ffa6 is described below

commit 7fb4ffa617ff2448037cfb90aac57339ba698416
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Tue Sep 13 10:19:21 2022 -0400

    BeanFactoryPostProcessor that rewrites moved classes and parent beans.
---
 .../util/RelocatedBeanFactoryPostProcessor.java    | 102 +++++++++++++++++++++
 .../RelocatedBeanFactoryPostProcessorTest.java     |  55 +++++++++++
 .../net/shibboleth/ext/spring/util/relocated.xml   |  34 +++++++
 3 files changed, 191 insertions(+)

diff --git a/shib-spring/src/main/java/net/shibboleth/ext/spring/util/RelocatedBeanFactoryPostProcessor.java b/shib-spring/src/main/java/net/shibboleth/ext/spring/util/RelocatedBeanFactoryPostProcessor.java
new file mode 100644
index 00000000..18b5f810
--- /dev/null
+++ b/shib-spring/src/main/java/net/shibboleth/ext/spring/util/RelocatedBeanFactoryPostProcessor.java
@@ -0,0 +1,102 @@
+/*
+ * 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.HashMap;
+import java.util.Map;
+
+import javax.annotation.Nonnull;
+
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+
+import net.shibboleth.utilities.java.support.annotation.ParameterName;
+import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
+import net.shibboleth.utilities.java.support.primitive.DeprecationSupport;
+import net.shibboleth.utilities.java.support.primitive.DeprecationSupport.ObjectType;
+import net.shibboleth.utilities.java.support.primitive.StringSupport;
+
+/**
+ * A {@link BeanFactoryPostProcessor} to rewrite and log on relocated classes or parent beans.
+ * 
+ * @since 9.0.0
+ */
+public class RelocatedBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
+    
+    /** Relocated classes. */
+    @Nonnull @NonnullElements private Map<String,String> movedClasses;
+
+    /** Relocated beans. */
+    @Nonnull @NonnullElements private Map<String,String> movedBeans;
+
+    /**
+     * Set class names to rewrite.
+     * 
+     * @param classes classes to detect with replacements identified
+     */
+    public void setClasses(@Nonnull @ParameterName(name="classes") final Map<String,String> classes) {
+        movedClasses = new HashMap<>(classes.size());
+        classes.forEach((k,v) -> {
+            final String key = StringSupport.trimOrNull(k);
+            final String val = StringSupport.trimOrNull(v);
+            if (key != null) {
+                movedClasses.put(key, val);
+            }
+        });
+    }
+
+    /**
+     * Set bean names to rewrite.
+     * 
+     * @param beans beans to detect with replacements identified
+     */
+    public void setBeans(@Nonnull @ParameterName(name="classes") final Map<String,String> beans) {
+        movedBeans = new HashMap<>(beans.size());
+        beans.forEach((k,v) -> {
+            final String key = StringSupport.trimOrNull(k);
+            final String val = StringSupport.trimOrNull(v);
+            if (key != null) {
+                movedBeans.put(key, val);
+            }
+        });
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {
+        
+        for (final String name : beanFactory.getBeanDefinitionNames()) {
+            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));
+            }
+        }
+    }
+
+}
\ No newline at end of file
diff --git a/shib-spring/src/test/java/net/shibboleth/ext/spring/util/RelocatedBeanFactoryPostProcessorTest.java b/shib-spring/src/test/java/net/shibboleth/ext/spring/util/RelocatedBeanFactoryPostProcessorTest.java
new file mode 100644
index 00000000..a03c644b
--- /dev/null
+++ b/shib-spring/src/test/java/net/shibboleth/ext/spring/util/RelocatedBeanFactoryPostProcessorTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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 org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.GenericApplicationContext;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ * Test for {@link RelocatedBeanFactoryPostProcessor}.
+ */
+public class RelocatedBeanFactoryPostProcessorTest {
+
+    /**
+     * Test replacement of bean's class.
+     */
+    @Test public void testRelocated() {
+        final ApplicationContext context = getContext("net/shibboleth/ext/spring/util/relocated.xml");
+        
+        final String foo = (String) context.getBean("foo");
+        Assert.assertEquals(foo, "bar");
+
+        final String frobnitz = (String) context.getBean("frobnitz");
+        Assert.assertEquals(frobnitz, "bar");
+}
+
+    private ApplicationContext getContext(final String config) {
+        final GenericApplicationContext context = new GenericApplicationContext();
+        final XmlBeanDefinitionReader beanDefinitionReader = new SchemaTypeAwareXMLBeanDefinitionReader(context);
+
+        beanDefinitionReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
+        beanDefinitionReader.loadBeanDefinitions(config);
+        context.refresh();
+     
+        return context;
+    }
+
+}
\ No newline at end of file
diff --git a/shib-spring/src/test/resources/net/shibboleth/ext/spring/util/relocated.xml b/shib-spring/src/test/resources/net/shibboleth/ext/spring/util/relocated.xml
new file mode 100644
index 00000000..fe34c3ca
--- /dev/null
+++ b/shib-spring/src/test/resources/net/shibboleth/ext/spring/util/relocated.xml
@@ -0,0 +1,34 @@
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:context="http://www.springframework.org/schema/context"
+       xmlns:util="http://www.springframework.org/schema/util"
+       xmlns:p="http://www.springframework.org/schema/p"
+       xmlns:c="http://www.springframework.org/schema/c"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
+                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
+                           
+       default-init-method="initialize"
+       default-destroy-method="destroy">
+
+    <bean class="net.shibboleth.ext.spring.util.RelocatedBeanFactoryPostProcessor">
+        <property name="classes">
+            <map>
+                <entry key="org.example.missing.Foo" value="java.lang.String" />
+            </map>
+        </property>
+        <property name="beans">
+            <map>
+                <entry key="missing" value="foo" />
+            </map>
+        </property>
+    </bean>
+
+    <!-- BFPP will rewrite as a String. -->
+    <bean id="foo" class="org.example.missing.Foo"
+        c:_0="bar" />
+
+    <!-- BFPP will rewrite as a String. -->
+    <bean id="frobnitz" parent="missing" />
+
+</beans>
\ No newline at end of file

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


More information about the commits mailing list