[Devel] implementing InMemory maps for EMPTY and TRANSIENT modes

This commit is contained in:
Robert von Burg 2013-11-21 18:20:06 +01:00
parent 567ec02814
commit 3a2f4495dd
14 changed files with 177 additions and 201 deletions

View File

@ -23,6 +23,7 @@ package li.strolch.runtime.agent;
import java.text.MessageFormat;
import li.strolch.runtime.component.ComponentContainer;
import li.strolch.runtime.configuration.ComponentConfiguration;
import org.slf4j.Logger;
@ -40,8 +41,8 @@ public class EmptyDataStoreModeAgentInitializer implements AgentLifecycleControl
protected StrolchAgent strolchAgent;
protected ComponentConfiguration configuration;
protected OrderMap orderMap;
protected ResourceMap resourceMap;
protected InMemoryOrderMap orderMap;
protected InMemoryResourceMap resourceMap;
/**
* @param strolchAgent
@ -65,8 +66,12 @@ public class EmptyDataStoreModeAgentInitializer implements AgentLifecycleControl
@Override
public void initialize() {
this.resourceMap = new InMemoryResourceMap();
this.orderMap = new InMemoryOrderMap();
ComponentContainer container = this.strolchAgent.getContainer();
this.resourceMap = new InMemoryResourceMap(container);
this.orderMap = new InMemoryOrderMap(container);
this.resourceMap.initialize(this.configuration);
this.orderMap.initialize(this.configuration);
}
@Override

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2012, Robert von Burg
*
* All rights reserved.
*
* This file is part of the XXX.
*
* XXX is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* XXX is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XXX. If not, see
* <http://www.gnu.org/licenses/>.
*/
package li.strolch.runtime.agent;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.xml.StrolchElementListener;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public class InMemoryElementListener implements StrolchElementListener {
private ResourceMap resourceMap;
private OrderMap orderMap;
public InMemoryElementListener(ResourceMap resourceMap, OrderMap orderMap) {
this.resourceMap = resourceMap;
this.orderMap = orderMap;
}
@Override
public void notifyResource(Resource resource) {
this.resourceMap.add(resource);
}
@Override
public void notifyOrder(Order order) {
this.orderMap.add(order);
}
}

View File

@ -26,16 +26,45 @@ import java.util.HashMap;
import java.util.Map;
import li.strolch.model.StrolchElement;
import li.strolch.runtime.component.ComponentContainer;
import li.strolch.runtime.component.StrolchComponent;
import li.strolch.runtime.configuration.ComponentConfiguration;
import ch.eitchnet.utils.helper.StringHelper;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public abstract class InMemoryElementMap<T extends StrolchElement> implements ElementMap<T> {
public abstract class InMemoryElementMap<T extends StrolchElement> extends StrolchComponent implements ElementMap<T> {
/**
* @param container
* @param componentName
*/
public InMemoryElementMap(ComponentContainer container, String componentName) {
super(container, componentName);
}
private Map<String, Map<String, T>> elementMap;
@Override
public void initialize(ComponentConfiguration configuration) {
this.elementMap = new HashMap<>();
super.initialize(configuration);
}
@Override
public void stop() {
this.elementMap.clear();
super.stop();
}
@Override
public void destroy() {
this.elementMap = null;
super.destroy();
}
@Override
public T getBy(String type, String id) {
if (StringHelper.isEmpty(type) || StringHelper.isEmpty(id))

View File

@ -22,6 +22,7 @@
package li.strolch.runtime.agent;
import li.strolch.model.Order;
import li.strolch.runtime.component.ComponentContainer;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -29,5 +30,10 @@ import li.strolch.model.Order;
*/
public class InMemoryOrderMap extends InMemoryElementMap<Order> implements OrderMap {
//
/**
* @param container
*/
public InMemoryOrderMap(ComponentContainer container) {
super(container, OrderMap.class.getSimpleName());
}
}

View File

@ -22,6 +22,7 @@
package li.strolch.runtime.agent;
import li.strolch.model.Resource;
import li.strolch.runtime.component.ComponentContainer;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -29,5 +30,10 @@ import li.strolch.model.Resource;
*/
public class InMemoryResourceMap extends InMemoryElementMap<Resource> implements ResourceMap {
//
/**
* @param container
*/
public InMemoryResourceMap(ComponentContainer container) {
super(container, ResourceMap.class.getSimpleName());
}
}

View File

@ -1,188 +0,0 @@
/*
* Copyright (c) 2012, Robert von Burg
*
* All rights reserved.
*
* This file is part of the XXX.
*
* XXX is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* XXX is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XXX. If not, see
* <http://www.gnu.org/licenses/>.
*/
package li.strolch.runtime.agent;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Date;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import li.strolch.exception.StrolchException;
import li.strolch.model.Order;
import li.strolch.model.ParameterBag;
import li.strolch.model.Resource;
import li.strolch.model.State;
import li.strolch.model.Tags;
import li.strolch.model.parameter.BooleanParameter;
import li.strolch.model.parameter.DateParameter;
import li.strolch.model.parameter.FloatParameter;
import li.strolch.model.parameter.IntegerParameter;
import li.strolch.model.parameter.LongParameter;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.parameter.StringListParameter;
import li.strolch.model.parameter.StringParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import ch.eitchnet.utils.helper.StringHelper;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public class ModelFileDefaultHandler extends DefaultHandler {
private static final Logger logger = LoggerFactory.getLogger(ModelFileDefaultHandler.class);
private File modelFile;
private Resource resource;
private Order order;
private ParameterBag pBag;
public ModelFileDefaultHandler(File modelFile) {
this.modelFile = modelFile;
}
@SuppressWarnings("nls")
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
switch (qName) {
case Tags.RESOURCE:
String resId = attributes.getValue(Tags.ID);
String resName = attributes.getValue(Tags.NAME);
String resType = attributes.getValue(Tags.TYPE);
Resource resource = new Resource(resId, resName, resType);
this.resource = resource;
break;
case Tags.ORDER:
String orderId = attributes.getValue(Tags.ID);
String orderName = attributes.getValue(Tags.NAME);
String orderType = attributes.getValue(Tags.TYPE);
String orderDateS = attributes.getValue(Tags.DATE);
String orderStateS = attributes.getValue(Tags.STATE);
Date orderDate = ISO8601FormatFactory.getInstance().getDateFormat().parse(orderDateS);
State orderState = State.valueOf(orderStateS);
Order order = new Order(orderId, orderName, orderType, orderDate, orderState);
this.order = order;
break;
case Tags.PARAMETER_BAG:
String pBagId = attributes.getValue(Tags.ID);
String pBagName = attributes.getValue(Tags.NAME);
String pBagType = attributes.getValue(Tags.TYPE);
ParameterBag pBag = new ParameterBag(pBagId, pBagName, pBagType);
this.pBag = pBag;
break;
case Tags.PARAMETER:
String paramId = attributes.getValue(Tags.ID);
String paramName = attributes.getValue(Tags.NAME);
String paramType = attributes.getValue(Tags.TYPE);
String paramValue = attributes.getValue(Tags.VALUE);
String paramHiddenS = attributes.getValue(Tags.HIDDEN);
boolean paramHidden = StringHelper.parseBoolean(paramHiddenS);
String paramUom = attributes.getValue(Tags.UOM);
String paramInterpretation = attributes.getValue(Tags.INTERPRETATION);
Parameter<?> param;
switch (paramType) {
case StringParameter.TYPE:
param = new StringParameter(paramId, paramName, paramValue);
break;
case IntegerParameter.TYPE:
param = new IntegerParameter(paramId, paramName, IntegerParameter.parseFromString(paramValue));
break;
case BooleanParameter.TYPE:
param = new BooleanParameter(paramId, paramName, BooleanParameter.parseFromString(paramValue));
break;
case LongParameter.TYPE:
param = new LongParameter(paramId, paramName, LongParameter.parseFromString(paramValue));
break;
case DateParameter.TYPE:
param = new DateParameter(paramId, paramName, DateParameter.parseFromString(paramValue));
break;
case StringListParameter.TYPE:
param = new StringListParameter(paramId, paramName, StringListParameter.parseFromString(paramValue));
break;
case FloatParameter.TYPE:
param = new FloatParameter(paramId, paramName, FloatParameter.parseFromString(paramValue));
break;
default:
throw new UnsupportedOperationException("Parameters of type " + paramType + " are not supported!");
}
param.setHidden(paramHidden);
param.setUom(paramUom);
param.setInterpretation(paramInterpretation);
this.pBag.addParameter(param);
break;
default:
throw new IllegalArgumentException("The element '" + qName + "' is unhandled!");
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
switch (qName) {
case Tags.RESOURCE:
this.resource = null;
break;
case Tags.ORDER:
this.order = null;
break;
case Tags.PARAMETER_BAG:
this.pBag = null;
break;
case Tags.PARAMETER:
break;
default:
throw new IllegalArgumentException("The element '" + qName + "' is unhandled!");
}
}
public void parseFile() {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
sp.parse(this.modelFile, this);
String msg = "SAX parsed model file {0}"; //$NON-NLS-1$
logger.info(MessageFormat.format(msg, this.modelFile.getAbsolutePath()));
} catch (ParserConfigurationException | SAXException | IOException e) {
String msg = "Parsing failed due to internal error: {0}"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, e.getMessage()), e);
}
}
}

View File

@ -22,9 +22,13 @@
package li.strolch.runtime.agent;
import java.io.File;
import java.text.MessageFormat;
import li.strolch.model.xml.XmlModelDefaultHandler;
import li.strolch.model.xml.XmlModelDefaultHandler.XmlModelStatistics;
import li.strolch.runtime.configuration.ComponentConfiguration;
import li.strolch.runtime.configuration.RuntimeConfiguration;
import ch.eitchnet.utils.helper.StringHelper;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -46,8 +50,14 @@ public class TransientDataStoreModeAgentInitializer extends EmptyDataStoreModeAg
RuntimeConfiguration runtimeConfiguration = this.configuration.getRuntimeConfiguration();
File modelFile = this.configuration.getDataFile(PROP_DATA_STORE_MODEL_FILE, null, runtimeConfiguration, true);
ModelFileDefaultHandler handler = new ModelFileDefaultHandler(modelFile);
InMemoryElementListener elementListener = new InMemoryElementListener(this.resourceMap, this.orderMap);
XmlModelDefaultHandler handler = new XmlModelDefaultHandler(elementListener, modelFile);
handler.parseFile();
XmlModelStatistics statistics = handler.getStatistics();
String durationS = StringHelper.formatNanoDuration(statistics.durationNanos);
logger.info(MessageFormat.format("Loading XML Model file {0} took {1}.", modelFile.getName(), 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();
}

View File

@ -31,7 +31,7 @@ public class StrolchComponent {
return this.state;
}
protected ComponentContainer getContainer() {
public ComponentContainer getContainer() {
return this.container;
}

View File

@ -31,7 +31,7 @@ public class RuntimeConfiguration extends AbstractionConfiguration {
throw new StrolchConfigurationException(msg);
}
File dataPathF = new File(rootPathF, PATH_CONFIG);
File dataPathF = new File(rootPathF, PATH_DATA);
if (!dataPathF.exists() && !dataPathF.mkdir()) {
String msg = "Could not create missing data path at {0}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, configPathF);

View File

@ -12,7 +12,6 @@
<impl>li.strolch.runtime.agent.StrolchAgent</impl>
<Properties>
<dataStoreMode>EMPTY</dataStoreMode>
<dataStoreModelFile>StrolcheModel.xml</dataStoreModelFile>
</Properties>
</Component>
<Component>

View File

@ -11,8 +11,8 @@
<api>li.strolch.runtime.agent.StrolchAgent</api>
<impl>li.strolch.runtime.agent.StrolchAgent</impl>
<Properties>
<dataStoreMode>EMPTY</dataStoreMode>
<dataStoreModelFile>StrolcheModel.xml</dataStoreModelFile>
<dataStoreMode>TRANSIENT</dataStoreMode>
<dataStoreModelFile>StrolchModel.xml</dataStoreModelFile>
</Properties>
</Component>
<Component>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<StrolchModel>
<Order Id="MyTestOrder" Name="Test Name" Type="TestType" Date="2013-11-20T07:42:57.699+01:00" State="CREATED">
<ParameterBag Id="@bag01" Name="Test Bag" Type="TestBag">
<Parameter Id="@param7" Name="StringList Param" Type="StringList" Value="Hello;World" />
<Parameter Id="@param6" Name="Date Param" Type="Date" Value="2012-11-30T18:12:05.628+01:00" />
<Parameter Id="@param5" Name="String Param" Type="String" Value="Strolch" />
<Parameter Id="@param4" Name="Long Param" Type="Long" Value="4453234566" />
<Parameter Id="@param3" Name="Integer Param" Type="Integer" Value="77" />
<Parameter Id="@param2" Name="Float Param" Type="Float" Value="44.3" />
<Parameter Id="@param1" Name="Boolean Param" Type="Boolean" Value="true" />
</ParameterBag>
</Order>
</StrolchModel>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<StrolchModel>
<Resource Id="MyTestResource" Name="Test Name" Type="TestType">
<ParameterBag Id="@bag01" Name="Test Bag" Type="TestBag">
<Parameter Id="@param7" Name="StringList Param" Type="StringList" Value="Hello;World" />
<Parameter Id="@param6" Name="Date Param" Type="Date" Value="2012-11-30T18:12:05.628+01:00" />
<Parameter Id="@param5" Name="String Param" Type="String" Value="Strolch" />
<Parameter Id="@param4" Name="Long Param" Type="Long" Value="4453234566" />
<Parameter Id="@param3" Name="Integer Param" Type="Integer" Value="77" />
<Parameter Id="@param2" Name="Float Param" Type="Float" Value="44.3" />
<Parameter Id="@param1" Name="Boolean Param" Type="Boolean" Value="true" />
</ParameterBag>
</Resource>
</StrolchModel>

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<StrolchModel>
<Resource Id="TestType" Name="TestType Template" Type="Template">
<ParameterBag Id="@bag01" Name="Test Bag" Type="TestBag">
<Parameter Id="@param7" Name="StringList Param" Type="StringList" Value="Hello;World" />
<Parameter Id="@param6" Name="Date Param" Type="Date" Value="2012-11-30T18:12:05.628+01:00" />
<Parameter Id="@param5" Name="String Param" Type="String" Value="Strolch" />
<Parameter Id="@param4" Name="Long Param" Type="Long" Value="4453234566" />
<Parameter Id="@param3" Name="Integer Param" Type="Integer" Value="77" />
<Parameter Id="@param2" Name="Float Param" Type="Float" Value="44.3" />
<Parameter Id="@param1" Name="Boolean Param" Type="Boolean" Value="true" />
</ParameterBag>
</Resource>
<Order Id="Template" Name="MyTestOrder Template" Type="MyTestOrder">
<ParameterBag Id="@bag01" Name="Test Bag" Type="TestBag">
<Parameter Id="@param7" Name="StringList Param" Type="StringList" Value="Hello;World" />
<Parameter Id="@param6" Name="Date Param" Type="Date" Value="2012-11-30T18:12:05.628+01:00" />
<Parameter Id="@param5" Name="String Param" Type="String" Value="Strolch" />
<Parameter Id="@param4" Name="Long Param" Type="Long" Value="4453234566" />
<Parameter Id="@param3" Name="Integer Param" Type="Integer" Value="77" />
<Parameter Id="@param2" Name="Float Param" Type="Float" Value="44.3" />
<Parameter Id="@param1" Name="Boolean Param" Type="Boolean" Value="true" />
</ParameterBag>
</Order>
<IncludeFile file="Resources.xml" />
<IncludeFile file="Orders.xml" />
</StrolchModel>