From 6aaf7a5364cd03f72c6b28984d51812514532adc Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Tue, 24 Dec 2013 16:51:31 +0100 Subject: [PATCH] [Minor] better error handling when the agent does not start Should the agent not be able to start due to configuration problems, then the agent must still be stopped, thus state changes in this context should not lead to exceptions when shutting down. --- .../runtime/agent/ComponentContainerImpl.java | 35 ++++++++++++------- .../strolch/runtime/agent/ComponentState.java | 4 +-- .../strolch/runtime/agent/StrolchAgent.java | 6 ++-- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/main/java/li/strolch/runtime/agent/ComponentContainerImpl.java b/src/main/java/li/strolch/runtime/agent/ComponentContainerImpl.java index 94c229d45..30f1747a6 100644 --- a/src/main/java/li/strolch/runtime/agent/ComponentContainerImpl.java +++ b/src/main/java/li/strolch/runtime/agent/ComponentContainerImpl.java @@ -257,25 +257,36 @@ public class ComponentContainerImpl implements ComponentContainer { logger.info("Stopping strolch components..."); //$NON-NLS-1$ - Set rootUpstreamComponents = this.dependencyAnalyzer.findRootDownstreamComponents(); - stop(rootUpstreamComponents); - this.state = this.state.validateStateChange(ComponentState.STOPPED); + if (this.dependencyAnalyzer == null) { + logger.info("Strolch Components have been stopped."); //$NON-NLS-1$ + } else { + 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())); + String msg = "All {0} Strolch Components have been stopped."; //$NON-NLS-1$ + logger.info(MessageFormat.format(msg, this.controllerMap.size())); + } } public void destroy() { logger.info("Destroying strolch components..."); //$NON-NLS-1$ - Set rootUpstreamComponents = this.dependencyAnalyzer.findRootDownstreamComponents(); - destroy(rootUpstreamComponents); - this.state = this.state.validateStateChange(ComponentState.DESTROYED); + if (this.dependencyAnalyzer == null) { + logger.info("Strolch Components have been destroyed."); //$NON-NLS-1$ + } else { + 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())); - this.controllerMap.clear(); - this.componentMap.clear(); + String msg = "All {0} Strolch Components have been destroyed!"; //$NON-NLS-1$ + logger.info(MessageFormat.format(msg, this.controllerMap.size())); + this.controllerMap.clear(); + this.componentMap.clear(); + } + + this.controllerMap = null; + this.componentMap = null; } } diff --git a/src/main/java/li/strolch/runtime/agent/ComponentState.java b/src/main/java/li/strolch/runtime/agent/ComponentState.java index 5a34de2db..12c96beb0 100644 --- a/src/main/java/li/strolch/runtime/agent/ComponentState.java +++ b/src/main/java/li/strolch/runtime/agent/ComponentState.java @@ -28,11 +28,11 @@ public enum ComponentState { switch (this) { case UNDEFINED: - if (newState != ComponentState.INITIALIZED) + if (newState != ComponentState.INITIALIZED && newState != STOPPED) throw getIllegalStateEx(newState); break; case INITIALIZED: - if (newState != ComponentState.STARTED) + if (newState != ComponentState.STARTED && newState != STOPPED) throw getIllegalStateEx(newState); break; case STARTED: diff --git a/src/main/java/li/strolch/runtime/agent/StrolchAgent.java b/src/main/java/li/strolch/runtime/agent/StrolchAgent.java index 6215ed29e..80e2395d7 100644 --- a/src/main/java/li/strolch/runtime/agent/StrolchAgent.java +++ b/src/main/java/li/strolch/runtime/agent/StrolchAgent.java @@ -66,11 +66,13 @@ public class StrolchAgent { } public void stop() { - this.container.stop(); + if (this.container != null) + this.container.stop(); } public void destroy() { - this.container.destroy(); + if (this.container != null) + this.container.destroy(); } public void setup(File path) {