[New] Implemented separate DataStoreMode for each StrolchRealm

added new component state mode SETUP which is before initialize which is
where configurations can be loaded, but no other components may be used
This commit is contained in:
Robert von Burg 2014-02-19 00:09:25 +01:00
parent 14de9b29df
commit d6ebf330c7
6 changed files with 35 additions and 10 deletions

View File

@ -19,7 +19,7 @@ import java.text.MessageFormat;
public enum ComponentState {
UNDEFINED, INITIALIZED, STARTED, STOPPED, DESTROYED;
UNDEFINED, SETUP, INITIALIZED, STARTED, STOPPED, DESTROYED;
public ComponentState validateStateChange(ComponentState newState) {
@ -28,6 +28,10 @@ public enum ComponentState {
switch (this) {
case UNDEFINED:
if (newState != ComponentState.SETUP && newState != STOPPED)
throw getIllegalStateEx(newState);
break;
case SETUP:
if (newState != ComponentState.INITIALIZED && newState != STOPPED)
throw getIllegalStateEx(newState);
break;

View File

@ -90,6 +90,8 @@ public class StrolchAgent {
this.strolchConfiguration = ConfigurationParser.parseConfiguration(path);
ComponentContainerImpl container = new ComponentContainerImpl(this);
container.setup(strolchConfiguration);
this.container = container;
RuntimeConfiguration runtimeConfiguration = this.strolchConfiguration.getRuntimeConfiguration();

View File

@ -69,6 +69,10 @@ public class StrolchComponent {
}
}
public void setup(ComponentConfiguration configuration) {
this.state = this.state.validateStateChange(ComponentState.SETUP);
}
public void initialize(ComponentConfiguration configuration) {
this.state = this.state.validateStateChange(ComponentState.INITIALIZED);
}

View File

@ -93,7 +93,7 @@ public class ComponentContainerImpl implements ComponentContainer {
return getComponent(RealmHandler.class).getRealm(realm);
}
private void initializeComponent(Map<Class<?>, StrolchComponent> componentMap,
private void setupComponent(Map<Class<?>, StrolchComponent> componentMap,
Map<String, ComponentController> controllerMap, ComponentConfiguration componentConfiguration) {
String componentName = componentConfiguration.getName();
@ -120,6 +120,7 @@ public class ComponentContainerImpl implements ComponentContainer {
Constructor<StrolchComponent> constructor = strolchComponentClass.getConstructor(ComponentContainer.class,
String.class);
StrolchComponent strolchComponent = constructor.newInstance(this, componentName);
strolchComponent.setup(componentConfiguration);
componentMap.put(apiClass, strolchComponent);
controllerMap.put(componentName, new ComponentController(strolchComponent));
@ -226,7 +227,7 @@ public class ComponentContainerImpl implements ComponentContainer {
destroy(dependencies);
}
public void initialize(StrolchConfiguration strolchConfiguration) {
public void setup(StrolchConfiguration strolchConfiguration) {
// first set up the container itself
this.strolchConfiguration = strolchConfiguration;
@ -236,7 +237,7 @@ public class ComponentContainerImpl implements ComponentContainer {
for (String componentName : componentNames) {
ComponentConfiguration componentConfiguration = strolchConfiguration
.getComponentConfiguration(componentName);
initializeComponent(componentMap, controllerMap, componentConfiguration);
setupComponent(componentMap, controllerMap, componentConfiguration);
}
// then analyze dependencies
@ -248,8 +249,12 @@ public class ComponentContainerImpl implements ComponentContainer {
this.controllerMap = controllerMap;
this.strolchConfiguration = strolchConfiguration;
this.state = this.state.validateStateChange(ComponentState.SETUP);
String msg = "Strolch Container setup with {0} components."; //$NON-NLS-1$
logger.info(MessageFormat.format(msg, this.componentMap.size()));
}
public void initialize(StrolchConfiguration strolchConfiguration) {
// now we can initialize the components
logger.info("Initializing strolch components..."); //$NON-NLS-1$
@ -258,7 +263,7 @@ public class ComponentContainerImpl implements ComponentContainer {
initialize(rootUpstreamComponents);
this.state = this.state.validateStateChange(ComponentState.INITIALIZED);
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()));
}

View File

@ -67,7 +67,7 @@ public class DefaultRealmHandler extends StrolchComponent implements RealmHandle
}
@Override
public void initialize(ComponentConfiguration configuration) {
public void setup(ComponentConfiguration configuration) {
this.realms = new HashMap<>();
String[] realms = configuration.getStringArray(PROP_REALMS, StrolchConstants.DEFAULT_REALM);
@ -81,6 +81,11 @@ public class DefaultRealmHandler extends StrolchComponent implements RealmHandle
StrolchRealm realm = dataStoreMode.createRealm(realmName);
this.realms.put(realmName, realm);
}
super.setup(configuration);
}
@Override
public void initialize(ComponentConfiguration configuration) {
for (String realmName : this.realms.keySet()) {
StrolchRealm realm = this.realms.get(realmName);

View File

@ -69,7 +69,7 @@ public class InMemoryTransaction extends AbstractTransaction {
public void autoCloseableCommit() {
if (logger.isDebugEnabled()) {
logger.info("Committing TX..."); //$NON-NLS-1$
logger.info("Committing TX for realm " + getRealmName() + "..."); //$NON-NLS-1$
}
long start = System.nanoTime();
@ -86,13 +86,16 @@ public class InMemoryTransaction extends AbstractTransaction {
long txDuration = end - this.startTime;
long closeDuration = end - start;
StringBuilder sb = new StringBuilder();
sb.append("TX has failed after "); //$NON-NLS-1$
sb.append("TX");
sb.append(getRealmName());
sb.append(" has failed after "); //$NON-NLS-1$
sb.append(StringHelper.formatNanoDuration(txDuration));
sb.append(" with close operation taking "); //$NON-NLS-1$
sb.append(StringHelper.formatNanoDuration(closeDuration));
logger.info(sb.toString());
throw new StrolchPersistenceException("Strolch Transaction failed due to " + e.getMessage(), e); //$NON-NLS-1$
throw new StrolchPersistenceException(
"Strolch Transaction for realm " + getRealmName() + " failed due to " + e.getMessage(), e); //$NON-NLS-1$
}
@ -106,7 +109,9 @@ public class InMemoryTransaction extends AbstractTransaction {
this.txResult.setRealm(getRealm().getRealm());
StringBuilder sb = new StringBuilder();
sb.append("TX was completed after "); //$NON-NLS-1$
sb.append("TX for realm ");
sb.append(getRealmName());
sb.append(" was completed after "); //$NON-NLS-1$
sb.append(StringHelper.formatNanoDuration(txDuration));
sb.append(" with close operation taking "); //$NON-NLS-1$
sb.append(StringHelper.formatNanoDuration(closeDuration));