1 5 package org.prevayler.implementation.logging; 6 7 import java.io.IOException ; 8 import java.util.ArrayList ; 9 import java.util.Date ; 10 import java.util.List ; 11 12 import org.prevayler.Transaction; 13 import org.prevayler.foundation.Turn; 14 import org.prevayler.implementation.*; 15 import org.prevayler.implementation.publishing.TransactionSubscriber; 16 17 18 public class TransientLogger implements TransactionLogger { 19 20 private final List log = new ArrayList (); 21 private long _initialTransaction; 22 private boolean _initialTransactionInitialized = false; 23 24 25 public void log(Transaction transaction, Date executionTime, Turn myTurn) { 26 if (!_initialTransactionInitialized) throw new IllegalStateException ("TransactionLogger.update() has to be called at least once before TransactionLogger.log()."); 27 28 try { 29 myTurn.start(); 30 log.add(new TransactionTimestamp(transaction, executionTime)); 31 } finally { 32 myTurn.end(); 33 } 34 } 35 36 public synchronized void update(TransactionSubscriber subscriber, long initialTransaction) throws IOException { 37 if (!_initialTransactionInitialized) { 38 _initialTransactionInitialized = true; 39 _initialTransaction = initialTransaction; 40 return; 41 } 42 if (initialTransaction < _initialTransaction) throw new IOException ("Unable to recover transaction " + initialTransaction + ". The oldest recoverable transaction is " + _initialTransaction + "."); 43 44 int i = (int)(initialTransaction - _initialTransaction); 45 if (i > log.size()) throw new IOException ("The transaction log has not yet reached transaction " + initialTransaction + ". The last logged transaction was " + (_initialTransaction + log.size() - 1) + "."); 46 47 while (i != log.size()) { 48 TransactionTimestamp entry = (TransactionTimestamp)log.get(i); 49 subscriber.receive(entry.transaction(), entry.timestamp()); 50 i++; 51 } 52 } 53 54 public void close() {} 55 56 } 57 | Popular Tags |