[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.
This commit is contained in:
Robert von Burg 2013-11-13 19:52:49 +01:00
parent ff1b629c9d
commit defba45d02
4 changed files with 69 additions and 49 deletions

View File

@ -22,10 +22,17 @@ public class ComponentContainer {
private Map<Class<?>, StrolchComponent> componentMap;
private Map<String, ComponentController> 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> T getComponent(Class<T> clazz) {
@ -186,6 +193,7 @@ public class ComponentContainer {
Set<ComponentController> 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<ComponentController> 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<ComponentController> 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<ComponentController> 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()));

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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;
}