[New] Implemented locking for Strolch elements
Also cleaned up hierarchy of StrolchTransaction by moving up everything into AbstractTransaction as made sense and added special delegation methods for the actual commit() and rollback() ind the concrete implementations.
This commit is contained in:
parent
b9c7278911
commit
e11625998a
|
@ -20,7 +20,7 @@ import java.util.Properties;
|
|||
|
||||
import li.strolch.agent.api.ComponentContainer;
|
||||
import li.strolch.agent.api.StrolchComponent;
|
||||
import li.strolch.agent.impl.StrolchRealm;
|
||||
import li.strolch.agent.api.StrolchRealm;
|
||||
import li.strolch.model.Order;
|
||||
import li.strolch.model.Resource;
|
||||
import li.strolch.model.Tags;
|
||||
|
|
|
@ -17,13 +17,9 @@ package li.strolch.persistence.xml;
|
|||
|
||||
import java.util.Set;
|
||||
|
||||
import li.strolch.agent.impl.StrolchRealm;
|
||||
import li.strolch.model.StrolchElement;
|
||||
import li.strolch.agent.api.StrolchRealm;
|
||||
import li.strolch.persistence.api.AbstractTransaction;
|
||||
import li.strolch.persistence.api.PersistenceHandler;
|
||||
import li.strolch.persistence.api.StrolchPersistenceException;
|
||||
import li.strolch.persistence.api.TransactionCloseStrategy;
|
||||
import li.strolch.runtime.observer.ObserverHandler;
|
||||
import ch.eitchnet.xmlpers.api.ModificationResult;
|
||||
import ch.eitchnet.xmlpers.api.PersistenceTransaction;
|
||||
import ch.eitchnet.xmlpers.api.TransactionResult;
|
||||
|
@ -31,40 +27,12 @@ import ch.eitchnet.xmlpers.api.TransactionResult;
|
|||
public class XmlStrolchTransaction extends AbstractTransaction {
|
||||
|
||||
private XmlPersistenceHandler persistenceHandler;
|
||||
private ObserverHandler observerHandler;
|
||||
private boolean suppressUpdates;
|
||||
private PersistenceTransaction tx;
|
||||
private TransactionCloseStrategy closeStrategy;
|
||||
|
||||
public XmlStrolchTransaction(StrolchRealm realm, PersistenceTransaction tx, XmlPersistenceHandler persistenceHandler) {
|
||||
super(realm);
|
||||
this.persistenceHandler = persistenceHandler;
|
||||
this.suppressUpdates = false;
|
||||
this.tx = tx;
|
||||
this.closeStrategy = TransactionCloseStrategy.COMMIT;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param observerHandler
|
||||
* the observerHandler to set
|
||||
*/
|
||||
public void setObserverHandler(ObserverHandler observerHandler) {
|
||||
this.observerHandler = observerHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param suppressUpdates
|
||||
* the suppressUpdates to set
|
||||
*/
|
||||
public void setSuppressUpdates(boolean suppressUpdates) {
|
||||
this.suppressUpdates = suppressUpdates;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suppressUpdates
|
||||
*/
|
||||
public boolean isSuppressUpdates() {
|
||||
return this.suppressUpdates;
|
||||
}
|
||||
|
||||
PersistenceTransaction getTx() {
|
||||
|
@ -72,49 +40,24 @@ public class XmlStrolchTransaction extends AbstractTransaction {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setCloseStrategy(TransactionCloseStrategy closeStrategy) {
|
||||
this.closeStrategy = closeStrategy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void autoCloseableCommit() {
|
||||
|
||||
TransactionResult txResult = new TransactionResult();
|
||||
if (!this.suppressUpdates && this.observerHandler != null) {
|
||||
this.tx.setTransactionResult(txResult);
|
||||
}
|
||||
|
||||
protected void commit(li.strolch.persistence.api.TransactionResult txResult) throws Exception {
|
||||
TransactionResult result = new TransactionResult();
|
||||
this.tx.setTransactionResult(result);
|
||||
this.tx.autoCloseableCommit();
|
||||
logger.info(txResult.getLogMessage());
|
||||
|
||||
if (!this.suppressUpdates && this.observerHandler != null) {
|
||||
|
||||
Set<String> keys = txResult.getKeys();
|
||||
for (String key : keys) {
|
||||
ModificationResult modificationResult = txResult.getModificationResult(key);
|
||||
|
||||
this.observerHandler.add(key, modificationResult.<StrolchElement> getCreated());
|
||||
this.observerHandler.update(key, modificationResult.<StrolchElement> getUpdated());
|
||||
this.observerHandler.remove(key, modificationResult.<StrolchElement> getDeleted());
|
||||
}
|
||||
Set<String> keys = result.getKeys();
|
||||
for (String key : keys) {
|
||||
ModificationResult modificationResult = result.getModificationResult(key);
|
||||
li.strolch.persistence.api.ModificationResult mr = new li.strolch.persistence.api.ModificationResult(key,
|
||||
modificationResult.getCreated(), modificationResult.getUpdated(), modificationResult.getDeleted());
|
||||
txResult.addModificationResult(mr);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void autoCloseableRollback() {
|
||||
protected void rollback(li.strolch.persistence.api.TransactionResult txResult) throws Exception {
|
||||
this.tx.autoCloseableRollback();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws StrolchPersistenceException {
|
||||
this.closeStrategy.close(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen() {
|
||||
return this.tx.isOpen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PersistenceHandler getPersistenceHandler() {
|
||||
return this.persistenceHandler;
|
||||
|
|
|
@ -29,6 +29,7 @@ import li.strolch.model.Order;
|
|||
import li.strolch.model.Resource;
|
||||
import li.strolch.model.State;
|
||||
import li.strolch.model.StrolchElement;
|
||||
import li.strolch.model.Tags;
|
||||
import li.strolch.persistence.api.StrolchTransaction;
|
||||
import li.strolch.runtime.StrolchConstants;
|
||||
import li.strolch.runtime.observer.Observer;
|
||||
|
@ -106,8 +107,8 @@ public class ObserverUpdateTest {
|
|||
|
||||
// register an observer for orders and resources
|
||||
ElementAddedObserver observer = new ElementAddedObserver();
|
||||
runtimeMock.getContainer().getComponent(ObserverHandler.class).registerObserver("Order", observer); //$NON-NLS-1$
|
||||
runtimeMock.getContainer().getComponent(ObserverHandler.class).registerObserver("Resource", observer); //$NON-NLS-1$
|
||||
runtimeMock.getContainer().getComponent(ObserverHandler.class).registerObserver(Tags.ORDER, observer); //$NON-NLS-1$
|
||||
runtimeMock.getContainer().getComponent(ObserverHandler.class).registerObserver(Tags.RESOURCE, observer); //$NON-NLS-1$
|
||||
|
||||
// create order
|
||||
Order newOrder = createOrder("MyTestOrder", "Test Name", "TestType", new Date(), State.CREATED); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
|
||||
|
@ -122,8 +123,7 @@ public class ObserverUpdateTest {
|
|||
}
|
||||
|
||||
assertEquals(2, observer.results.size());
|
||||
assertEquals(1, observer.results.get("Order").getCreated().size()); //$NON-NLS-1$
|
||||
assertEquals(1, observer.results.get("Resource").getCreated().size()); //$NON-NLS-1$
|
||||
|
||||
assertEquals(1, observer.results.get(Tags.ORDER).getCreated().size()); //$NON-NLS-1$
|
||||
assertEquals(1, observer.results.get(Tags.RESOURCE).getCreated().size()); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue