From d6ebf330c755b8151005b304a8a091ddc21da14d Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 19 Feb 2014 00:09:25 +0100 Subject: [PATCH] [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 --- .../java/li/strolch/agent/api/ComponentState.java | 6 +++++- .../java/li/strolch/agent/api/StrolchAgent.java | 2 ++ .../java/li/strolch/agent/api/StrolchComponent.java | 4 ++++ .../strolch/agent/impl/ComponentContainerImpl.java | 13 +++++++++---- .../li/strolch/agent/impl/DefaultRealmHandler.java | 7 ++++++- .../persistence/inmemory/InMemoryTransaction.java | 13 +++++++++---- 6 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/main/java/li/strolch/agent/api/ComponentState.java b/src/main/java/li/strolch/agent/api/ComponentState.java index 53b3825cc..118896587 100644 --- a/src/main/java/li/strolch/agent/api/ComponentState.java +++ b/src/main/java/li/strolch/agent/api/ComponentState.java @@ -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; diff --git a/src/main/java/li/strolch/agent/api/StrolchAgent.java b/src/main/java/li/strolch/agent/api/StrolchAgent.java index 4467eacdb..ed51697a5 100644 --- a/src/main/java/li/strolch/agent/api/StrolchAgent.java +++ b/src/main/java/li/strolch/agent/api/StrolchAgent.java @@ -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(); diff --git a/src/main/java/li/strolch/agent/api/StrolchComponent.java b/src/main/java/li/strolch/agent/api/StrolchComponent.java index d34d4c1ff..7052a832d 100644 --- a/src/main/java/li/strolch/agent/api/StrolchComponent.java +++ b/src/main/java/li/strolch/agent/api/StrolchComponent.java @@ -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); } diff --git a/src/main/java/li/strolch/agent/impl/ComponentContainerImpl.java b/src/main/java/li/strolch/agent/impl/ComponentContainerImpl.java index 21af41496..3e4f8a2a9 100644 --- a/src/main/java/li/strolch/agent/impl/ComponentContainerImpl.java +++ b/src/main/java/li/strolch/agent/impl/ComponentContainerImpl.java @@ -93,7 +93,7 @@ public class ComponentContainerImpl implements ComponentContainer { return getComponent(RealmHandler.class).getRealm(realm); } - private void initializeComponent(Map, StrolchComponent> componentMap, + private void setupComponent(Map, StrolchComponent> componentMap, Map controllerMap, ComponentConfiguration componentConfiguration) { String componentName = componentConfiguration.getName(); @@ -120,6 +120,7 @@ public class ComponentContainerImpl implements ComponentContainer { Constructor 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())); } diff --git a/src/main/java/li/strolch/agent/impl/DefaultRealmHandler.java b/src/main/java/li/strolch/agent/impl/DefaultRealmHandler.java index 6a7b5b09f..c440d3e09 100644 --- a/src/main/java/li/strolch/agent/impl/DefaultRealmHandler.java +++ b/src/main/java/li/strolch/agent/impl/DefaultRealmHandler.java @@ -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); diff --git a/src/main/java/li/strolch/persistence/inmemory/InMemoryTransaction.java b/src/main/java/li/strolch/persistence/inmemory/InMemoryTransaction.java index 6f49f04d1..a0bd6e7e8 100644 --- a/src/main/java/li/strolch/persistence/inmemory/InMemoryTransaction.java +++ b/src/main/java/li/strolch/persistence/inmemory/InMemoryTransaction.java @@ -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));