[New] TransientRealm now disallows updates, i.e. duplicates in XML model

This commit is contained in:
Robert von Burg 2015-08-24 09:50:46 +02:00
parent 4e6f7a916f
commit 4de7dfba85
3 changed files with 38 additions and 7 deletions

@ -1 +1 @@
Subproject commit 4b9e166025c85df5c427ed1e15bc3a1d98ff9252 Subproject commit 363c21d30a9631d94adf5a4b508f2ddb8494ec27

View File

@ -15,12 +15,14 @@
*/ */
package li.strolch.agent.impl; package li.strolch.agent.impl;
import java.text.MessageFormat;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
import li.strolch.agent.api.ActivityMap; import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.OrderMap; import li.strolch.agent.api.OrderMap;
import li.strolch.agent.api.ResourceMap; import li.strolch.agent.api.ResourceMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.Order; import li.strolch.model.Order;
import li.strolch.model.Resource; import li.strolch.model.Resource;
import li.strolch.model.activity.Activity; import li.strolch.model.activity.Activity;
@ -42,6 +44,8 @@ public class InMemoryElementListener implements StrolchElementListener {
private Set<String> resourceTypes; private Set<String> resourceTypes;
private Set<String> activityTypes; private Set<String> activityTypes;
private boolean failOnUpdate;
private StrolchTransaction tx; private StrolchTransaction tx;
private ResourceMap resourceMap; private ResourceMap resourceMap;
private OrderMap orderMap; private OrderMap orderMap;
@ -136,6 +140,14 @@ public class InMemoryElementListener implements StrolchElementListener {
this.activityTypes = activityTypes; this.activityTypes = activityTypes;
} }
/**
* @param failOnUpdate
* the failOnUpdate to set
*/
public void setFailOnUpdate(boolean failOnUpdate) {
this.failOnUpdate = failOnUpdate;
}
@Override @Override
public void notifyResource(Resource resource) { public void notifyResource(Resource resource) {
if (!this.resourceTypes.isEmpty() && !this.resourceTypes.contains(resource.getType())) 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.resourceMap.hasElement(this.tx, resource.getType(), resource.getId())) {
if (this.updateResources) { if (this.updateResources) {
this.resourceMap.update(this.tx, resource); 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) { } else if (this.addResources) {
this.resourceMap.add(this.tx, resource); this.resourceMap.add(this.tx, resource);
} }
// else ignore
} }
@Override @Override
@ -158,10 +174,14 @@ public class InMemoryElementListener implements StrolchElementListener {
if (this.orderMap.hasElement(this.tx, order.getType(), order.getId())) { if (this.orderMap.hasElement(this.tx, order.getType(), order.getId())) {
if (this.updateOrders) { if (this.updateOrders) {
this.orderMap.update(this.tx, order); 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) { } else if (this.addOrders) {
this.orderMap.add(this.tx, order); this.orderMap.add(this.tx, order);
} }
// else ignore
} }
@Override @Override
@ -172,9 +192,13 @@ public class InMemoryElementListener implements StrolchElementListener {
if (this.activityMap.hasElement(this.tx, activity.getType(), activity.getId())) { if (this.activityMap.hasElement(this.tx, activity.getType(), activity.getId())) {
if (this.updateActivities) { if (this.updateActivities) {
this.activityMap.update(this.tx, activity); 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) { } else if (this.addActivities) {
this.activityMap.add(this.tx, activity); this.activityMap.add(this.tx, activity);
} }
// else ignore
} }
} }

View File

@ -18,6 +18,10 @@ package li.strolch.agent.impl;
import java.io.File; import java.io.File;
import java.text.MessageFormat; 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.ActivityMap;
import li.strolch.agent.api.AuditTrail; import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.ComponentContainer; 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.StrolchConstants;
import li.strolch.runtime.configuration.ComponentConfiguration; import li.strolch.runtime.configuration.ComponentConfiguration;
import li.strolch.runtime.configuration.StrolchConfigurationException; 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 <eitch@eitchnet.ch> * @author Robert von Burg <eitch@eitchnet.ch>
@ -123,6 +123,13 @@ public class TransientRealm extends InternalStrolchRealm {
ModelStatistics statistics; ModelStatistics statistics;
try (StrolchTransaction tx = openTx(privilegeContext.getCertificate(), DefaultRealmHandler.AGENT_BOOT)) { try (StrolchTransaction tx = openTx(privilegeContext.getCertificate(), DefaultRealmHandler.AGENT_BOOT)) {
InMemoryElementListener elementListener = new InMemoryElementListener(tx); 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); XmlModelSaxFileReader handler = new XmlModelSaxFileReader(elementListener, this.modelFile, true);
handler.parseFile(); handler.parseFile();
statistics = handler.getStatistics(); statistics = handler.getStatistics();
@ -130,8 +137,8 @@ public class TransientRealm extends InternalStrolchRealm {
} }
String durationS = StringHelper.formatNanoDuration(statistics.durationNanos); String durationS = StringHelper.formatNanoDuration(statistics.durationNanos);
logger.info(MessageFormat.format( logger.info(MessageFormat.format("Loading XML Model file {0} for realm {1} took {2}.", this.modelFile.getName(), //$NON-NLS-1$
"Loading XML Model file {0} for realm {1} took {2}.", this.modelFile.getName(), getRealm(), durationS)); //$NON-NLS-1$ getRealm(), durationS));
logger.info(MessageFormat.format("Loaded {0} Orders", statistics.nrOfOrders)); //$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$ logger.info(MessageFormat.format("Loaded {0} Resources", statistics.nrOfResources)); //$NON-NLS-1$
logger.info(MessageFormat.format("Loaded {0} Activities", statistics.nrOfActivities)); //$NON-NLS-1$ logger.info(MessageFormat.format("Loaded {0} Activities", statistics.nrOfActivities)); //$NON-NLS-1$