From defba45d02190433ca0c593ded3b954566e9a2ca Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 13 Nov 2013 19:52:49 +0100 Subject: [PATCH] [New] added state to the container - checked by PrivilegeHandler The container how sets its own state once it has changed the state of the components. Now the DefaultStrolchPrivilegeHandler checks that the container has started before allowing any methods to be called. This might still be too restrictive in that when the container is stopped, that then an admin can not log in and perform any actions, but this can then be changed when needed. --- .../runtime/component/ComponentContainer.java | 15 +++++- .../runtime/component/ComponentState.java | 39 ++++++++++++++ .../runtime/component/StrolchComponent.java | 52 ++++--------------- .../DefaultStrolchPrivilegeHandler.java | 12 ++--- 4 files changed, 69 insertions(+), 49 deletions(-) diff --git a/src/main/java/li/strolch/runtime/component/ComponentContainer.java b/src/main/java/li/strolch/runtime/component/ComponentContainer.java index b878ba048..bb80429c8 100644 --- a/src/main/java/li/strolch/runtime/component/ComponentContainer.java +++ b/src/main/java/li/strolch/runtime/component/ComponentContainer.java @@ -22,10 +22,17 @@ public class ComponentContainer { private Map, StrolchComponent> componentMap; private Map controllerMap; - private ComponentDependencyAnalyzer dependencyAnalyzer; - private StrolchConfiguration strolchConfiguration; + private ComponentState state; + + public ComponentContainer() { + this.state = ComponentState.UNDEFINED; + } + + public ComponentState getState() { + return this.state; + } @SuppressWarnings("unchecked") public T getComponent(Class clazz) { @@ -186,6 +193,7 @@ public class ComponentContainer { Set rootUpstreamComponents = this.dependencyAnalyzer.findRootUpstreamComponents(); initialize(rootUpstreamComponents); + this.state = this.state.validateStateChange(ComponentState.INITIALIZED); String msg = "All {0} Strolch Components have been initialized."; //$NON-NLS-1$ logger.info(MessageFormat.format(msg, this.controllerMap.size())); @@ -197,6 +205,7 @@ public class ComponentContainer { Set rootUpstreamComponents = this.dependencyAnalyzer.findRootUpstreamComponents(); start(rootUpstreamComponents); + this.state = this.state.validateStateChange(ComponentState.STARTED); String msg = "All {0} Strolch Components started and container now ready to be used. Have fun =))"; //$NON-NLS-1$ logger.info(MessageFormat.format(msg, this.controllerMap.size())); @@ -208,6 +217,7 @@ public class ComponentContainer { Set rootUpstreamComponents = this.dependencyAnalyzer.findRootDownstreamComponents(); stop(rootUpstreamComponents); + this.state = this.state.validateStateChange(ComponentState.STOPPED); String msg = "All {0} Strolch Components have been stopped."; //$NON-NLS-1$ logger.info(MessageFormat.format(msg, this.controllerMap.size())); @@ -219,6 +229,7 @@ public class ComponentContainer { Set rootUpstreamComponents = this.dependencyAnalyzer.findRootDownstreamComponents(); destroy(rootUpstreamComponents); + this.state = this.state.validateStateChange(ComponentState.DESTROYED); String msg = "All {0} Strolch Components have been destroyed!"; //$NON-NLS-1$ logger.info(MessageFormat.format(msg, this.controllerMap.size())); diff --git a/src/main/java/li/strolch/runtime/component/ComponentState.java b/src/main/java/li/strolch/runtime/component/ComponentState.java index ca1e11c86..ed8226450 100644 --- a/src/main/java/li/strolch/runtime/component/ComponentState.java +++ b/src/main/java/li/strolch/runtime/component/ComponentState.java @@ -1,6 +1,45 @@ package li.strolch.runtime.component; +import java.text.MessageFormat; + public enum ComponentState { UNDEFINED, INITIALIZED, STARTED, STOPPED, DESTROYED; + + public ComponentState validateStateChange(ComponentState newState) { + + if (this == newState) + return this; + + switch (this) { + case UNDEFINED: + if (newState != ComponentState.INITIALIZED) + throw getIllegalStateEx(newState); + break; + case INITIALIZED: + if (newState != ComponentState.STARTED) + throw getIllegalStateEx(newState); + break; + case STARTED: + if (newState != ComponentState.STOPPED) + throw getIllegalStateEx(newState); + break; + case STOPPED: + if (newState != ComponentState.STARTED && newState != ComponentState.DESTROYED) + throw getIllegalStateEx(newState); + break; + case DESTROYED: + throw getIllegalStateEx(newState); + default: + throw getIllegalStateEx(newState); + } + + return newState; + } + + private IllegalStateException getIllegalStateEx(ComponentState newState) { + String msg = "Moving from state {0} to state {1} is not allowed!"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, this, newState); + throw new IllegalStateException(msg); + } } diff --git a/src/main/java/li/strolch/runtime/component/StrolchComponent.java b/src/main/java/li/strolch/runtime/component/StrolchComponent.java index 953a130bf..9c5a32586 100644 --- a/src/main/java/li/strolch/runtime/component/StrolchComponent.java +++ b/src/main/java/li/strolch/runtime/component/StrolchComponent.java @@ -31,43 +31,6 @@ public class StrolchComponent { return this.container; } - private IllegalStateException getIllegalStateEx(ComponentState currentState, ComponentState newState) { - String msg = "Moving from state {0} to state {1} is not allowed!"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, currentState, newState); - throw new IllegalStateException(msg); - } - - protected void changeState(ComponentState newState) { - - if (this.state == newState) - return; - - switch (this.state) { - case UNDEFINED: - if (newState != ComponentState.INITIALIZED) - throw getIllegalStateEx(this.state, newState); - break; - case INITIALIZED: - if (newState != ComponentState.STARTED) - throw getIllegalStateEx(this.state, newState); - break; - case STARTED: - if (newState != ComponentState.STOPPED) - throw getIllegalStateEx(this.state, newState); - break; - case STOPPED: - if (newState != ComponentState.STARTED && newState != ComponentState.DESTROYED) - throw getIllegalStateEx(this.state, newState); - break; - case DESTROYED: - throw getIllegalStateEx(this.state, newState); - default: - throw getIllegalStateEx(this.state, newState); - } - - this.state = newState; - } - protected void assertStarted() { if (getState() != ComponentState.STARTED) { String msg = "Component {0} is not yet started!"; //$NON-NLS-1$ @@ -75,19 +38,26 @@ public class StrolchComponent { } } + protected void assertContainerStarted() { + if (getContainer().getState() != ComponentState.STARTED) { + String msg = "Container is not yet started!"; //$NON-NLS-1$ + throw new IllegalStateException(msg); + } + } + public void initialize(ComponentConfiguration configuration) { - changeState(ComponentState.INITIALIZED); + this.state = this.state.validateStateChange(ComponentState.INITIALIZED); } public void start() { - changeState(ComponentState.STARTED); + this.state = this.state.validateStateChange(ComponentState.STARTED); } public void stop() { - changeState(ComponentState.STOPPED); + this.state = this.state.validateStateChange(ComponentState.STOPPED); } public void destroy() { - changeState(ComponentState.DESTROYED); + this.state = this.state.validateStateChange(ComponentState.DESTROYED); } } diff --git a/src/main/java/li/strolch/runtime/privilege/DefaultStrolchPrivilegeHandler.java b/src/main/java/li/strolch/runtime/privilege/DefaultStrolchPrivilegeHandler.java index 0428bb8c6..1977e0060 100644 --- a/src/main/java/li/strolch/runtime/privilege/DefaultStrolchPrivilegeHandler.java +++ b/src/main/java/li/strolch/runtime/privilege/DefaultStrolchPrivilegeHandler.java @@ -43,7 +43,7 @@ public class DefaultStrolchPrivilegeHandler extends StrolchComponent implements */ @Override public Certificate authenticate(String username, byte[] password) { - assertStarted(); + assertContainerStarted(); return this.privilegeHandler.authenticate(username, password); } @@ -54,7 +54,7 @@ public class DefaultStrolchPrivilegeHandler extends StrolchComponent implements */ @Override public void isCertificateValid(Certificate certificate) throws PrivilegeException { - assertStarted(); + assertContainerStarted(); this.privilegeHandler.isCertificateValid(certificate); } @@ -65,7 +65,7 @@ public class DefaultStrolchPrivilegeHandler extends StrolchComponent implements */ @Override public boolean invalidateSession(Certificate certificate) { - assertStarted(); + assertContainerStarted(); return this.privilegeHandler.invalidateSession(certificate); } @@ -77,7 +77,7 @@ public class DefaultStrolchPrivilegeHandler extends StrolchComponent implements */ @Override public PrivilegeContext getPrivilegeContext(Certificate certificate) throws PrivilegeException { - assertStarted(); + assertContainerStarted(); return this.privilegeHandler.getPrivilegeContext(certificate); } @@ -90,13 +90,13 @@ public class DefaultStrolchPrivilegeHandler extends StrolchComponent implements */ @Override public void runAsSystem(String systemUsername, SystemUserAction action) throws PrivilegeException { - assertStarted(); + assertContainerStarted(); this.privilegeHandler.runAsSystem(systemUsername, action); } @Override public PrivilegeHandler getPrivilegeHandler(Certificate certificate) throws PrivilegeException { - assertStarted(); + assertContainerStarted(); this.privilegeHandler.assertIsPrivilegeAdmin(certificate); return this.privilegeHandler; }