[java-opensaml] branch master updated: Add shorthand for string access to existing child contexts.

Scott Cantor cantor.2 at osu.edu
Tue Sep 4 15:05:19 EDT 2018


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

scantor pushed a commit to branch master
in repository java-opensaml.

View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=aa411d21e88a9a440daedb22c0e9148c9caf0f43

The following commit(s) were added to refs/heads/master by this push:
       new  aa411d2   Add shorthand for string access to existing child contexts.
aa411d2 is described below

commit aa411d21e88a9a440daedb22c0e9148c9caf0f43
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Tue Sep 4 15:05:17 2018 -0400

    Add shorthand for string access to existing child contexts.
---
 .../opensaml/messaging/context/BaseContext.java    | 19 +++++++++++++++-
 .../messaging/context/BaseContextTest.java         | 26 ++++++++++++++++++++++
 2 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/BaseContext.java b/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/BaseContext.java
index 2897d30..6af8d87 100644
--- a/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/BaseContext.java
+++ b/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/BaseContext.java
@@ -151,6 +151,12 @@ public abstract class BaseContext implements Iterable<BaseContext> {
     /**
      * Get a subcontext of the current context.
      * 
+     * <p>As of V3.4.0, if autocreate is false, this method will respond to a {@link ClassNotFoundException}
+     * by attempting to locate a matching subcontext based on the simple class name of the children and
+     * return the first match. If no match is found, it will proceed with the throw, but a future version
+     * of the API will eliminate that from the signature and simply return a null.</p>
+     * 
+     * 
      * @param className the name of the class type to obtain
      * @param autocreate flag indicating whether the subcontext instance should be auto-created
      * @return the held instance of the class, or null
@@ -158,7 +164,18 @@ public abstract class BaseContext implements Iterable<BaseContext> {
      */ 
     @Nullable public BaseContext getSubcontext(@Nonnull @NotEmpty final String className, final boolean autocreate)
             throws ClassNotFoundException {
-        return getSubcontext(Class.forName(className).asSubclass(BaseContext.class), autocreate);
+        try {
+            return getSubcontext(Class.forName(className).asSubclass(BaseContext.class), autocreate);
+        } catch (final ClassNotFoundException e) {
+            if (!autocreate) {
+                for (final BaseContext child : this) {
+                    if (child.getClass().getSimpleName().equals(className)) {
+                        return child;
+                    }
+                }
+            }
+            throw e;
+        }
     }
     
     /**
diff --git a/opensaml-messaging-api/src/test/java/org/opensaml/messaging/context/BaseContextTest.java b/opensaml-messaging-api/src/test/java/org/opensaml/messaging/context/BaseContextTest.java
index 3d3861c..bf17de3 100644
--- a/opensaml-messaging-api/src/test/java/org/opensaml/messaging/context/BaseContextTest.java
+++ b/opensaml-messaging-api/src/test/java/org/opensaml/messaging/context/BaseContextTest.java
@@ -132,6 +132,7 @@ public class BaseContextTest {
     /**
      *  Test auto creation of subcontexts.
      */
+    @SuppressWarnings("deprecation")
     public void testAutoCreateSubcontext() {
         TestContext parent = new TestContext();
         
@@ -216,5 +217,30 @@ public class BaseContextTest {
         Assert.assertNotNull(child);
         Assert.assertTrue(child instanceof TestContext);
     }
+
+    public void testStringAccessMissing() throws ClassNotFoundException {
+        TestContext parent = new TestContext();
+        parent.addSubcontext(new TestContext());
+        
+        BaseContext child = parent.getSubcontext("org.opensaml.messaging.context.MessageContext");
+        Assert.assertNull(child);
+    }
+
+    public void testSimpleStringAccess() throws ClassNotFoundException {
+        TestContext parent = new TestContext();
+        parent.addSubcontext(new TestContext());
+        
+        BaseContext child = parent.getSubcontext("TestContext");
+        Assert.assertNotNull(child);
+        Assert.assertTrue(child instanceof TestContext);
+    }
+
+    @Test(expectedExceptions = ClassNotFoundException.class)
+    public void testSimpleStringError() throws ClassNotFoundException {
+        TestContext parent = new TestContext();
+        parent.addSubcontext(new TestContext());
+        
+        parent.getSubcontext("NoContext");
+    }
     
 }
\ 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