[New] TransientRealm now disallows updates, i.e. duplicates in XML model
This commit is contained in:
parent
4e6f7a916f
commit
4de7dfba85
|
@ -1 +1 @@
|
|||
Subproject commit 4b9e166025c85df5c427ed1e15bc3a1d98ff9252
|
||||
Subproject commit 363c21d30a9631d94adf5a4b508f2ddb8494ec27
|
|
@ -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<String> resourceTypes;
|
||||
private Set<String> 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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 <eitch@eitchnet.ch>
|
||||
|
@ -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$
|
||||
|
|
Loading…
Reference in New Issue