[New] Added StrolchComponent for state of Strolch components

This commit is contained in:
Robert von Burg 2013-10-27 03:26:43 +01:00
parent f5dbc076d4
commit 3a0f37faad
12 changed files with 209 additions and 5 deletions

View File

@ -34,6 +34,10 @@
<groupId>li.strolch</groupId>
<artifactId>li.strolch.model</artifactId>
</dependency>
<dependency>
<groupId>ch.eitchnet</groupId>
<artifactId>ch.eitchnet.privilege</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,43 @@
package li.strolch.runtime.component;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import li.strolch.runtime.configuration.RuntimeConfiguration;
public class ComponentContainer {
private Map<Class<?>, StrolchComponent> componentMap;
public ComponentContainer() {
this.componentMap = new HashMap<>();
}
@SuppressWarnings("unchecked")
public <T extends StrolchComponent> T getComponent(Class<T> clazz) {
T component = (T) this.componentMap.get(clazz);
if (component == null) {
String msg = "The component does not exist for class {0}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, clazz);
throw new IllegalArgumentException(msg);
}
return component;
}
public void setup(RuntimeConfiguration configuration) {
}
public void start() {
}
public void stop() {
}
public void destroy() {
}
}

View File

@ -0,0 +1,6 @@
package li.strolch.runtime.component;
public enum ComponentState {
UNDEFINED, INITALIZED, STARTED, STOPPED, DESTROYED;
}

View File

@ -0,0 +1,67 @@
package li.strolch.runtime.component;
import java.text.MessageFormat;
import li.strolch.runtime.configuration.ComponentConfiguration;
public class StrolchComponent {
private ComponentState state;
public StrolchComponent() {
this.state = ComponentState.UNDEFINED;
}
public ComponentState getState() {
return this.state;
}
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) {
switch (this.state) {
case UNDEFINED:
if (newState != ComponentState.INITALIZED)
throw getIllegalStateEx(this.state, newState);
break;
case INITALIZED:
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;
}
public void initialize(ComponentConfiguration configuration) {
changeState(ComponentState.INITALIZED);
}
public void start() {
changeState(ComponentState.STARTED);
}
public void stop() {
changeState(ComponentState.STOPPED);
}
public void destroy() {
changeState(ComponentState.DESTROYED);
}
}

View File

@ -1,4 +1,4 @@
package li.strolch.runtime;
package li.strolch.runtime.configuration;
import java.text.MessageFormat;
import java.util.Map;

View File

@ -1,4 +1,4 @@
package li.strolch.runtime;
package li.strolch.runtime.configuration;
import java.util.Map;

View File

@ -1,5 +1,7 @@
package li.strolch.runtime;
package li.strolch.runtime.configuration;
import java.io.File;
import java.text.MessageFormat;
import java.util.Map;
public class RuntimeConfiguration extends AbstractionConfiguration {
@ -9,6 +11,12 @@ public class RuntimeConfiguration extends AbstractionConfiguration {
public RuntimeConfiguration(Map<String, String> configurationValues, String rootPath) {
super(RUNTIME, configurationValues);
File rootPathF = new File(rootPath);
if (!rootPathF.isDirectory() || !rootPathF.canRead()) {
String msg = "Root path is not readable at {0}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, rootPath);
throw new StrolchConfigurationException(msg);
}
this.rootPath = rootPath;
}

View File

@ -1,4 +1,4 @@
package li.strolch.runtime;
package li.strolch.runtime.configuration;
import java.text.MessageFormat;
import java.util.Map;

View File

@ -1,4 +1,4 @@
package li.strolch.runtime;
package li.strolch.runtime.configuration;
public class StrolchConfigurationException extends RuntimeException {

View File

@ -0,0 +1,42 @@
package li.strolch.runtime.privilege;
import java.io.File;
import li.strolch.runtime.component.ComponentState;
import li.strolch.runtime.component.StrolchComponent;
import li.strolch.runtime.configuration.ComponentConfiguration;
import ch.eitchnet.privilege.base.PrivilegeException;
import ch.eitchnet.privilege.handler.PrivilegeHandler;
import ch.eitchnet.privilege.helper.PrivilegeInitializationHelper;
import ch.eitchnet.privilege.model.Certificate;
public class StrolchPrivilegeHandler extends StrolchComponent {
private PrivilegeHandler privilegeHandler;
@Override
public void initialize(ComponentConfiguration configuration) {
super.initialize(configuration);
// initialize privilege
File privilegeConfigFile = new File(configuration.getRuntimeConfiguration().getRootPath()
+ "/config/Privilege.xml");
this.privilegeHandler = PrivilegeInitializationHelper.initializeFromXml(privilegeConfigFile);
}
public Certificate authenticate(String username, byte[] password) {
assertStarted();
return this.privilegeHandler.authenticate(username, password);
}
public void isCertificateValid(Certificate certificate) throws PrivilegeException {
assertStarted();
this.privilegeHandler.isCertificateValid(certificate);
}
private void assertStarted() {
if (getState() != ComponentState.STARTED)
throw new IllegalStateException("Component StrolchPrivilegeHandler is not yet started!"); //$NON-NLS-1$
}
}

View File

@ -0,0 +1,12 @@
package li.strolch.runtime;
import org.junit.Test;
public class ComponentContainerTest {
@Test
public void shouldStartContainer() {
}
}

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<StrolchConfiguration>
<Runtime>
<applicationName>StrolchRuntimeTest</applicationName>
<properties>
<rootPath>target/</rootPath>
</properties>
</Runtime>
<component>
<name>li.strolch.service.ServiceHandler</name>
<clazz>li.strolch.service.SimpleServiceHandler</clazz>
<properties>
</properties>
</component>
<component>
<name>li.strolch.persistence.api.StrolchPersistenceHandler</name>
<clazz>li.strolch.persistence.impl.XmlPersistenceHandler</clazz>
<properties>
<verbose>true</verbose>
</properties>
</component>
</StrolchConfiguration>