diff --git a/pom.xml b/pom.xml index daae11db3..28f030fb2 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,10 @@ li.strolch li.strolch.model + + li.strolch + li.strolch.persistence.api + ch.eitchnet ch.eitchnet.privilege diff --git a/src/main/java/li/strolch/runtime/StrolchConstants.java b/src/main/java/li/strolch/runtime/StrolchConstants.java new file mode 100644 index 000000000..ca1238f2e --- /dev/null +++ b/src/main/java/li/strolch/runtime/StrolchConstants.java @@ -0,0 +1,32 @@ +/* + * 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.runtime; + +import li.strolch.persistence.api.PersistenceHandler; +import li.strolch.runtime.observer.ObserverHandler; + +/** + * @author Robert von Burg + */ +@SuppressWarnings("nls") +public class StrolchConstants { + + public static final String PERSISTENCE_HANDLER = PersistenceHandler.class.getSimpleName(); + public static final String OBSERVER_HANDLER = ObserverHandler.class.getSimpleName(); + public static final String PRIVILEGE_HANDLER = "PrivilegeHandler"; + + public static final String DEFAULT_REALM = "defaultRealm"; +} diff --git a/src/main/java/li/strolch/runtime/agent/AbstractElementMapHandler.java b/src/main/java/li/strolch/runtime/agent/AbstractElementMapHandler.java index 1a1dfbf4a..d35ee7598 100644 --- a/src/main/java/li/strolch/runtime/agent/AbstractElementMapHandler.java +++ b/src/main/java/li/strolch/runtime/agent/AbstractElementMapHandler.java @@ -19,7 +19,7 @@ import java.text.MessageFormat; import java.util.Map; import li.strolch.exception.StrolchException; -import li.strolch.model.query.StrolchQuery; +import li.strolch.runtime.StrolchConstants; /** * @author Robert von Burg @@ -38,7 +38,7 @@ public abstract class AbstractElementMapHandler extends StrolchComponent impleme @Override public ResourceMap getResourceMap() { - return getResourceMap(StrolchQuery.DEFAULT_REALM); + return getResourceMap(StrolchConstants.DEFAULT_REALM); } @Override @@ -49,7 +49,7 @@ public abstract class AbstractElementMapHandler extends StrolchComponent impleme @Override public OrderMap getOrderMap() { - return getOrderMap(StrolchQuery.DEFAULT_REALM); + return getOrderMap(StrolchConstants.DEFAULT_REALM); } @Override @@ -59,7 +59,6 @@ public abstract class AbstractElementMapHandler extends StrolchComponent impleme } private StrolchRealm getRealm(String realm) { - assertContainerStarted(); StrolchRealm strolchRealm = this.realms.get(realm); if (strolchRealm == null) { String msg = "No realm is configured with the name {0}"; //$NON-NLS-1$ diff --git a/src/main/java/li/strolch/runtime/agent/CachedElementMapHandler.java b/src/main/java/li/strolch/runtime/agent/CachedElementMapHandler.java new file mode 100644 index 000000000..8a2c9b854 --- /dev/null +++ b/src/main/java/li/strolch/runtime/agent/CachedElementMapHandler.java @@ -0,0 +1,88 @@ +/* + * 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.runtime.agent; + +import java.text.MessageFormat; +import java.util.List; +import java.util.Set; + +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 ch.eitchnet.utils.helper.StringHelper; + +/** + * @author Robert von Burg + */ +public class CachedElementMapHandler extends InMemoryElementMapHandler { + + /** + * @param container + * @param componentName + */ + public CachedElementMapHandler(ComponentContainerImpl 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; + + OrderMap orderMap = getContainer().getOrderMap(realm); + ResourceMap resourceMap = getContainer().getResourceMap(realm); + + PersistenceHandler persistenceHandler = getContainer().getComponent(PersistenceHandler.class); + try (StrolchTransaction tx = persistenceHandler.openTx(realm)) { + + ResourceDao resourceDao = persistenceHandler.getResourceDao(tx); + Set resourceTypes = resourceDao.queryTypes(); + for (String type : resourceTypes) { + List resources = resourceDao.queryAll(type); + for (Resource resource : resources) { + resourceMap.add(resource); + nrOfResources++; + } + } + + OrderDao orderDao = persistenceHandler.getOrderDao(tx); + Set orderTypes = orderDao.queryTypes(); + for (String type : orderTypes) { + List orders = orderDao.queryAll(type); + for (Order order : orders) { + orderMap.add(order); + 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/runtime/agent/CachedElementMapHandlerConfigurator.java b/src/main/java/li/strolch/runtime/agent/CachedElementMapHandlerConfigurator.java new file mode 100644 index 000000000..0876233b5 --- /dev/null +++ b/src/main/java/li/strolch/runtime/agent/CachedElementMapHandlerConfigurator.java @@ -0,0 +1,49 @@ +/* + * 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.runtime.agent; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +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 ComponentConfiguration buildConfiguration(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 configuration; + } +} diff --git a/src/main/java/li/strolch/runtime/agent/ComponentContainer.java b/src/main/java/li/strolch/runtime/agent/ComponentContainer.java index bd16018c8..88ecb35de 100644 --- a/src/main/java/li/strolch/runtime/agent/ComponentContainer.java +++ b/src/main/java/li/strolch/runtime/agent/ComponentContainer.java @@ -17,16 +17,21 @@ package li.strolch.runtime.agent; /** * @author Robert von Burg - * + * */ public interface ComponentContainer { + public abstract ComponentState getState(); + public abstract boolean hasComponent(Class clazz); public abstract T getComponent(Class clazz); public abstract OrderMap getOrderMap(); + public abstract OrderMap getOrderMap(String realm); + public abstract ResourceMap getResourceMap(); + public abstract ResourceMap getResourceMap(String realm); } \ No newline at end of file diff --git a/src/main/java/li/strolch/runtime/agent/ComponentContainerImpl.java b/src/main/java/li/strolch/runtime/agent/ComponentContainerImpl.java index 4b4acae80..94c229d45 100644 --- a/src/main/java/li/strolch/runtime/agent/ComponentContainerImpl.java +++ b/src/main/java/li/strolch/runtime/agent/ComponentContainerImpl.java @@ -69,11 +69,21 @@ public class ComponentContainerImpl implements ComponentContainer { return getComponent(ElementMapHandler.class).getOrderMap(); } + @Override + public OrderMap getOrderMap(String realm) { + return getComponent(ElementMapHandler.class).getOrderMap(realm); + } + @Override public ResourceMap getResourceMap() { return getComponent(ElementMapHandler.class).getResourceMap(); } + @Override + public ResourceMap getResourceMap(String realm) { + return getComponent(ElementMapHandler.class).getResourceMap(realm); + } + private void initializeComponent(Map, StrolchComponent> componentMap, Map controllerMap, ComponentConfiguration componentConfiguration) { @@ -98,8 +108,8 @@ public class ComponentContainerImpl implements ComponentContainer { @SuppressWarnings("unchecked") Class strolchComponentClass = (Class) implClass; - Constructor constructor = strolchComponentClass.getConstructor(ComponentContainerImpl.class, - String.class); + Constructor constructor = strolchComponentClass.getConstructor( + ComponentContainerImpl.class, String.class); StrolchComponent strolchComponent = constructor.newInstance(this, componentName); componentMap.put(apiClass, strolchComponent); diff --git a/src/main/java/li/strolch/runtime/agent/DataStoreMode.java b/src/main/java/li/strolch/runtime/agent/DataStoreMode.java index 99528fc24..49ed6ce89 100644 --- a/src/main/java/li/strolch/runtime/agent/DataStoreMode.java +++ b/src/main/java/li/strolch/runtime/agent/DataStoreMode.java @@ -37,7 +37,7 @@ public enum DataStoreMode { CACHED { @Override public ElementMapHandlerConfigurator getElementMapConfigurationConfigurator() { - throw new UnsupportedOperationException(MessageFormat.format("The mode {0} is not yet supported!", this)); //$NON-NLS-1$ + return new CachedElementMapHandlerConfigurator(); } }, // TRANSACTIONAL { diff --git a/src/main/java/li/strolch/runtime/agent/InMemoryElementMapHandler.java b/src/main/java/li/strolch/runtime/agent/InMemoryElementMapHandler.java index e997c978b..3aa562cf8 100644 --- a/src/main/java/li/strolch/runtime/agent/InMemoryElementMapHandler.java +++ b/src/main/java/li/strolch/runtime/agent/InMemoryElementMapHandler.java @@ -17,7 +17,7 @@ package li.strolch.runtime.agent; import java.util.HashMap; -import li.strolch.model.query.StrolchQuery; +import li.strolch.runtime.StrolchConstants; import li.strolch.runtime.configuration.ComponentConfiguration; import li.strolch.runtime.configuration.RuntimeConfiguration; @@ -38,7 +38,7 @@ public class InMemoryElementMapHandler extends AbstractElementMapHandler { public void initialize(ComponentConfiguration configuration) { RuntimeConfiguration runtimeConfiguration = configuration.getRuntimeConfiguration(); - String[] realms = runtimeConfiguration.getStringArray(StrolchAgent.PROP_REALMS, StrolchQuery.DEFAULT_REALM); + String[] realms = runtimeConfiguration.getStringArray(StrolchAgent.PROP_REALMS, StrolchConstants.DEFAULT_REALM); this.realms = new HashMap<>(); for (String realm : realms) { diff --git a/src/main/java/li/strolch/runtime/agent/StrolchComponent.java b/src/main/java/li/strolch/runtime/agent/StrolchComponent.java index 5ee0dcea7..845e38676 100644 --- a/src/main/java/li/strolch/runtime/agent/StrolchComponent.java +++ b/src/main/java/li/strolch/runtime/agent/StrolchComponent.java @@ -25,7 +25,7 @@ import org.slf4j.LoggerFactory; public class StrolchComponent { protected static final Logger logger = LoggerFactory.getLogger(StrolchComponent.class); - private final ComponentContainerImpl container; + private final ComponentContainer container; private final String componentName; private ComponentState state; @@ -46,7 +46,7 @@ public class StrolchComponent { return this.state; } - protected ComponentContainerImpl getContainer() { + protected ComponentContainer getContainer() { return this.container; } @@ -58,7 +58,7 @@ public class StrolchComponent { } protected void assertContainerStarted() { - if (getContainer().getState() != ComponentState.STARTED) { + if (this.container.getState() != ComponentState.STARTED) { String msg = "Container is not yet started!"; //$NON-NLS-1$ throw new IllegalStateException(msg); } diff --git a/src/main/java/li/strolch/runtime/agent/StrolchRealm.java b/src/main/java/li/strolch/runtime/agent/StrolchRealm.java index 1b8971cb6..40eef9036 100644 --- a/src/main/java/li/strolch/runtime/agent/StrolchRealm.java +++ b/src/main/java/li/strolch/runtime/agent/StrolchRealm.java @@ -15,15 +15,11 @@ */ package li.strolch.runtime.agent; -import li.strolch.model.query.StrolchQuery; - /** * @author Robert von Burg */ public class StrolchRealm { - public static final String DEFAULT_REALM = StrolchQuery.DEFAULT_REALM; - private String realm; private ResourceMap resourceMap; private OrderMap orderMap; diff --git a/src/main/java/li/strolch/runtime/agent/TransientElementMapHandler.java b/src/main/java/li/strolch/runtime/agent/TransientElementMapHandler.java index 3835bf833..b41007285 100644 --- a/src/main/java/li/strolch/runtime/agent/TransientElementMapHandler.java +++ b/src/main/java/li/strolch/runtime/agent/TransientElementMapHandler.java @@ -22,6 +22,7 @@ import java.util.Map; import li.strolch.model.xml.XmlModelDefaultHandler.XmlModelStatistics; import li.strolch.model.xml.XmlModelFileHandler; +import li.strolch.runtime.StrolchConstants; import li.strolch.runtime.configuration.ComponentConfiguration; import li.strolch.runtime.configuration.RuntimeConfiguration; import li.strolch.runtime.configuration.StrolchConfigurationException; @@ -60,12 +61,10 @@ public class TransientElementMapHandler extends InMemoryElementMapHandler { File modelFile = runtimeConfiguration.getDataFile(key, null, runtimeConfiguration, true); this.realmModelFiles.put(realm, modelFile); } - - super.initialize(configuration); } private String getDataStoreFilePropKey(String realm) { - if (realm.equals(StrolchRealm.DEFAULT_REALM)) + if (realm.equals(StrolchConstants.DEFAULT_REALM)) return StrolchAgent.PROP_DATA_STORE_FILE; return StrolchAgent.PROP_DATA_STORE_FILE + "." + realm; //$NON-NLS-1$ } diff --git a/src/main/java/li/strolch/runtime/privilege/StrolchPrivilegeHandler.java b/src/main/java/li/strolch/runtime/privilege/StrolchPrivilegeHandler.java index 6b9b5625c..2591278eb 100644 --- a/src/main/java/li/strolch/runtime/privilege/StrolchPrivilegeHandler.java +++ b/src/main/java/li/strolch/runtime/privilege/StrolchPrivilegeHandler.java @@ -23,7 +23,6 @@ import ch.eitchnet.privilege.model.PrivilegeContext; /** * @author Robert von Burg - * */ public interface StrolchPrivilegeHandler { diff --git a/src/test/java/li/strolch/runtime/test/component/ConfigurationParserTest.java b/src/test/java/li/strolch/runtime/test/component/ConfigurationParserTest.java index 9d546c6f8..1ccf48b8b 100644 --- a/src/test/java/li/strolch/runtime/test/component/ConfigurationParserTest.java +++ b/src/test/java/li/strolch/runtime/test/component/ConfigurationParserTest.java @@ -85,7 +85,7 @@ public class ConfigurationParserTest { // // PersistenceHandler - // li.strolch.persistence.api.StrolchPersistenceHandler + // li.strolch.persistence.api.PersistenceHandler // li.strolch.persistence.impl.XmlPersistenceHandler // // true @@ -95,7 +95,7 @@ public class ConfigurationParserTest { .getComponentConfiguration("PersistenceHandler"); assertNotNull("Should have created a PersistenceHandler Configuration", persistenceHandlerConfiguration); assertEquals("PersistenceHandler", persistenceHandlerConfiguration.getName()); - assertEquals("li.strolch.persistence.api.StrolchPersistenceHandler", persistenceHandlerConfiguration.getApi()); + assertEquals("li.strolch.persistence.api.PersistenceHandler", persistenceHandlerConfiguration.getApi()); assertEquals("li.strolch.persistence.impl.XmlPersistenceHandler", persistenceHandlerConfiguration.getImpl()); assertEquals(1, persistenceHandlerConfiguration.getPropertyKeys().size()); assertEquals(true, persistenceHandlerConfiguration.getBoolean("verbose", null)); diff --git a/src/test/resources/configtest/config/StrolchConfiguration.xml b/src/test/resources/configtest/config/StrolchConfiguration.xml index 464464034..97d1d0909 100644 --- a/src/test/resources/configtest/config/StrolchConfiguration.xml +++ b/src/test/resources/configtest/config/StrolchConfiguration.xml @@ -26,7 +26,7 @@ PersistenceHandler - li.strolch.persistence.api.StrolchPersistenceHandler + li.strolch.persistence.api.PersistenceHandler li.strolch.persistence.impl.XmlPersistenceHandler true