/** * Store the value in a transaction- and thread-safe manner. It will only be persisted at the end * of the transaction but will be visible to the current transaction from this call onwards. * * @param value the value to store */ public void put(T value) { // the value is changing TransactionStorage storage = (TransactionStorage) AlfrescoTransactionSupport.getResource(txnKey); if (storage == null) { // it has not changed before storage = new TransactionStorage(); AlfrescoTransactionSupport.bindResource(txnKey, storage); // listen to the transaction AlfrescoTransactionSupport.bindListener(this); } storage.newValue = value; }
/** Promotes the storage value to the single value, if required */ public void afterCommit() { TransactionStorage storage = (TransactionStorage) AlfrescoTransactionSupport.getResource(txnKey); if (storage != null) { // the value was overridden setValue(storage.newValue); } }
/** @return Returns the transaction- and thread-safe wrapped instance */ @SuppressWarnings("unchecked") public T get() { // an in-transaction value overrides the singleton TransactionStorage storage = (TransactionStorage) AlfrescoTransactionSupport.getResource(txnKey); if (storage != null) { return (T) storage.newValue; } else { return (T) getValue(); } }