diff --git a/src/main/java/li/strolch/runtime/agent/EmptyDataStoreModeAgentInitializer.java b/src/main/java/li/strolch/runtime/agent/EmptyDataStoreModeAgentInitializer.java index 3a8bfc339..4d191ab55 100644 --- a/src/main/java/li/strolch/runtime/agent/EmptyDataStoreModeAgentInitializer.java +++ b/src/main/java/li/strolch/runtime/agent/EmptyDataStoreModeAgentInitializer.java @@ -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 diff --git a/src/main/java/li/strolch/runtime/agent/InMemoryElementListener.java b/src/main/java/li/strolch/runtime/agent/InMemoryElementListener.java new file mode 100644 index 000000000..9fd109a43 --- /dev/null +++ b/src/main/java/li/strolch/runtime/agent/InMemoryElementListener.java @@ -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 + * . + */ +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 + * + */ +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); + } + +} diff --git a/src/main/java/li/strolch/runtime/agent/InMemoryElementMap.java b/src/main/java/li/strolch/runtime/agent/InMemoryElementMap.java index 3ed3305c1..c6103b793 100644 --- a/src/main/java/li/strolch/runtime/agent/InMemoryElementMap.java +++ b/src/main/java/li/strolch/runtime/agent/InMemoryElementMap.java @@ -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 * */ -public abstract class InMemoryElementMap implements ElementMap { +public abstract class InMemoryElementMap extends StrolchComponent implements ElementMap { + + /** + * @param container + * @param componentName + */ + public InMemoryElementMap(ComponentContainer container, String componentName) { + super(container, componentName); + } private Map> 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)) diff --git a/src/main/java/li/strolch/runtime/agent/InMemoryOrderMap.java b/src/main/java/li/strolch/runtime/agent/InMemoryOrderMap.java index 1e3676b98..c1df62183 100644 --- a/src/main/java/li/strolch/runtime/agent/InMemoryOrderMap.java +++ b/src/main/java/li/strolch/runtime/agent/InMemoryOrderMap.java @@ -22,6 +22,7 @@ package li.strolch.runtime.agent; import li.strolch.model.Order; +import li.strolch.runtime.component.ComponentContainer; /** * @author Robert von Burg @@ -29,5 +30,10 @@ import li.strolch.model.Order; */ public class InMemoryOrderMap extends InMemoryElementMap implements OrderMap { - // + /** + * @param container + */ + public InMemoryOrderMap(ComponentContainer container) { + super(container, OrderMap.class.getSimpleName()); + } } diff --git a/src/main/java/li/strolch/runtime/agent/InMemoryResourceMap.java b/src/main/java/li/strolch/runtime/agent/InMemoryResourceMap.java index 7e74d45cd..ebd11d437 100644 --- a/src/main/java/li/strolch/runtime/agent/InMemoryResourceMap.java +++ b/src/main/java/li/strolch/runtime/agent/InMemoryResourceMap.java @@ -22,6 +22,7 @@ package li.strolch.runtime.agent; import li.strolch.model.Resource; +import li.strolch.runtime.component.ComponentContainer; /** * @author Robert von Burg @@ -29,5 +30,10 @@ import li.strolch.model.Resource; */ public class InMemoryResourceMap extends InMemoryElementMap implements ResourceMap { - // + /** + * @param container + */ + public InMemoryResourceMap(ComponentContainer container) { + super(container, ResourceMap.class.getSimpleName()); + } } diff --git a/src/main/java/li/strolch/runtime/agent/ModelFileDefaultHandler.java b/src/main/java/li/strolch/runtime/agent/ModelFileDefaultHandler.java deleted file mode 100644 index fc5c3fb39..000000000 --- a/src/main/java/li/strolch/runtime/agent/ModelFileDefaultHandler.java +++ /dev/null @@ -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 - * . - */ -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 - * - */ -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); - } - } -} diff --git a/src/main/java/li/strolch/runtime/agent/TransientDataStoreModeAgentInitializer.java b/src/main/java/li/strolch/runtime/agent/TransientDataStoreModeAgentInitializer.java index eb99ef9be..999b3c13f 100644 --- a/src/main/java/li/strolch/runtime/agent/TransientDataStoreModeAgentInitializer.java +++ b/src/main/java/li/strolch/runtime/agent/TransientDataStoreModeAgentInitializer.java @@ -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 @@ -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(); } diff --git a/src/main/java/li/strolch/runtime/component/StrolchComponent.java b/src/main/java/li/strolch/runtime/component/StrolchComponent.java index 189cbec02..d86f32f59 100644 --- a/src/main/java/li/strolch/runtime/component/StrolchComponent.java +++ b/src/main/java/li/strolch/runtime/component/StrolchComponent.java @@ -31,7 +31,7 @@ public class StrolchComponent { return this.state; } - protected ComponentContainer getContainer() { + public ComponentContainer getContainer() { return this.container; } diff --git a/src/main/java/li/strolch/runtime/configuration/RuntimeConfiguration.java b/src/main/java/li/strolch/runtime/configuration/RuntimeConfiguration.java index d99df62c0..e7852d67e 100644 --- a/src/main/java/li/strolch/runtime/configuration/RuntimeConfiguration.java +++ b/src/main/java/li/strolch/runtime/configuration/RuntimeConfiguration.java @@ -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); diff --git a/src/test/resources/configtest/config/StrolchConfiguration.xml b/src/test/resources/configtest/config/StrolchConfiguration.xml index 5fe013b7d..26062be7a 100644 --- a/src/test/resources/configtest/config/StrolchConfiguration.xml +++ b/src/test/resources/configtest/config/StrolchConfiguration.xml @@ -12,7 +12,6 @@ li.strolch.runtime.agent.StrolchAgent EMPTY - StrolcheModel.xml diff --git a/src/test/resources/runtimetest/config/StrolchConfiguration.xml b/src/test/resources/runtimetest/config/StrolchConfiguration.xml index 8877e00bc..3aaca6f0b 100644 --- a/src/test/resources/runtimetest/config/StrolchConfiguration.xml +++ b/src/test/resources/runtimetest/config/StrolchConfiguration.xml @@ -11,8 +11,8 @@ li.strolch.runtime.agent.StrolchAgent li.strolch.runtime.agent.StrolchAgent - EMPTY - StrolcheModel.xml + TRANSIENT + StrolchModel.xml diff --git a/src/test/resources/runtimetest/data/Orders.xml b/src/test/resources/runtimetest/data/Orders.xml new file mode 100644 index 000000000..55358bcaa --- /dev/null +++ b/src/test/resources/runtimetest/data/Orders.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/src/test/resources/runtimetest/data/Resources.xml b/src/test/resources/runtimetest/data/Resources.xml new file mode 100644 index 000000000..e6259cb83 --- /dev/null +++ b/src/test/resources/runtimetest/data/Resources.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/src/test/resources/runtimetest/data/StrolchModel.xml b/src/test/resources/runtimetest/data/StrolchModel.xml new file mode 100644 index 000000000..cb2396975 --- /dev/null +++ b/src/test/resources/runtimetest/data/StrolchModel.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file