[spring-extensions] 04/04: IDP-1047 Add AnnotationParameterNameDiscoverer and test

Rod Widdowson rdw at steadingsoftware.com
Mon Sep 19 12:24:36 EDT 2016


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

commit e025400fe0b192f821e5bfd59adfbb8fc50534dd
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Mon Sep 19 17:20:11 2016 +0100

    IDP-1047 Add AnnotationParameterNameDiscoverer and test
    
    https://issues.shibboleth.net/jira/browse/IDP-1047
    
    ParameterNameDiscoverer to look at the ParameterName annotation,
    plus extended test.
    
    Still have to work out how to inject this properly into our system
    and then what needs to be annotated.
---
 .../util/AnnotationParameterNameDiscoverer.java    | 99 ++++++++++++++++++++++
 .../net/shibboleth/ext/spring/util/ParamClass.java |  5 ++
 .../spring/util/ParameterNameAnnotationTest.java   | 31 +++++--
 src/test/resources/logback-test.xml                |  2 +-
 .../net/shibboleth/ext/spring/util/paramBeans.xml  |  2 +
 5 files changed, 129 insertions(+), 10 deletions(-)

diff --git a/src/main/java/net/shibboleth/ext/spring/util/AnnotationParameterNameDiscoverer.java b/src/main/java/net/shibboleth/ext/spring/util/AnnotationParameterNameDiscoverer.java
new file mode 100644
index 0000000..39bf3aa
--- /dev/null
+++ b/src/main/java/net/shibboleth/ext/spring/util/AnnotationParameterNameDiscoverer.java
@@ -0,0 +1,99 @@
+/*
+ * 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.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+
+import javax.annotation.Nullable;
+
+import net.shibboleth.utilities.java.support.annotation.ParameterName;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.DefaultParameterNameDiscoverer;
+import org.springframework.core.ParameterNameDiscoverer;
+
+/**
+ * An implementation of {@link ParameterNameDiscoverer} that is driven by the {@link ParameterName} Annotation.
+ */
+public class AnnotationParameterNameDiscoverer extends DefaultParameterNameDiscoverer implements
+        ParameterNameDiscoverer {
+
+    /** log. */
+    private final Logger log = LoggerFactory.getLogger(AnnotationParameterNameDiscoverer.class);
+
+    /** {@inheritDoc} */
+    @Override @Nullable public String[] getParameterNames(final Method method) {
+
+        return super.getParameterNames(method);
+    }
+
+    /** Given the annotations for each parameter is it one of ours?
+     * @param annotations the annotations for the parametere
+     * @return the "name" if one of ours.
+     */
+    private String getMyAnnotation(final Annotation[] annotations) {
+        for (final Annotation a : annotations) {
+            if (a instanceof ParameterName) {
+                final ParameterName param = (ParameterName) a;
+                return param.name();
+            }
+        }
+        return null;
+    }
+
+    /**
+     * {@inheritDoc} <br/>
+     * If we cannot do anything pass to the default discoverer.
+     */
+    @Override public String[] getParameterNames(final Constructor<?> ctor) {
+
+        final Annotation[][] annotationsArray = ctor.getParameterAnnotations();
+
+        if (annotationsArray.length == 0) {
+            return super.getParameterNames(ctor);
+        }
+
+        final String className = ctor.getDeclaringClass().getName();
+        boolean allPresent = true;
+        final boolean isOurs = (className != null) && 
+                (className.startsWith("org.opensaml") || className.startsWith("net.shibboleth"));
+
+        final String[] names = new String[annotationsArray.length];
+
+        for (int index = 0; index < annotationsArray.length; index++) {
+            names[index] = getMyAnnotation(annotationsArray[index]);
+            if (names[index] == null) {
+                allPresent = false;
+            }
+        }
+
+        if (!allPresent) {
+            if (isOurs) {
+                log.warn("Constructor for class '{}' with {} parameters: "
+                        + "Not all parameters are annotated with @ParameterName", className, annotationsArray.length);
+            }
+            return super.getParameterNames(ctor);
+        }
+        log.trace("Constructor for class '{}' with {} parameters called {}", className, names.length, names);
+        return names;
+    }
+
+}
diff --git a/src/test/java/net/shibboleth/ext/spring/util/ParamClass.java b/src/test/java/net/shibboleth/ext/spring/util/ParamClass.java
index 751e040..89564fe 100644
--- a/src/test/java/net/shibboleth/ext/spring/util/ParamClass.java
+++ b/src/test/java/net/shibboleth/ext/spring/util/ParamClass.java
@@ -32,6 +32,11 @@ public class ParamClass {
         p2 = param1;
     }
     
+    public ParamClass(final String param1) {
+        p1 = param1;
+        p2 = "HardWired Param The Second";
+    }
+
     public String getP1() {return p1;}
     public String getP2() {return p2;}
 }
diff --git a/src/test/java/net/shibboleth/ext/spring/util/ParameterNameAnnotationTest.java b/src/test/java/net/shibboleth/ext/spring/util/ParameterNameAnnotationTest.java
index 5f74e11..d86e3e1 100644
--- a/src/test/java/net/shibboleth/ext/spring/util/ParameterNameAnnotationTest.java
+++ b/src/test/java/net/shibboleth/ext/spring/util/ParameterNameAnnotationTest.java
@@ -17,6 +17,8 @@
 
 package net.shibboleth.ext.spring.util;
 
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
 import org.springframework.context.support.GenericApplicationContext;
 import org.testng.Assert;
@@ -41,19 +43,27 @@ public class ParameterNameAnnotationTest {
         Assert.assertEquals(byNumber.getP2(), "Param the Second");
         
         final ParamClass byId = context.getBean("InThroughTheOutBean", ParamClass.class);
-
-        //
         // Swapped
-        //
-        Assert.assertEquals(byId.getP2(), "Param the First");
         Assert.assertEquals(byId.getP1(), "Param the Second");
+        Assert.assertEquals(byId.getP2(), "Param the First");
+
+        final ParamClass single = context.getBean("SingleParam", ParamClass.class);
+        Assert.assertEquals(single.getP1(), "Param the First");
+        Assert.assertEquals(single.getP2(), "HardWired Param The Second");
 
     }
     
-    @Test(enabled=false) public void testWithAnnotationFilter() {
+    @Test(enabled=true) public void testWithAnnotationFilter() {
         
         final GenericApplicationContext context = new GenericApplicationContext();
         final XmlBeanDefinitionReader beanDefinitionReader = new SchemaTypeAwareXMLBeanDefinitionReader(context);
+        
+        final ConfigurableListableBeanFactory factory = context.getBeanFactory();
+        if (factory instanceof AbstractAutowireCapableBeanFactory) {
+            final AbstractAutowireCapableBeanFactory aaBeanFactory = (AbstractAutowireCapableBeanFactory) factory;
+            aaBeanFactory.setParameterNameDiscoverer(new AnnotationParameterNameDiscoverer());
+        }
+        
 
         beanDefinitionReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
         beanDefinitionReader.loadBeanDefinitions("net/shibboleth/ext/spring/util/paramBeans.xml");
@@ -64,13 +74,16 @@ public class ParameterNameAnnotationTest {
         Assert.assertEquals(byNumber.getP2(), "Param the Second");
         
         final ParamClass byId = context.getBean("InThroughTheOutBean", ParamClass.class);
-
-        //
-        // Swapped
-        //
+        // Correct
         Assert.assertEquals(byId.getP1(), "Param the First");
         Assert.assertEquals(byId.getP2(), "Param the Second");
 
+        // Generate warning
+        final ParamClass single = context.getBean("SingleParam", ParamClass.class);
+        Assert.assertEquals(single.getP1(), "Param the First");
+        Assert.assertEquals(single.getP2(), "HardWired Param The Second");
+
+
     }
 
 }
diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml
index fc63e09..3db9f1c 100644
--- a/src/test/resources/logback-test.xml
+++ b/src/test/resources/logback-test.xml
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <configuration>
 
-    <logger name="net.shibboleth.ext.spring" level="DEBUG"/>
+    <logger name="net.shibboleth.ext.spring" level="TRACE"/>
     <logger name="net.shibboleth.utilities" level="DEBUG"/>
 
     <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
diff --git a/src/test/resources/net/shibboleth/ext/spring/util/paramBeans.xml b/src/test/resources/net/shibboleth/ext/spring/util/paramBeans.xml
index 9009f54..f40dd3b 100644
--- a/src/test/resources/net/shibboleth/ext/spring/util/paramBeans.xml
+++ b/src/test/resources/net/shibboleth/ext/spring/util/paramBeans.xml
@@ -5,5 +5,7 @@
     <bean id="TheBeanRemainsTheSame" class="net.shibboleth.ext.spring.util.ParamClass" c:_0="Param the First" c:_1="Param the Second"/>
     
     <bean id="InThroughTheOutBean" class="net.shibboleth.ext.spring.util.ParamClass" c:param1="Param the First" c:param2="Param the Second"/>
+
+    <bean id="SingleParam" class="net.shibboleth.ext.spring.util.ParamClass" c:param1="Param the First"/>
         
 </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