KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > prevayler > implementation > logging > TransientLogger


1 //Prevayler(TM) - The Free-Software Prevalence Layer.
2
//Copyright (C) 2001-2003 Klaus Wuestefeld
3
//This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
4

5 package org.prevayler.implementation.logging;
6
7 import java.io.IOException JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Date JavaDoc;
10 import java.util.List JavaDoc;
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 JavaDoc log = new ArrayList JavaDoc();
21     private long _initialTransaction;
22     private boolean _initialTransactionInitialized = false;
23
24
25     public void log(Transaction transaction, Date JavaDoc executionTime, Turn myTurn) {
26         if (!_initialTransactionInitialized) throw new IllegalStateException JavaDoc("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 JavaDoc {
37         if (!_initialTransactionInitialized) {
38             _initialTransactionInitialized = true;
39             _initialTransaction = initialTransaction;
40             return;
41         }
42         if (initialTransaction < _initialTransaction) throw new IOException JavaDoc("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 JavaDoc("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