[java-support] 01/03: JSPT-98 Integrate lifecycle checking methods in base classes
Rod Widdowson
rdw at steadingsoftware.com
Mon Jul 11 15:37:35 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=98beeb63eaf6d50a7f8b2fddfaf7340386458008
commit 98beeb63eaf6d50a7f8b2fddfaf7340386458008
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 the equivalent static
ones in ComponentSupport.
Leave the 'initialize' and 'destroy' static methods since the have
some utility for classes which do (or may) not derive from our
base classes.
---
.../AbstractIdentifiedInitializableComponent.java | 36 +++++++++++++++-
.../component/AbstractInitializableComponent.java | 49 +++++++++++++++++++++-
.../java/support/component/ComponentSupport.java | 3 ++
3 files changed, 86 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..eded23e 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
@@ -76,6 +76,7 @@ public final class ComponentSupport {
*
* @param component component to check
*/
+ @Deprecated
public static void ifDestroyedThrowDestroyedComponentException(@Nonnull final DestructableComponent component) {
Constraint.isNotNull(component, "Component cannot be null");
@@ -97,6 +98,7 @@ public final class ComponentSupport {
*
* @param component component to check
*/
+ @Deprecated
public static void
ifNotInitializedThrowUninitializedComponentException(@Nonnull final InitializableComponent component) {
Constraint.isNotNull(component, "Component cannot be null");
@@ -118,6 +120,7 @@ public final class ComponentSupport {
*
* @param component component to check
*/
+ @Deprecated
public static void
ifInitializedThrowUnmodifiabledComponentException(@Nonnull final InitializableComponent component) {
Constraint.isNotNull(component, "Component cannot be null");
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list