comparison src/org/dancres/blitz/notify/NotifyLeaseHandlerImpl.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.blitz.notify;
2
3 import java.io.IOException;
4
5 import java.util.logging.Level;
6
7 import net.jini.core.lease.UnknownLeaseException;
8 import net.jini.core.lease.LeaseDeniedException;
9
10 import net.jini.core.transaction.TransactionException;
11
12 import org.dancres.blitz.disk.DiskTxn;
13
14 import org.dancres.blitz.lease.SpaceUID;
15 import org.dancres.blitz.lease.LeaseHandler;
16 import org.dancres.blitz.lease.LeaseBounds;
17
18 import org.dancres.blitz.txn.TxnOp;
19 import org.dancres.blitz.txn.TxnState;
20 import org.dancres.blitz.txn.TxnManager;
21
22 import org.dancres.blitz.task.Task;
23
24 import org.dancres.blitz.oid.OID;
25
26 import org.dancres.blitz.util.Time;
27
28 public class NotifyLeaseHandlerImpl implements LeaseHandler {
29 public boolean recognizes(SpaceUID aUID) {
30 return (aUID instanceof SpaceNotifyUID);
31 }
32
33 public long renew(SpaceUID aUID, long aLeaseDuration)
34 throws UnknownLeaseException, LeaseDeniedException, IOException {
35
36 long myDuration = LeaseBounds.boundNotify(aLeaseDuration);
37 long myExpiry = Time.getAbsoluteTime(myDuration);
38
39 boolean myResult;
40
41 OID myOID = ((SpaceNotifyUID) aUID).getOID();
42
43 DiskTxn myTxn = DiskTxn.newTxn();
44
45 try {
46 myResult = EventQueue.get().renew(myOID, myExpiry);
47 } finally {
48 myTxn.commit();
49 }
50
51 if (!myResult)
52 throw new UnknownLeaseException();
53
54 log(new LeaseRenewal(myOID, myExpiry));
55
56 return myDuration;
57 }
58
59 public void cancel(SpaceUID aUID)
60 throws UnknownLeaseException, IOException {
61
62 boolean myResult;
63
64 OID myOID = ((SpaceNotifyUID) aUID).getOID();
65
66 DiskTxn myTxn = DiskTxn.newTxn();
67
68 try {
69 myResult = EventQueue.get().cancel(myOID);
70 } finally {
71 myTxn.commit();
72 }
73
74 if (!myResult)
75 throw new UnknownLeaseException();
76
77 log(new LeaseCancel(myOID));
78 }
79
80 private void log(TxnOp anAction) throws IOException {
81 try {
82 TxnManager.get().log(anAction);
83 } catch (TransactionException aTE) {
84 throw new IOException("Failed to log action");
85 }
86 }
87
88 private static final class LeaseRenewal implements TxnOp {
89
90 private OID theOID;
91 private long theExpiry;
92
93 LeaseRenewal(OID aOID, long anExpiry) {
94 theOID = aOID;
95 theExpiry = anExpiry;
96 }
97
98 public void restore(TxnState aState) throws IOException {
99 EventQueue myQueue = EventQueue.get();
100
101 DiskTxn myTxn = DiskTxn.newTxn();
102
103 try {
104 myQueue.renew(theOID, theExpiry);
105 } catch (IOException anIOE) {
106 } finally {
107 myTxn.commit();
108 }
109 }
110
111 public void commit(TxnState aState) throws IOException {
112 // Nothing to do - already applied
113 }
114
115 public void abort(TxnState aState) throws IOException {
116 // Nothing to do - already applied
117 }
118
119 public String toString() {
120 return " NR : " + theOID + " : " + theExpiry;
121 }
122 }
123
124 private static final class LeaseCancel implements TxnOp {
125 private OID theOID;
126
127 LeaseCancel(OID aOID) {
128 theOID = aOID;
129 }
130
131 public void restore(TxnState aState) throws IOException {
132 EventQueue myQueue = EventQueue.get();
133
134 DiskTxn myTxn = DiskTxn.newTxn();
135
136 try {
137 myQueue.cancel(theOID);
138 } catch (IOException anIOE) {
139 } finally {
140 myTxn.commit();
141 }
142 }
143
144 public void commit(TxnState aState) throws IOException {
145 // Nothing to do - already applied
146 }
147
148 public void abort(TxnState aState) throws IOException {
149 // Nothing to do - already applied
150 }
151
152 public String toString() {
153 return " NC : " + theOID;
154 }
155 }
156
157 private static final class LogTask implements Task {
158 private TxnOp theAction;
159
160 LogTask(TxnOp anAction) {
161 theAction = anAction;
162 }
163
164 public void run() {
165 try {
166 TxnManager.get().log(theAction);
167 } catch (Exception anException) {
168 EventQueue.theLogger.log(Level.SEVERE,
169 "Failed to log lease action",
170 anException);
171 }
172 }
173 }
174 }