[java-support] 01/02: JSPT-98 Integrate lifecycle checking methods in base classes

Rod Widdowson rdw at steadingsoftware.com
Sun Jun 12 13:33:04 UTC 2022


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

rdw pushed a commit to branch dev/JSPT-98
in repository java-support.

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

commit 13ca2aaf47e817e8315cb6e298e8ac560c7df4ed
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Sat Jun 11 16:21:02 2022 +0100

    JSPT-98 Integrate lifecycle checking methods in base classes
    
    https://shibboleth.atlassian.net/browse/JSPT-98
    
    Add methods to abstract classes and deprecate ComponentSupport
---
 .../AbstractIdentifiedInitializableComponent.java  | 36 +++++++++++++++-
 .../component/AbstractInitializableComponent.java  | 49 +++++++++++++++++++++-
 .../java/support/component/ComponentSupport.java   |  1 +
 3 files changed, 84 insertions(+), 2 deletions(-)

diff --git a/src/main/java/net/shibboleth/utilities/java/support/component/AbstractIdentifiedInitializableComponent.java b/src/main/java/net/shibboleth/utilities/java/support/component/AbstractIdentifiedInitializableComponent.java
index 89c249c..eb48799 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/component/AbstractIdentifiedInitializableComponent.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/component/AbstractIdentifiedInitializableComponent.java
@@ -41,6 +41,40 @@ public abstract class AbstractIdentifiedInitializableComponent extends AbstractI
     @Nullable @NonnullAfterInit public synchronized String getId() {
         return id;
     }
+    
+    /**
+     * Checks if the component is destroyed and, if so, throws a {@link DestroyedComponentException}.
+     */
+    protected final void ifDestroyedThrowDestroyedComponentException() {
+        if (isDestroyed()) {
+            throw new DestroyedComponentException("Component '"
+                    + StringSupport.trimOrNull(getId())
+                    + "' has already been destroyed and can no longer be used.");
+        }
+    }
+
+    /**
+     * Checks if a component has not been initialized and, if so, throws a {@link UninitializedComponentException}.
+     */
+    protected final void ifNotInitializedThrowUninitializedComponentException() {
+        if (!isInitialized()) {
+            throw new UninitializedComponentException("Component '"
+                    + StringSupport.trimOrNull(getId())
+                    + "' has not yet been initialized and cannot be used.");
+        }
+    }
+
+    /**
+     * Checks if a component has been initialized and, if so, throws a {@link UnmodifiableComponentException}.
+     */
+    protected final void ifInitializedThrowUnmodifiabledComponentException() {
+        if (isInitialized()) {
+            throw new UnmodifiableComponentException("Component '"
+                    + StringSupport.trimOrNull(getId())
+                    + "' has already been initialized and can no longer be modified");
+        }
+    }
+
 
     /**
      * Sets the ID of this component. The component must not yet be initialized.
@@ -48,7 +82,7 @@ public abstract class AbstractIdentifiedInitializableComponent extends AbstractI
      * @param componentId ID of the component
      */
     protected synchronized void setId(@Nonnull @NotEmpty final String componentId) {
-        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+        throwSetterPreconditionExceptions();
 
         id = Constraint.isNotNull(StringSupport.trimOrNull(componentId), "Component ID can not be null or empty");
     }
diff --git a/src/main/java/net/shibboleth/utilities/java/support/component/AbstractInitializableComponent.java b/src/main/java/net/shibboleth/utilities/java/support/component/AbstractInitializableComponent.java
index 32529cf..642f528 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/component/AbstractInitializableComponent.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/component/AbstractInitializableComponent.java
@@ -57,7 +57,7 @@ public abstract class AbstractInitializableComponent implements DestructableComp
     /** {@inheritDoc} */
     @Override
     public final synchronized void initialize() throws ComponentInitializationException {
-        ComponentSupport.ifDestroyedThrowDestroyedComponentException(this);
+        ifDestroyedThrowDestroyedComponentException();
         if (isInitialized()) {
             return;
         }
@@ -65,6 +65,53 @@ public abstract class AbstractInitializableComponent implements DestructableComp
         doInitialize();
         isInitialized = true;
     }
+    
+    /**
+     * Checks if the component is destroyed and, if so, throws a {@link DestroyedComponentException}.
+     */
+    protected void ifDestroyedThrowDestroyedComponentException() {
+        if (isDestroyed()) {
+            throw new DestroyedComponentException(
+                    "Unidentified Component has already been destroyed and can no longer be used.");
+        }
+    }
+
+    /**
+     * Checks if a component has not been initialized and, if so, throws a {@link UninitializedComponentException}.
+     */
+    protected void ifNotInitializedThrowUninitializedComponentException() {
+        if (!isInitialized()) {
+            throw new UninitializedComponentException(
+                    "Unidentified Component has not yet been initialized and cannot be used.");
+        }
+    }
+
+    /**
+     * Checks if a component has been initialized and, if so, throws a {@link UnmodifiableComponentException}.
+     */
+    protected void ifInitializedThrowUnmodifiabledComponentException() {
+        if (isInitialized()) {
+            throw new UnmodifiableComponentException(
+                    "Unidentified Component has already been initialized and can no longer be modified");
+        }
+    }
+
+    /**
+     * Helper for a setter method to check the standard preconditions.
+     */
+    protected final void throwSetterPreconditionExceptions() {
+        ifDestroyedThrowDestroyedComponentException();
+        ifInitializedThrowUnmodifiabledComponentException();
+    }
+
+    /**
+     * Helper for any method to throw appropriate exceptions if we are either
+     * not initialized, or have been destroyed.
+     */
+    protected final void throwComponentStateExceptions() {
+        ifDestroyedThrowDestroyedComponentException();
+        ifNotInitializedThrowUninitializedComponentException();
+    }
 
     /**
      * Performs component specific destruction logic. This method is executed within the lock on the object being
diff --git a/src/main/java/net/shibboleth/utilities/java/support/component/ComponentSupport.java b/src/main/java/net/shibboleth/utilities/java/support/component/ComponentSupport.java
index e52a982..e1a5dfc 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/component/ComponentSupport.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/component/ComponentSupport.java
@@ -24,6 +24,7 @@ import net.shibboleth.utilities.java.support.logic.Constraint;
 import net.shibboleth.utilities.java.support.primitive.StringSupport;
 
 /** Support class for working with {@link Component} objects. */
+ at Deprecated
 public final class ComponentSupport {
 
     /** Constructor. */

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


More information about the commits mailing list