[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:
parent
ff1b629c9d
commit
defba45d02
|
@ -22,10 +22,17 @@ public class ComponentContainer {
|
||||||
|
|
||||||
private Map<Class<?>, StrolchComponent> componentMap;
|
private Map<Class<?>, StrolchComponent> componentMap;
|
||||||
private Map<String, ComponentController> controllerMap;
|
private Map<String, ComponentController> controllerMap;
|
||||||
|
|
||||||
private ComponentDependencyAnalyzer dependencyAnalyzer;
|
private ComponentDependencyAnalyzer dependencyAnalyzer;
|
||||||
|
|
||||||
private StrolchConfiguration strolchConfiguration;
|
private StrolchConfiguration strolchConfiguration;
|
||||||
|
private ComponentState state;
|
||||||
|
|
||||||
|
public ComponentContainer() {
|
||||||
|
this.state = ComponentState.UNDEFINED;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ComponentState getState() {
|
||||||
|
return this.state;
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T> T getComponent(Class<T> clazz) {
|
public <T> T getComponent(Class<T> clazz) {
|
||||||
|
@ -186,6 +193,7 @@ public class ComponentContainer {
|
||||||
|
|
||||||
Set<ComponentController> rootUpstreamComponents = this.dependencyAnalyzer.findRootUpstreamComponents();
|
Set<ComponentController> rootUpstreamComponents = this.dependencyAnalyzer.findRootUpstreamComponents();
|
||||||
initialize(rootUpstreamComponents);
|
initialize(rootUpstreamComponents);
|
||||||
|
this.state = this.state.validateStateChange(ComponentState.INITIALIZED);
|
||||||
|
|
||||||
String msg = "All {0} Strolch Components have been initialized."; //$NON-NLS-1$
|
String msg = "All {0} Strolch Components have been initialized."; //$NON-NLS-1$
|
||||||
logger.info(MessageFormat.format(msg, this.controllerMap.size()));
|
logger.info(MessageFormat.format(msg, this.controllerMap.size()));
|
||||||
|
@ -197,6 +205,7 @@ public class ComponentContainer {
|
||||||
|
|
||||||
Set<ComponentController> rootUpstreamComponents = this.dependencyAnalyzer.findRootUpstreamComponents();
|
Set<ComponentController> rootUpstreamComponents = this.dependencyAnalyzer.findRootUpstreamComponents();
|
||||||
start(rootUpstreamComponents);
|
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$
|
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()));
|
logger.info(MessageFormat.format(msg, this.controllerMap.size()));
|
||||||
|
@ -208,6 +217,7 @@ public class ComponentContainer {
|
||||||
|
|
||||||
Set<ComponentController> rootUpstreamComponents = this.dependencyAnalyzer.findRootDownstreamComponents();
|
Set<ComponentController> rootUpstreamComponents = this.dependencyAnalyzer.findRootDownstreamComponents();
|
||||||
stop(rootUpstreamComponents);
|
stop(rootUpstreamComponents);
|
||||||
|
this.state = this.state.validateStateChange(ComponentState.STOPPED);
|
||||||
|
|
||||||
String msg = "All {0} Strolch Components have been stopped."; //$NON-NLS-1$
|
String msg = "All {0} Strolch Components have been stopped."; //$NON-NLS-1$
|
||||||
logger.info(MessageFormat.format(msg, this.controllerMap.size()));
|
logger.info(MessageFormat.format(msg, this.controllerMap.size()));
|
||||||
|
@ -219,6 +229,7 @@ public class ComponentContainer {
|
||||||
|
|
||||||
Set<ComponentController> rootUpstreamComponents = this.dependencyAnalyzer.findRootDownstreamComponents();
|
Set<ComponentController> rootUpstreamComponents = this.dependencyAnalyzer.findRootDownstreamComponents();
|
||||||
destroy(rootUpstreamComponents);
|
destroy(rootUpstreamComponents);
|
||||||
|
this.state = this.state.validateStateChange(ComponentState.DESTROYED);
|
||||||
|
|
||||||
String msg = "All {0} Strolch Components have been destroyed!"; //$NON-NLS-1$
|
String msg = "All {0} Strolch Components have been destroyed!"; //$NON-NLS-1$
|
||||||
logger.info(MessageFormat.format(msg, this.controllerMap.size()));
|
logger.info(MessageFormat.format(msg, this.controllerMap.size()));
|
||||||
|
|
|
@ -1,6 +1,45 @@
|
||||||
package li.strolch.runtime.component;
|
package li.strolch.runtime.component;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
public enum ComponentState {
|
public enum ComponentState {
|
||||||
|
|
||||||
UNDEFINED, INITIALIZED, STARTED, STOPPED, DESTROYED;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,43 +31,6 @@ public class StrolchComponent {
|
||||||
return this.container;
|
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() {
|
protected void assertStarted() {
|
||||||
if (getState() != ComponentState.STARTED) {
|
if (getState() != ComponentState.STARTED) {
|
||||||
String msg = "Component {0} is not yet started!"; //$NON-NLS-1$
|
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) {
|
public void initialize(ComponentConfiguration configuration) {
|
||||||
changeState(ComponentState.INITIALIZED);
|
this.state = this.state.validateStateChange(ComponentState.INITIALIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
changeState(ComponentState.STARTED);
|
this.state = this.state.validateStateChange(ComponentState.STARTED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
changeState(ComponentState.STOPPED);
|
this.state = this.state.validateStateChange(ComponentState.STOPPED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
changeState(ComponentState.DESTROYED);
|
this.state = this.state.validateStateChange(ComponentState.DESTROYED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,7 @@ public class DefaultStrolchPrivilegeHandler extends StrolchComponent implements
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Certificate authenticate(String username, byte[] password) {
|
public Certificate authenticate(String username, byte[] password) {
|
||||||
assertStarted();
|
assertContainerStarted();
|
||||||
return this.privilegeHandler.authenticate(username, password);
|
return this.privilegeHandler.authenticate(username, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ public class DefaultStrolchPrivilegeHandler extends StrolchComponent implements
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void isCertificateValid(Certificate certificate) throws PrivilegeException {
|
public void isCertificateValid(Certificate certificate) throws PrivilegeException {
|
||||||
assertStarted();
|
assertContainerStarted();
|
||||||
this.privilegeHandler.isCertificateValid(certificate);
|
this.privilegeHandler.isCertificateValid(certificate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ public class DefaultStrolchPrivilegeHandler extends StrolchComponent implements
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean invalidateSession(Certificate certificate) {
|
public boolean invalidateSession(Certificate certificate) {
|
||||||
assertStarted();
|
assertContainerStarted();
|
||||||
return this.privilegeHandler.invalidateSession(certificate);
|
return this.privilegeHandler.invalidateSession(certificate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ public class DefaultStrolchPrivilegeHandler extends StrolchComponent implements
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public PrivilegeContext getPrivilegeContext(Certificate certificate) throws PrivilegeException {
|
public PrivilegeContext getPrivilegeContext(Certificate certificate) throws PrivilegeException {
|
||||||
assertStarted();
|
assertContainerStarted();
|
||||||
return this.privilegeHandler.getPrivilegeContext(certificate);
|
return this.privilegeHandler.getPrivilegeContext(certificate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,13 +90,13 @@ public class DefaultStrolchPrivilegeHandler extends StrolchComponent implements
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void runAsSystem(String systemUsername, SystemUserAction action) throws PrivilegeException {
|
public void runAsSystem(String systemUsername, SystemUserAction action) throws PrivilegeException {
|
||||||
assertStarted();
|
assertContainerStarted();
|
||||||
this.privilegeHandler.runAsSystem(systemUsername, action);
|
this.privilegeHandler.runAsSystem(systemUsername, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PrivilegeHandler getPrivilegeHandler(Certificate certificate) throws PrivilegeException {
|
public PrivilegeHandler getPrivilegeHandler(Certificate certificate) throws PrivilegeException {
|
||||||
assertStarted();
|
assertContainerStarted();
|
||||||
this.privilegeHandler.assertIsPrivilegeAdmin(certificate);
|
this.privilegeHandler.assertIsPrivilegeAdmin(certificate);
|
||||||
return this.privilegeHandler;
|
return this.privilegeHandler;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue