From 4de7dfba8592a3cbd361f728507d51a7720e86be Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Mon, 24 Aug 2015 09:50:46 +0200 Subject: [PATCH] [New] TransientRealm now disallows updates, i.e. duplicates in XML model --- ch.eitchnet.utils | 2 +- .../agent/impl/InMemoryElementListener.java | 24 +++++++++++++++++++ .../li/strolch/agent/impl/TransientRealm.java | 19 ++++++++++----- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/ch.eitchnet.utils b/ch.eitchnet.utils index 4b9e16602..363c21d30 160000 --- a/ch.eitchnet.utils +++ b/ch.eitchnet.utils @@ -1 +1 @@ -Subproject commit 4b9e166025c85df5c427ed1e15bc3a1d98ff9252 +Subproject commit 363c21d30a9631d94adf5a4b508f2ddb8494ec27 diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/InMemoryElementListener.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/InMemoryElementListener.java index 8a818f23d..0b335a500 100644 --- a/li.strolch.agent/src/main/java/li/strolch/agent/impl/InMemoryElementListener.java +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/InMemoryElementListener.java @@ -15,12 +15,14 @@ */ package li.strolch.agent.impl; +import java.text.MessageFormat; import java.util.Collections; import java.util.Set; import li.strolch.agent.api.ActivityMap; import li.strolch.agent.api.OrderMap; import li.strolch.agent.api.ResourceMap; +import li.strolch.exception.StrolchException; import li.strolch.model.Order; import li.strolch.model.Resource; import li.strolch.model.activity.Activity; @@ -42,6 +44,8 @@ public class InMemoryElementListener implements StrolchElementListener { private Set resourceTypes; private Set activityTypes; + private boolean failOnUpdate; + private StrolchTransaction tx; private ResourceMap resourceMap; private OrderMap orderMap; @@ -136,6 +140,14 @@ public class InMemoryElementListener implements StrolchElementListener { this.activityTypes = activityTypes; } + /** + * @param failOnUpdate + * the failOnUpdate to set + */ + public void setFailOnUpdate(boolean failOnUpdate) { + this.failOnUpdate = failOnUpdate; + } + @Override public void notifyResource(Resource resource) { if (!this.resourceTypes.isEmpty() && !this.resourceTypes.contains(resource.getType())) @@ -144,10 +156,14 @@ public class InMemoryElementListener implements StrolchElementListener { if (this.resourceMap.hasElement(this.tx, resource.getType(), resource.getId())) { if (this.updateResources) { this.resourceMap.update(this.tx, resource); + } else if (this.failOnUpdate) { + throw new StrolchException(MessageFormat + .format("Resource {0} already exists and updating is disallowed!", resource.getLocator())); } } else if (this.addResources) { this.resourceMap.add(this.tx, resource); } + // else ignore } @Override @@ -158,10 +174,14 @@ public class InMemoryElementListener implements StrolchElementListener { if (this.orderMap.hasElement(this.tx, order.getType(), order.getId())) { if (this.updateOrders) { this.orderMap.update(this.tx, order); + } else if (failOnUpdate) { + throw new StrolchException(MessageFormat.format("Order {0} already exists and updating is disallowed!", + order.getLocator())); } } else if (this.addOrders) { this.orderMap.add(this.tx, order); } + // else ignore } @Override @@ -172,9 +192,13 @@ public class InMemoryElementListener implements StrolchElementListener { if (this.activityMap.hasElement(this.tx, activity.getType(), activity.getId())) { if (this.updateActivities) { this.activityMap.update(this.tx, activity); + } else if (failOnUpdate) { + throw new StrolchException(MessageFormat + .format("Activity {0} already exists and updating is disallowed!", activity.getLocator())); } } else if (this.addActivities) { this.activityMap.add(this.tx, activity); } + // else ignore } } diff --git a/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransientRealm.java b/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransientRealm.java index 755aa30b6..500aaa0d0 100644 --- a/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransientRealm.java +++ b/li.strolch.agent/src/main/java/li/strolch/agent/impl/TransientRealm.java @@ -18,6 +18,10 @@ package li.strolch.agent.impl; import java.io.File; import java.text.MessageFormat; +import ch.eitchnet.privilege.model.Certificate; +import ch.eitchnet.privilege.model.PrivilegeContext; +import ch.eitchnet.utils.dbc.DBC; +import ch.eitchnet.utils.helper.StringHelper; import li.strolch.agent.api.ActivityMap; import li.strolch.agent.api.AuditTrail; import li.strolch.agent.api.ComponentContainer; @@ -31,10 +35,6 @@ 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.privilege.model.Certificate; -import ch.eitchnet.privilege.model.PrivilegeContext; -import ch.eitchnet.utils.dbc.DBC; -import ch.eitchnet.utils.helper.StringHelper; /** * @author Robert von Burg @@ -123,6 +123,13 @@ public class TransientRealm extends InternalStrolchRealm { ModelStatistics statistics; try (StrolchTransaction tx = openTx(privilegeContext.getCertificate(), DefaultRealmHandler.AGENT_BOOT)) { InMemoryElementListener elementListener = new InMemoryElementListener(tx); + + // explicitly deny updating, so that we can detect XML files with duplicates + elementListener.setUpdateResources(false); + elementListener.setUpdateOrders(false); + elementListener.setUpdateActivities(false); + elementListener.setFailOnUpdate(true); + XmlModelSaxFileReader handler = new XmlModelSaxFileReader(elementListener, this.modelFile, true); handler.parseFile(); statistics = handler.getStatistics(); @@ -130,8 +137,8 @@ public class TransientRealm extends InternalStrolchRealm { } String durationS = StringHelper.formatNanoDuration(statistics.durationNanos); - logger.info(MessageFormat.format( - "Loading XML Model file {0} for realm {1} took {2}.", this.modelFile.getName(), getRealm(), durationS)); //$NON-NLS-1$ + logger.info(MessageFormat.format("Loading XML Model file {0} for realm {1} took {2}.", this.modelFile.getName(), //$NON-NLS-1$ + getRealm(), durationS)); logger.info(MessageFormat.format("Loaded {0} Orders", statistics.nrOfOrders)); //$NON-NLS-1$ logger.info(MessageFormat.format("Loaded {0} Resources", statistics.nrOfResources)); //$NON-NLS-1$ logger.info(MessageFormat.format("Loaded {0} Activities", statistics.nrOfActivities)); //$NON-NLS-1$