[java-shib-shared] branch main updated: JSSH-47 SpringServiceableComponent fails on ID-less component

Rod Widdowson rdw at steadingsoftware.com
Wed May 15 13:09:27 UTC 2024


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

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

The following commit(s) were added to refs/heads/main by this push:
     new e5f3a747 JSSH-47 SpringServiceableComponent fails on ID-less component
e5f3a747 is described below

commit e5f3a7470c1a42012535a593ad8bf58829e1e6ff
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Wed May 15 14:08:00 2024 +0100

    JSSH-47 SpringServiceableComponent fails on ID-less component
    
    https://shibboleth.atlassian.net/browse/JSSH-47
    
    If the supplied id is null (or there is none to find) make up a "should be unique"
    name and use that.  Log this at Debug.
---
 .../service/impl/SpringServiceableComponent.java   | 15 ++--
 .../impl/SpringServicableComponentTest.java        | 79 ++++++++++++++++++++++
 2 files changed, 90 insertions(+), 4 deletions(-)

diff --git a/shib-service/src/main/java/net/shibboleth/shared/spring/service/impl/SpringServiceableComponent.java b/shib-service/src/main/java/net/shibboleth/shared/spring/service/impl/SpringServiceableComponent.java
index ea8f687b..db374a26 100644
--- a/shib-service/src/main/java/net/shibboleth/shared/spring/service/impl/SpringServiceableComponent.java
+++ b/shib-service/src/main/java/net/shibboleth/shared/spring/service/impl/SpringServiceableComponent.java
@@ -17,6 +17,7 @@ package net.shibboleth.shared.spring.service.impl;
 import javax.annotation.Nonnull;
 
 import net.shibboleth.shared.component.IdentifiableComponent;
+import net.shibboleth.shared.primitive.LoggerFactory;
 import net.shibboleth.shared.service.ServiceableComponent;
 import net.shibboleth.shared.spring.service.AbstractServiceableComponent;
 
@@ -38,12 +39,18 @@ public class SpringServiceableComponent<T> extends AbstractServiceableComponent<
      */
     public SpringServiceableComponent(@Nonnull final T what) {
         theComponent = what;
+        String id = null;
         if (what instanceof IdentifiableComponent c) {
-            final String id = c.getId();
-            if (id != null) {
-                setId(id);
-            }
+            id = c.getId();
         }
+    	if (id != null) {
+            setId(id);
+        }
+    	else {
+    		final String generatedId = what.toString() + Long.toString(System.currentTimeMillis()); 
+    		LoggerFactory.getLogger(SpringServiceableComponent.class).debug("Generated id {} for object of type {}", what.getClass(), generatedId);
+    		setId(generatedId);
+    	}
     }
     
     /** {@inheritDoc} */
diff --git a/shib-service/src/test/java/net/shibboleth/shared/spring/service/impl/SpringServicableComponentTest.java b/shib-service/src/test/java/net/shibboleth/shared/spring/service/impl/SpringServicableComponentTest.java
new file mode 100644
index 00000000..803126aa
--- /dev/null
+++ b/shib-service/src/test/java/net/shibboleth/shared/spring/service/impl/SpringServicableComponentTest.java
@@ -0,0 +1,79 @@
+/*
+ * Licensed 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.shared.spring.service.impl;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.springframework.context.support.GenericApplicationContext;
+import org.testng.annotations.Test;
+
+import net.shibboleth.shared.component.ComponentInitializationException;
+import net.shibboleth.shared.component.IdentifiableComponent;
+
+public class SpringServicableComponentTest {
+
+	@Test
+	public void testIdNoName() throws ComponentInitializationException {
+		final MyComponent comp = new MyComponent();
+		try (final SpringServiceableComponent<MyComponent> serviceableComponent = new SpringServiceableComponent<>(comp)){
+			serviceableComponent.setApplicationContext(new GenericApplicationContext());
+			serviceableComponent.initialize();
+			serviceableComponent.pinComponent();
+			assertNotNull(serviceableComponent.getId());
+		}
+	}
+	
+	@Test
+	public void testIdName() throws ComponentInitializationException {
+		final MyComponent comp = new MyComponent();
+		comp.setId("Name");
+		try (final SpringServiceableComponent<MyComponent> serviceableComponent = new SpringServiceableComponent<>(comp)){
+			serviceableComponent.setApplicationContext(new GenericApplicationContext());			
+			serviceableComponent.initialize();
+			serviceableComponent.pinComponent();
+			assertEquals(serviceableComponent.getId(), "Name");
+
+		}
+	}
+	
+	@Test
+	public void testNoId() throws ComponentInitializationException {
+		try (final SpringServiceableComponent<Integer> serviceableComponent = new SpringServiceableComponent<>(42)){
+			serviceableComponent.setApplicationContext(new GenericApplicationContext());
+			serviceableComponent.initialize();
+			serviceableComponent.pinComponent();
+			assertNotNull(serviceableComponent.getId());
+		}
+	}
+	
+	static class MyComponent implements IdentifiableComponent {
+		private String id;
+		@Override
+		@Nullable
+		public String getId() {
+			return id;
+		}
+		
+		@Override
+		public void setId(@Nonnull String componentId) {
+			id = componentId;
+		}
+
+	}
+}

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


More information about the commits mailing list