diff --git a/src/main/java/li/strolch/agent/api/ComponentContainer.java b/src/main/java/li/strolch/agent/api/ComponentContainer.java index e14da2141..136391b21 100644 --- a/src/main/java/li/strolch/agent/api/ComponentContainer.java +++ b/src/main/java/li/strolch/agent/api/ComponentContainer.java @@ -17,7 +17,6 @@ package li.strolch.agent.api; import java.util.Set; -import li.strolch.agent.impl.DataStoreMode; import li.strolch.agent.impl.StrolchRealm; import li.strolch.exception.StrolchException; import li.strolch.runtime.StrolchConstants; @@ -31,8 +30,6 @@ public interface ComponentContainer { public abstract ComponentState getState(); - public abstract DataStoreMode getDataStoreMode(); - public abstract boolean hasComponent(Class clazz); public abstract T getComponent(Class clazz); diff --git a/src/main/java/li/strolch/agent/api/ElementMapHandler.java b/src/main/java/li/strolch/agent/api/RealmHandler.java similarity index 97% rename from src/main/java/li/strolch/agent/api/ElementMapHandler.java rename to src/main/java/li/strolch/agent/api/RealmHandler.java index 06cddc9a8..bcb0dd9ab 100644 --- a/src/main/java/li/strolch/agent/api/ElementMapHandler.java +++ b/src/main/java/li/strolch/agent/api/RealmHandler.java @@ -24,7 +24,7 @@ import li.strolch.runtime.StrolchConstants; /** * @author Robert von Burg */ -public interface ElementMapHandler { +public interface RealmHandler { /** * Returns the names of the configured {@link StrolchRealm StrolchRealms} diff --git a/src/main/java/li/strolch/agent/api/StrolchAgent.java b/src/main/java/li/strolch/agent/api/StrolchAgent.java index af78fb0f1..4467eacdb 100644 --- a/src/main/java/li/strolch/agent/api/StrolchAgent.java +++ b/src/main/java/li/strolch/agent/api/StrolchAgent.java @@ -19,14 +19,10 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.text.MessageFormat; -import java.util.List; import java.util.Properties; import java.util.Set; import li.strolch.agent.impl.ComponentContainerImpl; -import li.strolch.agent.impl.DataStoreMode; -import li.strolch.agent.impl.ElementMapHandlerConfigurator; -import li.strolch.runtime.configuration.ComponentConfiguration; import li.strolch.runtime.configuration.ConfigurationParser; import li.strolch.runtime.configuration.RuntimeConfiguration; import li.strolch.runtime.configuration.StrolchConfiguration; @@ -40,9 +36,6 @@ import org.slf4j.LoggerFactory; public class StrolchAgent { private static final String AGENT_VERSION_PROPERTIES = "/agentVersion.properties"; //$NON-NLS-1$ - public static final String PROP_DATA_STORE_MODE = "dataStoreMode"; //$NON-NLS-1$ - public static final String PROP_DATA_STORE_FILE = "dataStoreFile"; //$NON-NLS-1$ - public static final String PROP_REALMS = "realms"; //$NON-NLS-1$ private static final Logger logger = LoggerFactory.getLogger(StrolchAgent.class); /** @@ -95,19 +88,11 @@ public class StrolchAgent { logger.info(MessageFormat.format(msg, path.getAbsolutePath())); this.strolchConfiguration = ConfigurationParser.parseConfiguration(path); - RuntimeConfiguration runtimeConfiguration = this.strolchConfiguration.getRuntimeConfiguration(); - DataStoreMode dataStoreMode = DataStoreMode.parseDataStoreMode(runtimeConfiguration.getString( - PROP_DATA_STORE_MODE, null)); - ElementMapHandlerConfigurator mapHandlerConfigurator = dataStoreMode.getElementMapConfigurationConfigurator(); - List configurations = mapHandlerConfigurator.buildConfigurations(this); - for (ComponentConfiguration configuration : configurations) { - this.strolchConfiguration.addConfiguration(configuration.getName(), configuration); - } - - ComponentContainerImpl container = new ComponentContainerImpl(this, dataStoreMode); + ComponentContainerImpl container = new ComponentContainerImpl(this); this.container = container; + RuntimeConfiguration runtimeConfiguration = this.strolchConfiguration.getRuntimeConfiguration(); logger.info(MessageFormat.format("Setup Agent {0}", runtimeConfiguration.getApplicationName())); //$NON-NLS-1$ } diff --git a/src/main/java/li/strolch/agent/impl/AbstractElementMapHandler.java b/src/main/java/li/strolch/agent/impl/AbstractElementMapHandler.java deleted file mode 100644 index 5c5e5a30a..000000000 --- a/src/main/java/li/strolch/agent/impl/AbstractElementMapHandler.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2013 Robert von Burg - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package li.strolch.agent.impl; - -import java.text.MessageFormat; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import li.strolch.agent.api.ComponentContainer; -import li.strolch.agent.api.ElementMapHandler; -import li.strolch.agent.api.StrolchComponent; -import li.strolch.exception.StrolchException; -import ch.eitchnet.utils.dbc.DBC; - -/** - * @author Robert von Burg - */ -public abstract class AbstractElementMapHandler extends StrolchComponent implements ElementMapHandler { - - protected Map realms; - - /** - * @param container - * @param componentName - */ - public AbstractElementMapHandler(ComponentContainer container, String componentName) { - super(container, componentName); - } - - @Override - public Set getRealmNames() { - return new HashSet<>(this.realms.keySet()); - } - - @Override - public StrolchRealm getRealm(String realm) throws StrolchException { - DBC.PRE.assertNotEmpty("Realm name must be set!", realm); - StrolchRealm strolchRealm = this.realms.get(realm); - if (strolchRealm == null) { - String msg = "No realm is configured with the name {0}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, realm); - throw new StrolchException(msg); - } - return strolchRealm; - } -} diff --git a/src/main/java/li/strolch/agent/impl/CachedElementMap.java b/src/main/java/li/strolch/agent/impl/CachedElementMap.java index 6d790c782..b4a90778c 100644 --- a/src/main/java/li/strolch/agent/impl/CachedElementMap.java +++ b/src/main/java/li/strolch/agent/impl/CachedElementMap.java @@ -184,7 +184,7 @@ public abstract class CachedElementMap implements Elem /** * Special method used when starting the container to cache the values. Not to be used anywhere else but from the - * {@link CachedElementMapHandler} and of course through the {@link #add(StrolchTransaction, StrolchElement)}-call + * {@link CachedRealm} and of course through the {@link #add(StrolchTransaction, StrolchElement)}-call * to not duplicate code * * @param element diff --git a/src/main/java/li/strolch/agent/impl/CachedElementMapHandler.java b/src/main/java/li/strolch/agent/impl/CachedElementMapHandler.java deleted file mode 100644 index d747b947a..000000000 --- a/src/main/java/li/strolch/agent/impl/CachedElementMapHandler.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright 2013 Robert von Burg - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package li.strolch.agent.impl; - -import java.text.MessageFormat; -import java.util.HashMap; -import java.util.List; -import java.util.Set; - -import li.strolch.agent.api.ComponentContainer; -import li.strolch.agent.api.StrolchAgent; -import li.strolch.model.Order; -import li.strolch.model.Resource; -import li.strolch.persistence.api.OrderDao; -import li.strolch.persistence.api.PersistenceHandler; -import li.strolch.persistence.api.ResourceDao; -import li.strolch.persistence.api.StrolchTransaction; -import li.strolch.runtime.StrolchConstants; -import li.strolch.runtime.configuration.ComponentConfiguration; -import li.strolch.runtime.configuration.RuntimeConfiguration; -import ch.eitchnet.utils.helper.StringHelper; - -/** - * @author Robert von Burg - */ -public class CachedElementMapHandler extends AbstractElementMapHandler { - - /** - * @param container - * @param componentName - */ - public CachedElementMapHandler(ComponentContainer container, String componentName) { - super(container, componentName); - } - - @Override - public void initialize(ComponentConfiguration configuration) { - - RuntimeConfiguration runtimeConfiguration = configuration.getRuntimeConfiguration(); - String[] realms = runtimeConfiguration.getStringArray(StrolchAgent.PROP_REALMS, StrolchConstants.DEFAULT_REALM); - - this.realms = new HashMap<>(); - for (String realm : realms) { - PersistenceHandler persistenceHandler = getContainer().getComponent(PersistenceHandler.class); - CachedResourceMap resourceMap = new CachedResourceMap(); - CachedOrderMap orderMap = new CachedOrderMap(); - StrolchRealm strolchRealm = new StrolchRealm(realm, persistenceHandler, resourceMap, orderMap); - this.realms.put(realm, strolchRealm); - } - - super.initialize(configuration); - } - - @Override - public void start() { - - for (String realm : this.realms.keySet()) { - - long start = System.nanoTime(); - int nrOfOrders = 0; - int nrOfResources = 0; - - StrolchRealm strolchRealm = this.realms.get(realm); - CachedOrderMap orderMap = (CachedOrderMap) strolchRealm.getOrderMap(); - CachedResourceMap resourceMap = (CachedResourceMap) strolchRealm.getResourceMap(); - - try (StrolchTransaction tx = strolchRealm.openTx()) { - ResourceDao resourceDao = tx.getPersistenceHandler().getResourceDao(tx); - Set resourceTypes = resourceDao.queryTypes(); - for (String type : resourceTypes) { - List resources = resourceDao.queryAll(type); - for (Resource resource : resources) { - resourceMap.insert(resource, null); - nrOfResources++; - } - } - } - - try (StrolchTransaction tx = strolchRealm.openTx()) { - OrderDao orderDao = tx.getPersistenceHandler().getOrderDao(tx); - Set orderTypes = orderDao.queryTypes(); - for (String type : orderTypes) { - List orders = orderDao.queryAll(type); - for (Order order : orders) { - orderMap.insert(order, null); - nrOfOrders++; - } - } - } - - long duration = System.nanoTime() - start; - String durationS = StringHelper.formatNanoDuration(duration); - logger.info(MessageFormat.format("Loading Model from Database for realm {0} took {1}.", realm, durationS)); //$NON-NLS-1$ - logger.info(MessageFormat.format("Loaded {0} Orders", nrOfOrders)); //$NON-NLS-1$ - logger.info(MessageFormat.format("Loaded {0} Resources", nrOfResources)); //$NON-NLS-1$ - } - - super.start(); - } -} diff --git a/src/main/java/li/strolch/agent/impl/CachedElementMapHandlerConfigurator.java b/src/main/java/li/strolch/agent/impl/CachedElementMapHandlerConfigurator.java deleted file mode 100644 index df31643b6..000000000 --- a/src/main/java/li/strolch/agent/impl/CachedElementMapHandlerConfigurator.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2013 Robert von Burg - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package li.strolch.agent.impl; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import li.strolch.agent.api.ElementMapHandler; -import li.strolch.agent.api.StrolchAgent; -import li.strolch.persistence.api.PersistenceHandler; -import li.strolch.runtime.configuration.ComponentConfiguration; -import li.strolch.runtime.configuration.RuntimeConfiguration; - -/** - * @author Robert von Burg - */ -public class CachedElementMapHandlerConfigurator implements ElementMapHandlerConfigurator { - - @Override - public List buildConfigurations(StrolchAgent agent) { - - String name = ElementMapHandler.class.getSimpleName(); - String api = ElementMapHandler.class.getName(); - String impl = CachedElementMapHandler.class.getName(); - - Map configurationValues = new HashMap<>(); - Set dependencies = new HashSet<>(); - dependencies.add(PersistenceHandler.class.getSimpleName()); - - RuntimeConfiguration runtimeConfiguration = agent.getStrolchConfiguration().getRuntimeConfiguration(); - ComponentConfiguration configuration = new ComponentConfiguration(runtimeConfiguration, name, - configurationValues, api, impl, dependencies); - - return Arrays.asList(configuration); - } -} diff --git a/src/main/java/li/strolch/agent/impl/CachedRealm.java b/src/main/java/li/strolch/agent/impl/CachedRealm.java new file mode 100644 index 000000000..c57025c6f --- /dev/null +++ b/src/main/java/li/strolch/agent/impl/CachedRealm.java @@ -0,0 +1,122 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.agent.impl; + +import java.text.MessageFormat; +import java.util.List; +import java.util.Set; + +import li.strolch.agent.api.ComponentContainer; +import li.strolch.agent.api.OrderMap; +import li.strolch.agent.api.ResourceMap; +import li.strolch.model.Order; +import li.strolch.model.Resource; +import li.strolch.persistence.api.OrderDao; +import li.strolch.persistence.api.PersistenceHandler; +import li.strolch.persistence.api.ResourceDao; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.runtime.configuration.ComponentConfiguration; +import ch.eitchnet.utils.helper.StringHelper; + +/** + * @author Robert von Burg + */ +public class CachedRealm extends StrolchRealm { + + private PersistenceHandler persistenceHandler; + private CachedResourceMap resourceMap; + private CachedOrderMap orderMap; + + public CachedRealm(String realm) { + super(realm); + } + + @Override + public DataStoreMode getMode() { + return DataStoreMode.CACHED; + } + + @Override + public StrolchTransaction openTx() { + return this.persistenceHandler.openTx(this); + } + + @Override + public ResourceMap getResourceMap() { + return this.resourceMap; + } + + @Override + public OrderMap getOrderMap() { + return this.orderMap; + } + + @Override + public void initialize(ComponentContainer container, ComponentConfiguration configuration) { + + this.persistenceHandler = container.getComponent(PersistenceHandler.class); + this.resourceMap = new CachedResourceMap(); + this.orderMap = new CachedOrderMap(); + } + + @Override + public void start() { + + long start = System.nanoTime(); + int nrOfOrders = 0; + int nrOfResources = 0; + + try (StrolchTransaction tx = openTx()) { + ResourceDao resourceDao = tx.getPersistenceHandler().getResourceDao(tx); + Set resourceTypes = resourceDao.queryTypes(); + for (String type : resourceTypes) { + List resources = resourceDao.queryAll(type); + for (Resource resource : resources) { + resourceMap.insert(resource, null); + nrOfResources++; + } + } + } + + try (StrolchTransaction tx = openTx()) { + OrderDao orderDao = tx.getPersistenceHandler().getOrderDao(tx); + Set orderTypes = orderDao.queryTypes(); + for (String type : orderTypes) { + List orders = orderDao.queryAll(type); + for (Order order : orders) { + orderMap.insert(order, null); + nrOfOrders++; + } + } + } + + long duration = System.nanoTime() - start; + String durationS = StringHelper.formatNanoDuration(duration); + logger.info(MessageFormat.format("Loading Model from Database for realm {0} took {1}.", getRealm(), durationS)); //$NON-NLS-1$ + logger.info(MessageFormat.format("Loaded {0} Orders", nrOfOrders)); //$NON-NLS-1$ + logger.info(MessageFormat.format("Loaded {0} Resources", nrOfResources)); //$NON-NLS-1$ + } + + @Override + public void stop() { + // + } + + @Override + public void destroy() { + // + } +} diff --git a/src/main/java/li/strolch/agent/impl/ComponentContainerImpl.java b/src/main/java/li/strolch/agent/impl/ComponentContainerImpl.java index 705e06b9b..21af41496 100644 --- a/src/main/java/li/strolch/agent/impl/ComponentContainerImpl.java +++ b/src/main/java/li/strolch/agent/impl/ComponentContainerImpl.java @@ -24,7 +24,7 @@ import java.util.Set; import li.strolch.agent.api.ComponentContainer; import li.strolch.agent.api.ComponentState; -import li.strolch.agent.api.ElementMapHandler; +import li.strolch.agent.api.RealmHandler; import li.strolch.agent.api.StrolchAgent; import li.strolch.agent.api.StrolchComponent; import li.strolch.exception.StrolchException; @@ -40,17 +40,14 @@ public class ComponentContainerImpl implements ComponentContainer { private static final Logger logger = LoggerFactory.getLogger(ComponentContainerImpl.class); private StrolchAgent agent; - private DataStoreMode dataStoreMode; - private Map, StrolchComponent> componentMap; private Map controllerMap; private ComponentDependencyAnalyzer dependencyAnalyzer; private StrolchConfiguration strolchConfiguration; private ComponentState state; - public ComponentContainerImpl(StrolchAgent agent, DataStoreMode dataStoreMode) { + public ComponentContainerImpl(StrolchAgent agent) { this.agent = agent; - this.dataStoreMode = dataStoreMode; this.state = ComponentState.UNDEFINED; } @@ -64,11 +61,6 @@ public class ComponentContainerImpl implements ComponentContainer { return this.state; } - @Override - public DataStoreMode getDataStoreMode() { - return this.dataStoreMode; - } - @Override public Set> getComponentTypes() { return this.componentMap.keySet(); @@ -93,12 +85,12 @@ public class ComponentContainerImpl implements ComponentContainer { @Override public Set getRealmNames() { - return getComponent(ElementMapHandler.class).getRealmNames(); + return getComponent(RealmHandler.class).getRealmNames(); } @Override public StrolchRealm getRealm(String realm) throws StrolchException { - return getComponent(ElementMapHandler.class).getRealm(realm); + return getComponent(RealmHandler.class).getRealm(realm); } private void initializeComponent(Map, StrolchComponent> componentMap, diff --git a/src/main/java/li/strolch/agent/impl/DataStoreMode.java b/src/main/java/li/strolch/agent/impl/DataStoreMode.java index 59464eb02..9e6d02ab8 100644 --- a/src/main/java/li/strolch/agent/impl/DataStoreMode.java +++ b/src/main/java/li/strolch/agent/impl/DataStoreMode.java @@ -24,30 +24,30 @@ import java.text.MessageFormat; public enum DataStoreMode { EMPTY { @Override - public ElementMapHandlerConfigurator getElementMapConfigurationConfigurator() { - return new EmptyElementMapHandlerConfigurator(); + public StrolchRealm createRealm(String realm) { + return new EmptyRealm(realm); } }, // TRANSIENT { @Override - public ElementMapHandlerConfigurator getElementMapConfigurationConfigurator() { - return new TransientElementMapHandlerConfigurator(); + public StrolchRealm createRealm(String realm) { + return new TransientRealm(realm); } }, // CACHED { @Override - public ElementMapHandlerConfigurator getElementMapConfigurationConfigurator() { - return new CachedElementMapHandlerConfigurator(); + public StrolchRealm createRealm(String realm) { + return new CachedRealm(realm); } }, // TRANSACTIONAL { @Override - public ElementMapHandlerConfigurator getElementMapConfigurationConfigurator() { - return new TransactionalElementMapHandlerConfigurator(); + public StrolchRealm createRealm(String realm) { + return new TransactionalRealm(realm); } }; // - public abstract ElementMapHandlerConfigurator getElementMapConfigurationConfigurator(); + public abstract StrolchRealm createRealm(String realm); public static DataStoreMode parseDataStoreMode(String modeS) { for (DataStoreMode dataStoreMode : values()) { diff --git a/src/main/java/li/strolch/agent/impl/DefaultRealmHandler.java b/src/main/java/li/strolch/agent/impl/DefaultRealmHandler.java new file mode 100644 index 000000000..6a7b5b09f --- /dev/null +++ b/src/main/java/li/strolch/agent/impl/DefaultRealmHandler.java @@ -0,0 +1,110 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.agent.impl; + +import static ch.eitchnet.utils.helper.StringHelper.DOT; + +import java.text.MessageFormat; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import li.strolch.agent.api.ComponentContainer; +import li.strolch.agent.api.RealmHandler; +import li.strolch.agent.api.StrolchComponent; +import li.strolch.exception.StrolchException; +import li.strolch.runtime.StrolchConstants; +import li.strolch.runtime.configuration.ComponentConfiguration; +import ch.eitchnet.utils.dbc.DBC; + +/** + * @author Robert von Burg + */ +public class DefaultRealmHandler extends StrolchComponent implements RealmHandler { + + public static final String PREFIX_DATA_STORE_MODE = "dataStoreMode"; //$NON-NLS-1$ + public static final String PROP_REALMS = "realms"; //$NON-NLS-1$ + + protected Map realms; + + /** + * @param container + * @param componentName + */ + public DefaultRealmHandler(ComponentContainer container, String componentName) { + super(container, componentName); + } + + @Override + public Set getRealmNames() { + return new HashSet<>(this.realms.keySet()); + } + + @Override + public StrolchRealm getRealm(String realm) throws StrolchException { + DBC.PRE.assertNotEmpty("Realm name must be set!", realm); + StrolchRealm strolchRealm = this.realms.get(realm); + if (strolchRealm == null) { + String msg = "No realm is configured with the name {0}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, realm); + throw new StrolchException(msg); + } + return strolchRealm; + } + + @Override + public void initialize(ComponentConfiguration configuration) { + + this.realms = new HashMap<>(); + String[] realms = configuration.getStringArray(PROP_REALMS, StrolchConstants.DEFAULT_REALM); + for (String realmName : realms) { + String dataStoreModeKey = PREFIX_DATA_STORE_MODE; + if (!realmName.equals(StrolchConstants.DEFAULT_REALM)) + dataStoreModeKey += DOT + realmName; + + String realmMode = configuration.getString(dataStoreModeKey, null); + DataStoreMode dataStoreMode = DataStoreMode.parseDataStoreMode(realmMode); + StrolchRealm realm = dataStoreMode.createRealm(realmName); + this.realms.put(realmName, realm); + } + + for (String realmName : this.realms.keySet()) { + StrolchRealm realm = this.realms.get(realmName); + realm.initialize(getContainer(), configuration); + } + + super.initialize(configuration); + } + + @Override + public void start() { + for (String realmName : this.realms.keySet()) { + StrolchRealm realm = this.realms.get(realmName); + realm.start(); + } + super.start(); + } + + @Override + public void stop() { + for (String realmName : this.realms.keySet()) { + StrolchRealm realm = this.realms.get(realmName); + realm.stop(); + } + super.stop(); + } +} diff --git a/src/main/java/li/strolch/agent/impl/ElementMapHandlerConfigurator.java b/src/main/java/li/strolch/agent/impl/ElementMapHandlerConfigurator.java deleted file mode 100644 index a33e843bf..000000000 --- a/src/main/java/li/strolch/agent/impl/ElementMapHandlerConfigurator.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2013 Robert von Burg - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package li.strolch.agent.impl; - -import java.util.List; - -import li.strolch.agent.api.StrolchAgent; -import li.strolch.runtime.configuration.ComponentConfiguration; - -/** - * @author Robert von Burg - */ -public interface ElementMapHandlerConfigurator { - - public List buildConfigurations(StrolchAgent agent); -} diff --git a/src/main/java/li/strolch/agent/impl/EmptyElementMapHandler.java b/src/main/java/li/strolch/agent/impl/EmptyElementMapHandler.java deleted file mode 100644 index f9288fef9..000000000 --- a/src/main/java/li/strolch/agent/impl/EmptyElementMapHandler.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2013 Robert von Burg - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package li.strolch.agent.impl; - -import li.strolch.agent.api.ComponentContainer; - -/** - * @author Robert von Burg - */ -public class EmptyElementMapHandler extends InMemoryElementMapHandler { - - /** - * @param container - * @param componentName - */ - public EmptyElementMapHandler(ComponentContainer container, String componentName) { - super(container, componentName); - } -} diff --git a/src/main/java/li/strolch/agent/impl/EmptyElementMapHandlerConfigurator.java b/src/main/java/li/strolch/agent/impl/EmptyElementMapHandlerConfigurator.java deleted file mode 100644 index 0f92acb3e..000000000 --- a/src/main/java/li/strolch/agent/impl/EmptyElementMapHandlerConfigurator.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2013 Robert von Burg - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package li.strolch.agent.impl; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -import li.strolch.agent.api.ElementMapHandler; -import li.strolch.agent.api.StrolchAgent; -import li.strolch.runtime.configuration.ComponentConfiguration; -import li.strolch.runtime.configuration.RuntimeConfiguration; - -/** - * @author Robert von Burg - */ -public class EmptyElementMapHandlerConfigurator extends TransientElementMapHandlerConfigurator { - - @Override - protected ComponentConfiguration getElementMapHandlerConfiguration(StrolchAgent agent) { - String name = ElementMapHandler.class.getSimpleName(); - String api = ElementMapHandler.class.getName(); - String impl = EmptyElementMapHandler.class.getName(); - - Map configurationValues = new HashMap<>(); - Set dependencies = Collections.emptySet(); - - RuntimeConfiguration runtimeConfiguration = agent.getStrolchConfiguration().getRuntimeConfiguration(); - ComponentConfiguration configuration = new ComponentConfiguration(runtimeConfiguration, name, - configurationValues, api, impl, dependencies); - - return configuration; - } -} diff --git a/src/main/java/li/strolch/agent/impl/EmptyRealm.java b/src/main/java/li/strolch/agent/impl/EmptyRealm.java new file mode 100644 index 000000000..a430ed59f --- /dev/null +++ b/src/main/java/li/strolch/agent/impl/EmptyRealm.java @@ -0,0 +1,82 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.agent.impl; + +import java.text.MessageFormat; + +import li.strolch.agent.api.ComponentContainer; +import li.strolch.agent.api.OrderMap; +import li.strolch.agent.api.ResourceMap; +import li.strolch.persistence.api.PersistenceHandler; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.persistence.inmemory.InMemoryPersistence; +import li.strolch.runtime.configuration.ComponentConfiguration; + +/** + * @author Robert von Burg + */ +public class EmptyRealm extends StrolchRealm { + + private ResourceMap resourceMap; + private OrderMap orderMap; + private PersistenceHandler persistenceHandler; + + public EmptyRealm(String realm) { + super(realm); + } + + @Override + public DataStoreMode getMode() { + return DataStoreMode.EMPTY; + } + + @Override + public StrolchTransaction openTx() { + return this.persistenceHandler.openTx(this); + } + + @Override + public ResourceMap getResourceMap() { + return this.resourceMap; + } + + @Override + public OrderMap getOrderMap() { + return this.orderMap; + } + + @Override + public void initialize(ComponentContainer container, ComponentConfiguration configuration) { + this.persistenceHandler = new InMemoryPersistence(); + this.resourceMap = new TransactionalResourceMap(); + this.orderMap = new TransactionalOrderMap(); + } + + @Override + public void start() { + logger.info(MessageFormat.format("Initialized EMPTY Realm {0}", getRealm())); //$NON-NLS-1$ + } + + @Override + public void stop() { + // + } + + @Override + public void destroy() { + // + } +} diff --git a/src/main/java/li/strolch/agent/impl/InMemoryElementMapHandler.java b/src/main/java/li/strolch/agent/impl/InMemoryElementMapHandler.java deleted file mode 100644 index 15873a221..000000000 --- a/src/main/java/li/strolch/agent/impl/InMemoryElementMapHandler.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2013 Robert von Burg - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package li.strolch.agent.impl; - -import java.util.HashMap; - -import li.strolch.agent.api.ComponentContainer; -import li.strolch.agent.api.StrolchAgent; -import li.strolch.persistence.api.PersistenceHandler; -import li.strolch.runtime.StrolchConstants; -import li.strolch.runtime.configuration.ComponentConfiguration; -import li.strolch.runtime.configuration.RuntimeConfiguration; - -/** - * @author Robert von Burg - */ -public class InMemoryElementMapHandler extends AbstractElementMapHandler { - - /** - * @param container - * @param componentName - */ - public InMemoryElementMapHandler(ComponentContainer container, String componentName) { - super(container, componentName); - } - - @Override - public void initialize(ComponentConfiguration configuration) { - - RuntimeConfiguration runtimeConfiguration = configuration.getRuntimeConfiguration(); - String[] realms = runtimeConfiguration.getStringArray(StrolchAgent.PROP_REALMS, StrolchConstants.DEFAULT_REALM); - - this.realms = new HashMap<>(); - for (String realm : realms) { - PersistenceHandler persistenceHandler = getContainer().getComponent(PersistenceHandler.class); - TransactionalResourceMap resourceMap = new TransactionalResourceMap(); - TransactionalOrderMap orderMap = new TransactionalOrderMap(); - StrolchRealm strolchRealm = new StrolchRealm(realm, persistenceHandler, resourceMap, orderMap); - this.realms.put(realm, strolchRealm); - } - - super.initialize(configuration); - } -} diff --git a/src/main/java/li/strolch/agent/impl/StrolchRealm.java b/src/main/java/li/strolch/agent/impl/StrolchRealm.java index e444179e0..a1e071c49 100644 --- a/src/main/java/li/strolch/agent/impl/StrolchRealm.java +++ b/src/main/java/li/strolch/agent/impl/StrolchRealm.java @@ -15,50 +15,44 @@ */ package li.strolch.agent.impl; +import li.strolch.agent.api.ComponentContainer; import li.strolch.agent.api.OrderMap; import li.strolch.agent.api.ResourceMap; -import li.strolch.persistence.api.PersistenceHandler; import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.runtime.configuration.ComponentConfiguration; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @author Robert von Burg */ -public class StrolchRealm { +public abstract class StrolchRealm { + protected static final Logger logger = LoggerFactory.getLogger(StrolchRealm.class); private String realm; - private ResourceMap resourceMap; - private OrderMap orderMap; - private PersistenceHandler persistenceHandler; - public StrolchRealm(String realm, PersistenceHandler persistenceHandler, ResourceMap resourceMap, OrderMap orderMap) { + public StrolchRealm(String realm) { this.realm = realm; - this.persistenceHandler = persistenceHandler; - this.resourceMap = resourceMap; - this.orderMap = orderMap; } - /** - * @return the realm - */ public String getRealm() { return this.realm; } - public StrolchTransaction openTx() { - return this.persistenceHandler.openTx(this); - } + public abstract DataStoreMode getMode(); - /** - * @return the resourceMap - */ - public ResourceMap getResourceMap() { - return this.resourceMap; - } + public abstract void initialize(ComponentContainer container, ComponentConfiguration configuration); - /** - * @return the orderMap - */ - public OrderMap getOrderMap() { - return this.orderMap; - } + public abstract void start(); + + public abstract void stop(); + + public abstract void destroy(); + + public abstract StrolchTransaction openTx(); + + public abstract ResourceMap getResourceMap(); + + public abstract OrderMap getOrderMap(); } diff --git a/src/main/java/li/strolch/agent/impl/TransactionalElementMapHandler.java b/src/main/java/li/strolch/agent/impl/TransactionalElementMapHandler.java deleted file mode 100644 index af696e247..000000000 --- a/src/main/java/li/strolch/agent/impl/TransactionalElementMapHandler.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2013 Robert von Burg - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package li.strolch.agent.impl; - -import java.text.MessageFormat; - -import li.strolch.agent.api.ComponentContainer; -import li.strolch.agent.api.OrderMap; -import li.strolch.agent.api.ResourceMap; -import li.strolch.persistence.api.StrolchTransaction; -import ch.eitchnet.utils.helper.StringHelper; - -/** - * @author Robert von Burg - */ -public class TransactionalElementMapHandler extends InMemoryElementMapHandler { - - /** - * @param container - * @param componentName - */ - public TransactionalElementMapHandler(ComponentContainer container, String componentName) { - super(container, componentName); - } - - @Override - public void start() { - - for (String realm : this.realms.keySet()) { - - long start = System.nanoTime(); - int nrOfOrders = 0; - int nrOfResources = 0; - - StrolchRealm strolchRealm = this.realms.get(realm); - OrderMap orderMap = strolchRealm.getOrderMap(); - ResourceMap resourceMap = strolchRealm.getResourceMap(); - - try (StrolchTransaction tx = strolchRealm.openTx()) { - nrOfOrders = orderMap.getAllKeys(tx).size(); - } - - try (StrolchTransaction tx = strolchRealm.openTx()) { - nrOfResources = resourceMap.getAllKeys(tx).size(); - } - - long duration = System.nanoTime() - start; - String durationS = StringHelper.formatNanoDuration(duration); - logger.info(MessageFormat - .format("Initialized Transactional Maps for realm {0} took {1}.", realm, durationS)); //$NON-NLS-1$ - logger.info(MessageFormat.format("There are {0} Orders", nrOfOrders)); //$NON-NLS-1$ - logger.info(MessageFormat.format("There are {0} Resources", nrOfResources)); //$NON-NLS-1$ - } - - super.start(); - } -} diff --git a/src/main/java/li/strolch/agent/impl/TransactionalElementMapHandlerConfigurator.java b/src/main/java/li/strolch/agent/impl/TransactionalElementMapHandlerConfigurator.java deleted file mode 100644 index 08e08f021..000000000 --- a/src/main/java/li/strolch/agent/impl/TransactionalElementMapHandlerConfigurator.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2013 Robert von Burg - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package li.strolch.agent.impl; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import li.strolch.agent.api.ElementMapHandler; -import li.strolch.agent.api.StrolchAgent; -import li.strolch.persistence.api.PersistenceHandler; -import li.strolch.runtime.configuration.ComponentConfiguration; -import li.strolch.runtime.configuration.RuntimeConfiguration; - -/** - * @author Robert von Burg - */ -public class TransactionalElementMapHandlerConfigurator implements ElementMapHandlerConfigurator { - - @Override - public List buildConfigurations(StrolchAgent agent) { - - String name = ElementMapHandler.class.getSimpleName(); - String api = ElementMapHandler.class.getName(); - String impl = TransactionalElementMapHandler.class.getName(); - - Map configurationValues = new HashMap<>(); - Set dependencies = new HashSet<>(); - dependencies.add(PersistenceHandler.class.getSimpleName()); - - RuntimeConfiguration runtimeConfiguration = agent.getStrolchConfiguration().getRuntimeConfiguration(); - ComponentConfiguration configuration = new ComponentConfiguration(runtimeConfiguration, name, - configurationValues, api, impl, dependencies); - - return Arrays.asList(configuration); - } -} diff --git a/src/main/java/li/strolch/agent/impl/TransactionalRealm.java b/src/main/java/li/strolch/agent/impl/TransactionalRealm.java new file mode 100644 index 000000000..f3f648c77 --- /dev/null +++ b/src/main/java/li/strolch/agent/impl/TransactionalRealm.java @@ -0,0 +1,100 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.agent.impl; + +import java.text.MessageFormat; + +import li.strolch.agent.api.ComponentContainer; +import li.strolch.agent.api.OrderMap; +import li.strolch.agent.api.ResourceMap; +import li.strolch.persistence.api.PersistenceHandler; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.runtime.configuration.ComponentConfiguration; +import ch.eitchnet.utils.helper.StringHelper; + +/** + * @author Robert von Burg + */ +public class TransactionalRealm extends StrolchRealm { + + private ResourceMap resourceMap; + private OrderMap orderMap; + private PersistenceHandler persistenceHandler; + + public TransactionalRealm(String realm) { + super(realm); + } + + @Override + public DataStoreMode getMode() { + return DataStoreMode.TRANSACTIONAL; + } + + @Override + public StrolchTransaction openTx() { + return this.persistenceHandler.openTx(this); + } + + @Override + public ResourceMap getResourceMap() { + return this.resourceMap; + } + + @Override + public OrderMap getOrderMap() { + return this.orderMap; + } + + @Override + public void initialize(ComponentContainer container, ComponentConfiguration configuration) { + this.resourceMap = new TransactionalResourceMap(); + this.orderMap = new TransactionalOrderMap(); + this.persistenceHandler = container.getComponent(PersistenceHandler.class); + } + + @Override + public void start() { + + long start = System.nanoTime(); + int nrOfOrders = 0; + int nrOfResources = 0; + + try (StrolchTransaction tx = openTx()) { + nrOfOrders = orderMap.getAllKeys(tx).size(); + } + + try (StrolchTransaction tx = openTx()) { + nrOfResources = resourceMap.getAllKeys(tx).size(); + } + + long duration = System.nanoTime() - start; + String durationS = StringHelper.formatNanoDuration(duration); + logger.info(MessageFormat.format( + "Initialized Transactional Maps for realm {0} took {1}.", getRealm(), durationS)); //$NON-NLS-1$ + logger.info(MessageFormat.format("There are {0} Orders", nrOfOrders)); //$NON-NLS-1$ + logger.info(MessageFormat.format("There are {0} Resources", nrOfResources)); //$NON-NLS-1$ + } + + @Override + public void stop() { + // + } + + @Override + public void destroy() { + // + } +} diff --git a/src/main/java/li/strolch/agent/impl/TransientElementMapHandler.java b/src/main/java/li/strolch/agent/impl/TransientElementMapHandler.java deleted file mode 100644 index b5de070e6..000000000 --- a/src/main/java/li/strolch/agent/impl/TransientElementMapHandler.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright 2013 Robert von Burg - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package li.strolch.agent.impl; - -import java.io.File; -import java.text.MessageFormat; -import java.util.HashMap; -import java.util.Map; - -import li.strolch.agent.api.ComponentContainer; -import li.strolch.agent.api.StrolchAgent; -import li.strolch.model.xml.XmlModelSaxReader.XmlModelStatistics; -import li.strolch.model.xml.XmlModelSaxFileReader; -import li.strolch.persistence.api.StrolchTransaction; -import li.strolch.runtime.StrolchConstants; -import li.strolch.runtime.configuration.ComponentConfiguration; -import li.strolch.runtime.configuration.RuntimeConfiguration; -import li.strolch.runtime.configuration.StrolchConfigurationException; -import ch.eitchnet.utils.helper.StringHelper; - -/** - * @author Robert von Burg - */ -public class TransientElementMapHandler extends InMemoryElementMapHandler { - - private Map realmModelFiles; - - /** - * @param container - * @param componentName - */ - public TransientElementMapHandler(ComponentContainer container, String componentName) { - super(container, componentName); - } - - @Override - public void initialize(ComponentConfiguration configuration) { - super.initialize(configuration); - - this.realmModelFiles = new HashMap<>(); - RuntimeConfiguration runtimeConfiguration = configuration.getRuntimeConfiguration(); - for (String realm : this.realms.keySet()) { - String key = getDataStoreFilePropKey(realm); - - if (!runtimeConfiguration.hasProperty(key)) { - String msg = "There is no data store file for realm {0}. Set a property with key {1}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, realm, key); - throw new StrolchConfigurationException(msg); - } - - File modelFile = runtimeConfiguration.getDataFile(key, null, runtimeConfiguration, true); - this.realmModelFiles.put(realm, modelFile); - } - } - - private String getDataStoreFilePropKey(String realm) { - if (realm.equals(StrolchConstants.DEFAULT_REALM)) - return StrolchAgent.PROP_DATA_STORE_FILE; - return StrolchAgent.PROP_DATA_STORE_FILE + "." + realm; //$NON-NLS-1$ - } - - @Override - public void start() { - - for (String realm : this.realms.keySet()) { - - StrolchRealm strolchRealm = this.realms.get(realm); - - File modelFile = this.realmModelFiles.get(realm); - XmlModelStatistics statistics; - try (StrolchTransaction tx = strolchRealm.openTx()) { - InMemoryElementListener elementListener = new InMemoryElementListener(tx); - XmlModelSaxFileReader handler = new XmlModelSaxFileReader(elementListener, modelFile); - handler.parseFile(); - statistics = handler.getStatistics(); - } - - String durationS = StringHelper.formatNanoDuration(statistics.durationNanos); - logger.info(MessageFormat.format( - "Loading XML Model file {0} for realm {1} took {2}.", modelFile.getName(), realm, durationS)); //$NON-NLS-1$ - logger.info(MessageFormat.format("Loaded {0} Orders", statistics.nrOfOrders)); //$NON-NLS-1$ - logger.info(MessageFormat.format("Loaded {0} Resources", statistics.nrOfResources)); //$NON-NLS-1$ - } - - super.start(); - } -} diff --git a/src/main/java/li/strolch/agent/impl/TransientElementMapHandlerConfigurator.java b/src/main/java/li/strolch/agent/impl/TransientElementMapHandlerConfigurator.java deleted file mode 100644 index e5fe0d461..000000000 --- a/src/main/java/li/strolch/agent/impl/TransientElementMapHandlerConfigurator.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright 2013 Robert von Burg - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package li.strolch.agent.impl; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import li.strolch.agent.api.ElementMapHandler; -import li.strolch.agent.api.StrolchAgent; -import li.strolch.persistence.api.PersistenceHandler; -import li.strolch.persistence.inmemory.InMemoryPersistenceHandler; -import li.strolch.runtime.configuration.ComponentConfiguration; -import li.strolch.runtime.configuration.RuntimeConfiguration; - -/** - * @author Robert von Burg - */ -public class TransientElementMapHandlerConfigurator implements ElementMapHandlerConfigurator { - - @Override - public List buildConfigurations(StrolchAgent agent) { - - List configurations = new ArrayList<>(); - configurations.add(getElementMapHandlerConfiguration(agent)); - configurations.add(getPersistenceHandlerConfiguration(agent)); - - return configurations; - } - - protected ComponentConfiguration getPersistenceHandlerConfiguration(StrolchAgent agent) { - String name = PersistenceHandler.class.getSimpleName(); - String api = PersistenceHandler.class.getName(); - String impl = InMemoryPersistenceHandler.class.getName(); - - Map configurationValues = new HashMap<>(); - Set dependencies = Collections.emptySet(); - - RuntimeConfiguration runtimeConfiguration = agent.getStrolchConfiguration().getRuntimeConfiguration(); - ComponentConfiguration configuration = new ComponentConfiguration(runtimeConfiguration, name, - configurationValues, api, impl, dependencies); - return configuration; - } - - protected ComponentConfiguration getElementMapHandlerConfiguration(StrolchAgent agent) { - String name = ElementMapHandler.class.getSimpleName(); - String api = ElementMapHandler.class.getName(); - String impl = TransientElementMapHandler.class.getName(); - - Map configurationValues = new HashMap<>(); - Set dependencies = new HashSet<>(); - dependencies.add(PersistenceHandler.class.getSimpleName()); - - RuntimeConfiguration runtimeConfiguration = agent.getStrolchConfiguration().getRuntimeConfiguration(); - ComponentConfiguration configuration = new ComponentConfiguration(runtimeConfiguration, name, - configurationValues, api, impl, dependencies); - return configuration; - } -} diff --git a/src/main/java/li/strolch/agent/impl/TransientRealm.java b/src/main/java/li/strolch/agent/impl/TransientRealm.java new file mode 100644 index 000000000..cceef5b20 --- /dev/null +++ b/src/main/java/li/strolch/agent/impl/TransientRealm.java @@ -0,0 +1,120 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.agent.impl; + +import static ch.eitchnet.utils.helper.StringHelper.DOT; + +import java.io.File; +import java.text.MessageFormat; + +import li.strolch.agent.api.ComponentContainer; +import li.strolch.agent.api.OrderMap; +import li.strolch.agent.api.ResourceMap; +import li.strolch.model.xml.XmlModelSaxFileReader; +import li.strolch.model.xml.XmlModelSaxReader.XmlModelStatistics; +import li.strolch.persistence.api.PersistenceHandler; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.persistence.inmemory.InMemoryPersistence; +import li.strolch.runtime.StrolchConstants; +import li.strolch.runtime.configuration.ComponentConfiguration; +import li.strolch.runtime.configuration.StrolchConfigurationException; +import ch.eitchnet.utils.helper.StringHelper; + +/** + * @author Robert von Burg + */ +public class TransientRealm extends StrolchRealm { + + public static final String PREFIX_DATA_STORE_FILE = "dataStoreFile"; //$NON-NLS-1$ + + private ResourceMap resourceMap; + private OrderMap orderMap; + private PersistenceHandler persistenceHandler; + + private File modelFile; + + public TransientRealm(String realm) { + super(realm); + } + + @Override + public DataStoreMode getMode() { + return DataStoreMode.TRANSIENT; + } + + @Override + public StrolchTransaction openTx() { + return this.persistenceHandler.openTx(this); + } + + @Override + public ResourceMap getResourceMap() { + return this.resourceMap; + } + + @Override + public OrderMap getOrderMap() { + return this.orderMap; + } + + @Override + public void initialize(ComponentContainer container, ComponentConfiguration configuration) { + + String key = PREFIX_DATA_STORE_FILE; + if (!getRealm().equals(StrolchConstants.DEFAULT_REALM)) + key += DOT + getRealm(); + + if (!configuration.hasProperty(key)) { + String msg = "There is no data store file for realm {0}. Set a property with key {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, getRealm(), key); + throw new StrolchConfigurationException(msg); + } + + this.modelFile = configuration.getDataFile(key, null, configuration.getRuntimeConfiguration(), true); + + this.persistenceHandler = new InMemoryPersistence(); + this.resourceMap = new TransactionalResourceMap(); + this.orderMap = new TransactionalOrderMap(); + } + + @Override + public void start() { + + XmlModelStatistics statistics; + try (StrolchTransaction tx = openTx()) { + InMemoryElementListener elementListener = new InMemoryElementListener(tx); + XmlModelSaxFileReader handler = new XmlModelSaxFileReader(elementListener, modelFile); + handler.parseFile(); + statistics = handler.getStatistics(); + } + + String durationS = StringHelper.formatNanoDuration(statistics.durationNanos); + logger.info(MessageFormat.format( + "Loading XML Model file {0} for realm {1} took {2}.", modelFile.getName(), getRealm(), durationS)); //$NON-NLS-1$ + logger.info(MessageFormat.format("Loaded {0} Orders", statistics.nrOfOrders)); //$NON-NLS-1$ + logger.info(MessageFormat.format("Loaded {0} Resources", statistics.nrOfResources)); //$NON-NLS-1$ + } + + @Override + public void stop() { + // + } + + @Override + public void destroy() { + // + } +} diff --git a/src/main/java/li/strolch/persistence/inmemory/InMemoryPersistence.java b/src/main/java/li/strolch/persistence/inmemory/InMemoryPersistence.java new file mode 100644 index 000000000..bcb0d13f9 --- /dev/null +++ b/src/main/java/li/strolch/persistence/inmemory/InMemoryPersistence.java @@ -0,0 +1,63 @@ +package li.strolch.persistence.inmemory; + +import java.util.HashMap; +import java.util.Map; + +import li.strolch.agent.impl.StrolchRealm; +import li.strolch.persistence.api.OrderDao; +import li.strolch.persistence.api.PersistenceHandler; +import li.strolch.persistence.api.ResourceDao; +import li.strolch.persistence.api.StrolchTransaction; + +public class InMemoryPersistence implements PersistenceHandler { + + private Map daoCache; + + public InMemoryPersistence() { + this.daoCache = new HashMap<>(); + } + + @Override + public StrolchTransaction openTx(StrolchRealm realm) { + return new InMemoryTransaction(realm, this); + } + + @Override + public OrderDao getOrderDao(StrolchTransaction tx) { + DaoCache daoCache = getDaoCache(tx); + return daoCache.getOrderDao(); + } + + @Override + public ResourceDao getResourceDao(StrolchTransaction tx) { + DaoCache daoCache = getDaoCache(tx); + return daoCache.getResourceDao(); + } + + private synchronized DaoCache getDaoCache(StrolchTransaction tx) { + DaoCache daoCache = this.daoCache.get(tx.getRealmName()); + if (daoCache == null) { + daoCache = new DaoCache(new InMemoryOrderDao(), new InMemoryResourceDao()); + this.daoCache.put(tx.getRealmName(), daoCache); + } + return daoCache; + } + + private class DaoCache { + private OrderDao orderDao; + private ResourceDao resourceDao; + + public DaoCache(OrderDao orderDao, ResourceDao resourceDao) { + this.orderDao = orderDao; + this.resourceDao = resourceDao; + } + + public OrderDao getOrderDao() { + return this.orderDao; + } + + public ResourceDao getResourceDao() { + return this.resourceDao; + } + } +} diff --git a/src/main/java/li/strolch/persistence/inmemory/InMemoryPersistenceHandler.java b/src/main/java/li/strolch/persistence/inmemory/InMemoryPersistenceHandler.java index 8992cd564..3d389621b 100644 --- a/src/main/java/li/strolch/persistence/inmemory/InMemoryPersistenceHandler.java +++ b/src/main/java/li/strolch/persistence/inmemory/InMemoryPersistenceHandler.java @@ -1,8 +1,20 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package li.strolch.persistence.inmemory; -import java.util.HashMap; -import java.util.Map; - import li.strolch.agent.api.ComponentContainer; import li.strolch.agent.api.StrolchComponent; import li.strolch.agent.impl.StrolchRealm; @@ -12,61 +24,40 @@ import li.strolch.persistence.api.ResourceDao; import li.strolch.persistence.api.StrolchTransaction; import li.strolch.runtime.configuration.ComponentConfiguration; +/** + * @author Robert von Burg + * + */ public class InMemoryPersistenceHandler extends StrolchComponent implements PersistenceHandler { - private Map daoCache; + private InMemoryPersistence persistenceHandler; + /** + * @param container + * @param componentName + */ public InMemoryPersistenceHandler(ComponentContainer container, String componentName) { super(container, componentName); } @Override - public StrolchTransaction openTx(StrolchRealm realm) { - return new InMemoryTransaction(realm, this); + public void initialize(ComponentConfiguration configuration) { + this.persistenceHandler = new InMemoryPersistence(); + super.initialize(configuration); } @Override - public void initialize(ComponentConfiguration configuration) { - super.initialize(configuration); - this.daoCache = new HashMap<>(); + public StrolchTransaction openTx(StrolchRealm realm) { + return this.persistenceHandler.openTx(realm); } @Override public OrderDao getOrderDao(StrolchTransaction tx) { - DaoCache daoCache = getDaoCache(tx); - return daoCache.getOrderDao(); + return this.persistenceHandler.getOrderDao(tx); } @Override public ResourceDao getResourceDao(StrolchTransaction tx) { - DaoCache daoCache = getDaoCache(tx); - return daoCache.getResourceDao(); - } - - private synchronized DaoCache getDaoCache(StrolchTransaction tx) { - DaoCache daoCache = this.daoCache.get(tx.getRealmName()); - if (daoCache == null) { - daoCache = new DaoCache(new InMemoryOrderDao(), new InMemoryResourceDao()); - this.daoCache.put(tx.getRealmName(), daoCache); - } - return daoCache; - } - - private class DaoCache { - private OrderDao orderDao; - private ResourceDao resourceDao; - - public DaoCache(OrderDao orderDao, ResourceDao resourceDao) { - this.orderDao = orderDao; - this.resourceDao = resourceDao; - } - - public OrderDao getOrderDao() { - return this.orderDao; - } - - public ResourceDao getResourceDao() { - return this.resourceDao; - } + return this.persistenceHandler.getResourceDao(tx); } } diff --git a/src/main/java/li/strolch/persistence/inmemory/InMemoryTransaction.java b/src/main/java/li/strolch/persistence/inmemory/InMemoryTransaction.java index b0861a870..6f49f04d1 100644 --- a/src/main/java/li/strolch/persistence/inmemory/InMemoryTransaction.java +++ b/src/main/java/li/strolch/persistence/inmemory/InMemoryTransaction.java @@ -17,7 +17,7 @@ import ch.eitchnet.utils.helper.StringHelper; public class InMemoryTransaction extends AbstractTransaction { - private InMemoryPersistenceHandler persistenceHandler; + private InMemoryPersistence persistenceHandler; private TransactionCloseStrategy closeStrategy; private ObserverHandler observerHandler; @@ -28,7 +28,7 @@ public class InMemoryTransaction extends AbstractTransaction { private TransactionResult txResult; private boolean open; - public InMemoryTransaction(StrolchRealm realm, InMemoryPersistenceHandler persistenceHandler) { + public InMemoryTransaction(StrolchRealm realm, InMemoryPersistence persistenceHandler) { super(realm); this.persistenceHandler = persistenceHandler; this.startTime = System.nanoTime(); diff --git a/src/main/java/li/strolch/service/api/AbstractService.java b/src/main/java/li/strolch/service/api/AbstractService.java index 3e9456de1..c9b511cc7 100644 --- a/src/main/java/li/strolch/service/api/AbstractService.java +++ b/src/main/java/li/strolch/service/api/AbstractService.java @@ -18,7 +18,7 @@ package li.strolch.service.api; import java.text.MessageFormat; import li.strolch.agent.api.ComponentContainer; -import li.strolch.agent.api.ElementMapHandler; +import li.strolch.agent.api.RealmHandler; import li.strolch.agent.impl.StrolchRealm; import li.strolch.exception.StrolchException; import li.strolch.persistence.api.StrolchTransaction; @@ -79,11 +79,11 @@ public abstract class AbstractService realmNames = container.getRealmNames(); + assertEquals(6, realmNames.size()); + + Set expectedRealmNames = new HashSet<>(Arrays.asList("defaultRealm", "myRealm", "otherRealm", + "cachedRealm", "transactionalRealm", "emptyRealm")); + assertEquals(expectedRealmNames, realmNames); + + assertEquals(DataStoreMode.TRANSIENT, container.getRealm("defaultRealm").getMode()); + assertEquals(DataStoreMode.TRANSIENT, container.getRealm("myRealm").getMode()); + assertEquals(DataStoreMode.TRANSIENT, container.getRealm("otherRealm").getMode()); + assertEquals(DataStoreMode.CACHED, container.getRealm("cachedRealm").getMode()); + assertEquals(DataStoreMode.TRANSACTIONAL, container.getRealm("transactionalRealm").getMode()); + assertEquals(DataStoreMode.EMPTY, container.getRealm("emptyRealm").getMode()); + destroyContainer(agent); } catch (Exception e) { logger.error(e.getMessage(), e); diff --git a/src/test/java/li/strolch/runtime/configuration/ControllerDependencyTest.java b/src/test/java/li/strolch/runtime/configuration/ControllerDependencyTest.java index a86481a2e..f9f3d9591 100644 --- a/src/test/java/li/strolch/runtime/configuration/ControllerDependencyTest.java +++ b/src/test/java/li/strolch/runtime/configuration/ControllerDependencyTest.java @@ -29,10 +29,6 @@ import li.strolch.agent.api.StrolchComponent; import li.strolch.agent.impl.ComponentContainerImpl; import li.strolch.agent.impl.ComponentController; import li.strolch.agent.impl.ComponentDependencyAnalyzer; -import li.strolch.agent.impl.DataStoreMode; -import li.strolch.runtime.configuration.ConfigurationParser; -import li.strolch.runtime.configuration.StrolchConfiguration; -import li.strolch.runtime.configuration.StrolchConfigurationException; import org.junit.Before; import org.junit.Rule; @@ -111,7 +107,7 @@ public class ControllerDependencyTest { @Before public void setupModel() { - this.container = new ComponentContainerImpl(null, DataStoreMode.EMPTY); + this.container = new ComponentContainerImpl(null); this.con2 = new ComponentController(new StrolchComponent(this.container, "2")); this.con5 = new ComponentController(new StrolchComponent(this.container, "5")); @@ -342,7 +338,6 @@ public class ControllerDependencyTest { this.con9.addUpstreamDependency(this.con3); } - @Test public void shouldNotBreakModel1() { assertModel(); @@ -540,7 +535,7 @@ public class ControllerDependencyTest { @Test public void shouldAddDepedencies() { - ComponentContainerImpl container = new ComponentContainerImpl(null, DataStoreMode.EMPTY); + ComponentContainerImpl container = new ComponentContainerImpl(null); StrolchComponent component = new StrolchComponent(container, "1"); //$NON-NLS-1$ ComponentController controller = new ComponentController(component); diff --git a/src/test/resources/cachedtest/config/StrolchConfiguration.xml b/src/test/resources/cachedtest/config/StrolchConfiguration.xml index d84bc52d3..111bb622d 100644 --- a/src/test/resources/cachedtest/config/StrolchConfiguration.xml +++ b/src/test/resources/cachedtest/config/StrolchConfiguration.xml @@ -3,16 +3,24 @@ StrolchRuntimeTest - CACHED - StrolchModel.xml true + + RealmHandler + li.strolch.agent.api.RealmHandler + li.strolch.agent.impl.DefaultRealmHandler + PersistenceHandler + + CACHED + StrolchModel.xml + + ServiceHandler li.strolch.runtime.configuration.model.ServiceHandlerTest li.strolch.runtime.configuration.model.ServiceHandlerTestImpl - PersistenceHandler + RealmHandler diff --git a/src/test/resources/configtest/config/StrolchConfiguration.xml b/src/test/resources/configtest/config/StrolchConfiguration.xml index d7892e019..add12441e 100644 --- a/src/test/resources/configtest/config/StrolchConfiguration.xml +++ b/src/test/resources/configtest/config/StrolchConfiguration.xml @@ -3,15 +3,22 @@ StrolchRuntimeTest - EMPTY true + + RealmHandler + li.strolch.agent.api.RealmHandler + li.strolch.agent.impl.DefaultRealmHandler + + EMPTY + + ServiceHandler li.strolch.service.api.ServiceHandler li.strolch.service.SimpleServiceHandler - Agent + RealmHandler diff --git a/src/test/resources/emptytest/config/StrolchConfiguration.xml b/src/test/resources/emptytest/config/StrolchConfiguration.xml index af27866e8..8c3fd34c9 100644 --- a/src/test/resources/emptytest/config/StrolchConfiguration.xml +++ b/src/test/resources/emptytest/config/StrolchConfiguration.xml @@ -3,14 +3,22 @@ StrolchRuntimeTest - EMPTY true + + RealmHandler + li.strolch.agent.api.RealmHandler + li.strolch.agent.impl.DefaultRealmHandler + + EMPTY + + ServiceHandler li.strolch.runtime.configuration.model.ServiceHandlerTest li.strolch.runtime.configuration.model.ServiceHandlerTestImpl + RealmHandler diff --git a/src/test/resources/realmtest/config/StrolchConfiguration.xml b/src/test/resources/realmtest/config/StrolchConfiguration.xml index 3591b5391..59a99a3c1 100644 --- a/src/test/resources/realmtest/config/StrolchConfiguration.xml +++ b/src/test/resources/realmtest/config/StrolchConfiguration.xml @@ -4,18 +4,31 @@ StrolchRuntimeTest TRANSIENT - defaultRealm, myRealm, otherRealm - DefaultRealm.xml - MyRealm.xml - OtherRealm.xml true + + RealmHandler + li.strolch.agent.api.RealmHandler + li.strolch.agent.impl.DefaultRealmHandler + + defaultRealm, myRealm, otherRealm, cachedRealm, transactionalRealm, emptyRealm + TRANSIENT + DefaultRealm.xml + TRANSIENT + MyRealm.xml + TRANSIENT + OtherRealm.xml + TRANSACTIONAL + CACHED + EMPTY + + ServiceHandler li.strolch.runtime.configuration.model.ServiceHandlerTest li.strolch.runtime.configuration.model.ServiceHandlerTestImpl - PersistenceHandler + RealmHandler @@ -35,4 +48,9 @@ true + + PersistenceHandler + li.strolch.persistence.api.PersistenceHandler + li.strolch.persistence.inmemory.InMemoryPersistenceHandler + \ No newline at end of file diff --git a/src/test/resources/transactionaltest/config/StrolchConfiguration.xml b/src/test/resources/transactionaltest/config/StrolchConfiguration.xml index b5b9e434a..fed410642 100644 --- a/src/test/resources/transactionaltest/config/StrolchConfiguration.xml +++ b/src/test/resources/transactionaltest/config/StrolchConfiguration.xml @@ -3,16 +3,24 @@ StrolchRuntimeTest - TRANSACTIONAL - StrolchModel.xml true + + RealmHandler + li.strolch.agent.api.RealmHandler + li.strolch.agent.impl.DefaultRealmHandler + PersistenceHandler + + TRANSACTIONAL + StrolchModel.xml + + ServiceHandler li.strolch.runtime.configuration.model.ServiceHandlerTest li.strolch.runtime.configuration.model.ServiceHandlerTestImpl - PersistenceHandler + RealmHandler diff --git a/src/test/resources/transienttest/config/StrolchConfiguration.xml b/src/test/resources/transienttest/config/StrolchConfiguration.xml index ec5b0bff5..43df69343 100644 --- a/src/test/resources/transienttest/config/StrolchConfiguration.xml +++ b/src/test/resources/transienttest/config/StrolchConfiguration.xml @@ -3,16 +3,24 @@ StrolchRuntimeTest - TRANSIENT - StrolchModel.xml true + + RealmHandler + li.strolch.agent.api.RealmHandler + li.strolch.agent.impl.DefaultRealmHandler + + + TRANSIENT + StrolchModel.xml + + ServiceHandler li.strolch.runtime.configuration.model.ServiceHandlerTest li.strolch.runtime.configuration.model.ServiceHandlerTestImpl - PersistenceHandler + RealmHandler @@ -36,7 +44,7 @@ EnumHandler li.strolch.runtime.query.enums.EnumHandler li.strolch.runtime.query.enums.DefaultEnumHandler - ElementMapHandler + RealmHandler defaultRealm Resource/Enumeration/salutations