[spring-extensions] branch master updated: IDP-1533 Add a CombiningListFactoryBean

Rod Widdowson rdw at steadingsoftware.com
Thu Feb 13 10:33:26 EST 2020


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

rdw 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=de7f8655fd9c6cfa088315ac4fb1d6534acee235

The following commit(s) were added to refs/heads/master by this push:
       new  de7f865   IDP-1533 Add a CombiningListFactoryBean
de7f865 is described below

commit de7f8655fd9c6cfa088315ac4fb1d6534acee235
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Thu Feb 13 10:12:28 2020 +0000

    IDP-1533 Add a CombiningListFactoryBean
    
    https://issues.shibboleth.net/jira/browse/IDP-1533
    
    The tests confirm that this can be used to combine lists of strings into
    a list of Resources as needed by this case.
---
 .../spring/factory/CombiningListFactoryBean.java   | 85 ++++++++++++++++++++++
 .../factory/CombiningListFactoryBeanTest.java      | 72 ++++++++++++++++++
 .../ext/spring/factory/ResourceListBean.java       | 41 +++++++++++
 .../net/shibboleth/ext/spring/factory/lists.xml    | 37 ++++++++++
 .../ext/spring/factory/resourceLists.xml           | 28 +++++++
 5 files changed, 263 insertions(+)

diff --git a/src/main/java/net/shibboleth/ext/spring/factory/CombiningListFactoryBean.java b/src/main/java/net/shibboleth/ext/spring/factory/CombiningListFactoryBean.java
new file mode 100644
index 0000000..d744723
--- /dev/null
+++ b/src/main/java/net/shibboleth/ext/spring/factory/CombiningListFactoryBean.java
@@ -0,0 +1,85 @@
+/*
+ * 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.factory;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import javax.annotation.Nonnull;
+
+import org.springframework.beans.factory.BeanCreationException;
+import org.springframework.beans.factory.config.ListFactoryBean;
+
+import net.shibboleth.utilities.java.support.logic.Constraint;
+
+/**
+ * A factory which extends {@link ListFactoryBean} by requiring two
+ * lists as input.
+ */
+public class CombiningListFactoryBean extends ListFactoryBean {
+
+    /** First list to combine. */
+    private List<?> firstList = Collections.emptyList();
+ 
+    /** Second list to combine. */
+    private List<?> secondList = Collections.emptyList();
+ 
+    /** {@inheritDoc} */
+    @Override public void setSourceList(final List<?> sourceList) {
+        throw new BeanCreationException("Call setFirstList() amnd setSecondList");
+    }
+
+    /** Set the first list to combine.
+     * @return Returns the firstList.
+     */
+    public List<?> getFirstList() {
+        return firstList;
+    }
+
+    /** Get the first list to combine.
+     * @param list The firstList to set.
+     */
+    public void setFirstList(@Nonnull final List<?> list) {
+        firstList = Constraint.isNotNull(list, "First list must be nonnull");
+    }
+
+    /** Set the second list to combine.
+     * @return Returns the secondList.
+     */
+    public List<?> getSecondList() {
+        return secondList;
+    }
+
+    /** Get the second list to combine.
+     * @param list The secondList to set.
+     */
+    public void setSecondList(@Nonnull final List<?> list) {
+        secondList = Constraint.isNotNull(list, "Second list must be nonnull");
+    }
+    
+    /** {@inheritDoc} */
+    @Override protected List<Object> createInstance() {
+        final ArrayList<Object> combined = new ArrayList<>(firstList.size() + secondList.size());
+        combined.addAll(firstList);
+        combined.addAll(secondList);
+        super.setSourceList(combined);
+        return super.createInstance();
+    }
+
+}
diff --git a/src/test/java/net/shibboleth/ext/spring/factory/CombiningListFactoryBeanTest.java b/src/test/java/net/shibboleth/ext/spring/factory/CombiningListFactoryBeanTest.java
new file mode 100644
index 0000000..a3f6b14
--- /dev/null
+++ b/src/test/java/net/shibboleth/ext/spring/factory/CombiningListFactoryBeanTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.factory;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.springframework.context.support.GenericApplicationContext;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+import org.testng.annotations.Test;
+
+import net.shibboleth.ext.spring.util.ApplicationContextBuilder;
+
+ at SuppressWarnings("javadoc")
+public class CombiningListFactoryBeanTest {
+
+    @Test
+    public void test() {
+        
+        Resource r = new ClassPathResource("net/shibboleth/ext/spring/factory/lists.xml");
+  
+        GenericApplicationContext ctx = new ApplicationContextBuilder()
+                .setName("appCtx")
+                .setServiceConfigurations(Collections.singletonList(r))
+                .build();
+        
+        
+        final List<?> list = (List<?>) ctx.getBean("combined");
+        
+        assertEquals(list.size(), 4);
+        assertTrue(list.contains("a"));
+        assertTrue(list.contains("b"));
+        assertTrue(list.contains("parent"));
+        assertTrue(list.contains("child"));
+    }
+    
+    @Test
+    public void resourceTest() {
+        
+        Resource r = new ClassPathResource("net/shibboleth/ext/spring/factory/resourceLists.xml");
+  
+        GenericApplicationContext ctx = new ApplicationContextBuilder()
+                .setName("appCtx")
+                .setServiceConfigurations(Collections.singletonList(r))
+                .build();
+        
+        
+        final ResourceListBean bean = ctx.getBean(ResourceListBean.class);
+        
+        assertEquals(bean.getResources().size(), 2);
+        
+    }
+}
diff --git a/src/test/java/net/shibboleth/ext/spring/factory/ResourceListBean.java b/src/test/java/net/shibboleth/ext/spring/factory/ResourceListBean.java
new file mode 100644
index 0000000..03423c5
--- /dev/null
+++ b/src/test/java/net/shibboleth/ext/spring/factory/ResourceListBean.java
@@ -0,0 +1,41 @@
+/*
+ * 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.factory;
+
+import java.util.List;
+
+import org.springframework.core.io.Resource;
+
+ at SuppressWarnings("javadoc")
+public class ResourceListBean {
+    private List<Resource> resources;
+
+    /** get list.
+     * @return Returns the resources.
+     */
+    public List<Resource> getResources() {
+        return resources;
+    }
+
+    /** set list.
+     * @param what The resources to set.
+     */
+    public void setResources(final List<Resource> what) {
+        resources = what;
+    }
+}
\ No newline at end of file
diff --git a/src/test/resources/net/shibboleth/ext/spring/factory/lists.xml b/src/test/resources/net/shibboleth/ext/spring/factory/lists.xml
new file mode 100644
index 0000000..23d5c84
--- /dev/null
+++ b/src/test/resources/net/shibboleth/ext/spring/factory/lists.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+           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"
+                           >
+
+    <util:list id="list">
+        <value>a</value>
+        <value>b</value>
+    </util:list>
+    
+    <bean id="parent" class="org.springframework.beans.factory.config.ListFactoryBean">
+        <property name="sourceList">
+            <list>
+                <value>parent</value>
+            </list>
+        </property>
+    </bean>
+    
+    
+    <bean id="child" parent="parent" class="org.springframework.beans.factory.config.ListFactoryBean">
+        <property name="sourceList">
+        <list merge="true">
+                <value>child</value>
+        </list>
+        </property>
+    </bean>
+
+    <bean id="combined" class="net.shibboleth.ext.spring.factory.CombiningListFactoryBean"
+     p:firstList-ref="list" p:secondList-ref="child"/>
+
+</beans>
\ No newline at end of file
diff --git a/src/test/resources/net/shibboleth/ext/spring/factory/resourceLists.xml b/src/test/resources/net/shibboleth/ext/spring/factory/resourceLists.xml
new file mode 100644
index 0000000..a85964a
--- /dev/null
+++ b/src/test/resources/net/shibboleth/ext/spring/factory/resourceLists.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+           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"
+                           >
+
+    <util:list id="first">
+        <value>classpath:/net/shibboleth/ext/spring/factory/lists.xml</value>
+    </util:list>
+     
+    <bean id="second" class="org.springframework.beans.factory.config.ListFactoryBean">
+        <property name="sourceList">
+        <list >
+        <value>classpath:/net/shibboleth/ext/spring/factory/resourceLists.xml</value>
+        </list>
+        </property>
+    </bean>
+
+    <bean id="combined" class="net.shibboleth.ext.spring.factory.CombiningListFactoryBean"
+     p:firstList-ref="first" p:secondList-ref="second"/>
+
+    <bean id = "resourceList" class="net.shibboleth.ext.spring.factory.ResourceListBean" p:resources-ref="combined" />
+</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