[New] Implemented CACHED mode for Strolch

This commit is contained in:
Robert von Burg 2013-12-24 00:18:06 +01:00
parent 2686e3d7bf
commit 435b11c5ef
15 changed files with 205 additions and 24 deletions

View File

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

View File

@ -0,0 +1,32 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* 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 <eitch@eitchnet.ch>
*/
@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";
}

View File

@ -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 <eitch@eitchnet.ch>
@ -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$

View File

@ -0,0 +1,88 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* 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 <eitch@eitchnet.ch>
*/
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<String> resourceTypes = resourceDao.queryTypes();
for (String type : resourceTypes) {
List<Resource> resources = resourceDao.queryAll(type);
for (Resource resource : resources) {
resourceMap.add(resource);
nrOfResources++;
}
}
OrderDao orderDao = persistenceHandler.getOrderDao(tx);
Set<String> orderTypes = orderDao.queryTypes();
for (String type : orderTypes) {
List<Order> 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();
}
}

View File

@ -0,0 +1,49 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* 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 <eitch@eitchnet.ch>
*/
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<String, String> configurationValues = new HashMap<>();
Set<String> 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;
}
}

View File

@ -17,16 +17,21 @@ package li.strolch.runtime.agent;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*
*/
public interface ComponentContainer {
public abstract ComponentState getState();
public abstract boolean hasComponent(Class<?> clazz);
public abstract <T> T getComponent(Class<T> clazz);
public abstract OrderMap getOrderMap();
public abstract OrderMap getOrderMap(String realm);
public abstract ResourceMap getResourceMap();
public abstract ResourceMap getResourceMap(String realm);
}

View File

@ -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<Class<?>, StrolchComponent> componentMap,
Map<String, ComponentController> controllerMap, ComponentConfiguration componentConfiguration) {
@ -98,8 +108,8 @@ public class ComponentContainerImpl implements ComponentContainer {
@SuppressWarnings("unchecked")
Class<StrolchComponent> strolchComponentClass = (Class<StrolchComponent>) implClass;
Constructor<StrolchComponent> constructor = strolchComponentClass.getConstructor(ComponentContainerImpl.class,
String.class);
Constructor<StrolchComponent> constructor = strolchComponentClass.getConstructor(
ComponentContainerImpl.class, String.class);
StrolchComponent strolchComponent = constructor.newInstance(this, componentName);
componentMap.put(apiClass, strolchComponent);

View File

@ -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 {

View File

@ -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) {

View File

@ -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);
}

View File

@ -15,15 +15,11 @@
*/
package li.strolch.runtime.agent;
import li.strolch.model.query.StrolchQuery;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class StrolchRealm {
public static final String DEFAULT_REALM = StrolchQuery.DEFAULT_REALM;
private String realm;
private ResourceMap resourceMap;
private OrderMap orderMap;

View File

@ -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$
}

View File

@ -23,7 +23,6 @@ import ch.eitchnet.privilege.model.PrivilegeContext;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public interface StrolchPrivilegeHandler {

View File

@ -85,7 +85,7 @@ public class ConfigurationParserTest {
// <Component>
// <name>PersistenceHandler</name>
// <api>li.strolch.persistence.api.StrolchPersistenceHandler</api>
// <api>li.strolch.persistence.api.PersistenceHandler</api>
// <impl>li.strolch.persistence.impl.XmlPersistenceHandler</impl>
// <Properties>
// <verbose>true</verbose>
@ -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));

View File

@ -26,7 +26,7 @@
</Component>
<Component>
<name>PersistenceHandler</name>
<api>li.strolch.persistence.api.StrolchPersistenceHandler</api>
<api>li.strolch.persistence.api.PersistenceHandler</api>
<impl>li.strolch.persistence.impl.XmlPersistenceHandler</impl>
<Properties>
<verbose>true</verbose>