[Minor] Give component name, when state change invalid

This commit is contained in:
Robert von Burg 2017-02-17 13:37:35 +01:00
parent 28a7fbba17
commit 3770e7b65c
3 changed files with 20 additions and 20 deletions

View File

@ -21,7 +21,7 @@ public enum ComponentState {
UNDEFINED, SETUP, INITIALIZED, STARTED, STOPPED, DESTROYED; UNDEFINED, SETUP, INITIALIZED, STARTED, STOPPED, DESTROYED;
public ComponentState validateStateChange(ComponentState newState) { public ComponentState validateStateChange(ComponentState newState, String componentName) {
if (this == newState) if (this == newState)
return this; return this;
@ -29,35 +29,35 @@ public enum ComponentState {
switch (this) { switch (this) {
case UNDEFINED: case UNDEFINED:
if (newState != ComponentState.SETUP && newState != STOPPED) if (newState != ComponentState.SETUP && newState != STOPPED)
throw getIllegalStateEx(newState); throw getIllegalStateEx(newState, componentName);
break; break;
case SETUP: case SETUP:
if (newState != ComponentState.INITIALIZED && newState != STOPPED && newState != DESTROYED) if (newState != ComponentState.INITIALIZED && newState != STOPPED && newState != DESTROYED)
throw getIllegalStateEx(newState); throw getIllegalStateEx(newState, componentName);
break; break;
case INITIALIZED: case INITIALIZED:
if (newState != ComponentState.STARTED && newState != STOPPED && newState != DESTROYED) if (newState != ComponentState.STARTED && newState != STOPPED && newState != DESTROYED)
throw getIllegalStateEx(newState); throw getIllegalStateEx(newState, componentName);
break; break;
case STARTED: case STARTED:
if (newState != ComponentState.STOPPED) if (newState != ComponentState.STOPPED)
throw getIllegalStateEx(newState); throw getIllegalStateEx(newState, componentName);
break; break;
case STOPPED: case STOPPED:
if (newState != ComponentState.STARTED && newState != ComponentState.DESTROYED) if (newState != ComponentState.STARTED && newState != ComponentState.DESTROYED)
throw getIllegalStateEx(newState); throw getIllegalStateEx(newState, componentName);
break; break;
case DESTROYED: case DESTROYED:
throw getIllegalStateEx(newState); throw getIllegalStateEx(newState, componentName);
default: default:
throw getIllegalStateEx(newState); throw getIllegalStateEx(newState, componentName);
} }
return newState; return newState;
} }
private IllegalStateException getIllegalStateEx(ComponentState newState) { private IllegalStateException getIllegalStateEx(ComponentState newState, String componentName) {
String msg = "Moving from state {0} to state {1} is not allowed!"; //$NON-NLS-1$ String msg = "Moving from state {0} to state {1} is not allowed for component " + componentName; //$NON-NLS-1$
msg = MessageFormat.format(msg, this, newState); msg = MessageFormat.format(msg, this, newState);
throw new IllegalStateException(msg); throw new IllegalStateException(msg);
} }

View File

@ -136,7 +136,7 @@ public class StrolchComponent {
* @param configuration * @param configuration
*/ */
public void setup(ComponentConfiguration configuration) { public void setup(ComponentConfiguration configuration) {
this.state = this.state.validateStateChange(ComponentState.SETUP); this.state = this.state.validateStateChange(ComponentState.SETUP, getName());
} }
/** /**
@ -147,7 +147,7 @@ public class StrolchComponent {
*/ */
public void initialize(ComponentConfiguration configuration) throws Exception { public void initialize(ComponentConfiguration configuration) throws Exception {
this.configuration = configuration; this.configuration = configuration;
this.state = this.state.validateStateChange(ComponentState.INITIALIZED); this.state = this.state.validateStateChange(ComponentState.INITIALIZED, getName());
} }
/** /**
@ -157,7 +157,7 @@ public class StrolchComponent {
* @throws Exception * @throws Exception
*/ */
public void start() throws Exception { public void start() throws Exception {
this.state = this.state.validateStateChange(ComponentState.STARTED); this.state = this.state.validateStateChange(ComponentState.STARTED, getName());
} }
/** /**
@ -167,7 +167,7 @@ public class StrolchComponent {
* @throws Exception * @throws Exception
*/ */
public void stop() throws Exception { public void stop() throws Exception {
this.state = this.state.validateStateChange(ComponentState.STOPPED); this.state = this.state.validateStateChange(ComponentState.STOPPED, getName());
} }
/** /**
@ -177,7 +177,7 @@ public class StrolchComponent {
* @throws Exception * @throws Exception
*/ */
public void destroy() throws Exception { public void destroy() throws Exception {
this.state = this.state.validateStateChange(ComponentState.DESTROYED); this.state = this.state.validateStateChange(ComponentState.DESTROYED, getName());
} }
/** /**

View File

@ -178,7 +178,7 @@ public class ComponentContainerImpl implements ComponentContainer {
} }
public void setup(StrolchConfiguration strolchConfiguration) { public void setup(StrolchConfiguration strolchConfiguration) {
this.state.validateStateChange(ComponentState.SETUP); this.state.validateStateChange(ComponentState.SETUP, "agent");
// set the application locale // set the application locale
Locale.setDefault(strolchConfiguration.getRuntimeConfiguration().getLocale()); Locale.setDefault(strolchConfiguration.getRuntimeConfiguration().getLocale());
@ -220,7 +220,7 @@ public class ComponentContainerImpl implements ComponentContainer {
} }
public void initialize(StrolchConfiguration strolchConfiguration) { public void initialize(StrolchConfiguration strolchConfiguration) {
this.state.validateStateChange(ComponentState.INITIALIZED); this.state.validateStateChange(ComponentState.INITIALIZED, "agent");
// now we can initialize the components // now we can initialize the components
String msg = "{0}:{1} Initializing {2} Strolch Components..."; //$NON-NLS-1$ String msg = "{0}:{1} Initializing {2} Strolch Components..."; //$NON-NLS-1$
@ -238,7 +238,7 @@ public class ComponentContainerImpl implements ComponentContainer {
} }
public void start() { public void start() {
this.state.validateStateChange(ComponentState.STARTED); this.state.validateStateChange(ComponentState.STARTED, "agent");
String msg = "{0}:{1} Starting {2} Strolch Components..."; //$NON-NLS-1$ String msg = "{0}:{1} Starting {2} Strolch Components..."; //$NON-NLS-1$
String environment = getEnvironment(); String environment = getEnvironment();
@ -257,7 +257,7 @@ public class ComponentContainerImpl implements ComponentContainer {
} }
public void stop() { public void stop() {
this.state.validateStateChange(ComponentState.STOPPED); this.state.validateStateChange(ComponentState.STOPPED, "agent");
String msg = "{0}:{1} Stopping {2} Strolch Components..."; //$NON-NLS-1$ String msg = "{0}:{1} Stopping {2} Strolch Components..."; //$NON-NLS-1$
String environment = getEnvironment(); String environment = getEnvironment();
@ -278,7 +278,7 @@ public class ComponentContainerImpl implements ComponentContainer {
} }
public void destroy() { public void destroy() {
this.state.validateStateChange(ComponentState.DESTROYED); this.state.validateStateChange(ComponentState.DESTROYED, "agent");
String msg = "{0}:{1} Destroying {2} Strolch Components..."; //$NON-NLS-1$ String msg = "{0}:{1} Destroying {2} Strolch Components..."; //$NON-NLS-1$
String environment = getEnvironment(); String environment = getEnvironment();