1 6 package org.prevayler.implementation.publishing.censorship; 7 8 import org.prevayler.Transaction; 9 import org.prevayler.implementation.snapshot.*; 10 11 import java.io.ByteArrayInputStream ; 12 import java.io.ByteArrayOutputStream ; 13 import java.util.Date ; 14 15 public class StrictTransactionCensor implements TransactionCensor { 16 17 private final Object _king; 18 private Object _royalFoodTaster; 19 private final SnapshotManager _snapshotManager; 20 21 22 public StrictTransactionCensor(SnapshotManager snapshotManager) { 23 _snapshotManager = snapshotManager; 24 _king = _snapshotManager.recoveredPrevalentSystem(); 25 } 27 28 public void approve(Transaction transaction, Date executionTime) throws RuntimeException , Error { 29 try { 30 Transaction transactionCopy = (Transaction)_snapshotManager.deepCopy(transaction, "Unable to produce a copy of the transaction for trying out before applying it to the real system."); 31 transactionCopy.executeOn(royalFoodTaster(), executionTime); 32 } catch (RuntimeException rx) { 33 letTheFoodTasterDie(); 34 throw rx; 35 } catch (Error error) { 36 letTheFoodTasterDie(); 37 throw error; 38 } 39 } 40 41 private void letTheFoodTasterDie() { 42 _royalFoodTaster = null; } 44 45 private Object royalFoodTaster() { 46 if (_royalFoodTaster == null) produceNewFoodTaster(); 47 return _royalFoodTaster; 48 } 49 50 private void produceNewFoodTaster() { 51 try { 52 ByteArrayOutputStream out = new ByteArrayOutputStream (); 53 synchronized (_king) { _snapshotManager.writeSnapshot(_king, out); } 54 _royalFoodTaster = _snapshotManager.readSnapshot(new ByteArrayInputStream (out.toByteArray())); 55 } catch (Exception ex) { 56 ex.printStackTrace(); 57 throw new RuntimeException ("Unable to produce a copy of the prevalent system for trying out transactions before applying them to the real system."); 58 } 59 } 60 61 } 62 63 64 | Popular Tags |