comparison src/org/dancres/blitz/remote/LeaseMapImpl.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.remote;
2
3 import java.rmi.RemoteException;
4
5 import java.util.Iterator;
6 import java.util.HashMap;
7
8 import net.jini.core.lease.LeaseMapException;
9 import net.jini.core.lease.Lease;
10
11 import com.sun.jini.lease.AbstractLeaseMap;
12
13 import org.dancres.blitz.lease.SpaceUID;
14
15 /**
16 @todo Check constraints are compatible
17 */
18 class LeaseMapImpl extends AbstractLeaseMap {
19 private Landlord theLandlord;
20
21 LeaseMapImpl(Lease aLease, long aDuration, Landlord aLandlord) {
22 super(aLease, aDuration);
23 theLandlord = aLandlord;
24 }
25
26 public boolean canContainKey(Object aKey) {
27 if (aKey instanceof LeaseImpl) {
28 LeaseImpl myOther = (LeaseImpl) aKey;
29
30 return (myOther.theStub.equals(theLandlord));
31 }
32
33 return false;
34 }
35
36 public void cancelAll() throws LeaseMapException, RemoteException {
37 LeaseImpl[] myLeases = new LeaseImpl[size()];
38 SpaceUID[] myUIDs = new SpaceUID[myLeases.length];
39
40 Iterator mySet = keySet().iterator();
41
42 for (int i = 0; mySet.hasNext(); i++) {
43 LeaseImpl myLease = (LeaseImpl) mySet.next();
44
45 myLeases[i] = myLease;
46 myUIDs[i] = myLease.getUID();
47 }
48
49 LeaseResults myResults = theLandlord.cancel(myUIDs);
50
51 if (myResults == null)
52 return;
53
54 HashMap myFails = new HashMap();
55
56 for (int i = 0; i < myResults.theFailures.length; i++) {
57 if (myResults.theFailures[i] != null) {
58 myFails.put(myLeases[i], myResults.theFailures[i]);
59 }
60 }
61
62 throw new LeaseMapException("Some leases didn't cancel", myFails);
63 }
64
65 public void renewAll() throws LeaseMapException, RemoteException {
66 LeaseImpl[] myLeases = new LeaseImpl[size()];
67 SpaceUID[] myUIDs = new SpaceUID[myLeases.length];
68 long[] myDurations = new long[myLeases.length];
69
70 Iterator mySet = keySet().iterator();
71
72 for (int i = 0; mySet.hasNext(); i++) {
73 LeaseImpl myLease = (LeaseImpl) mySet.next();
74
75 myLeases[i] = myLease;
76 myUIDs[i] = myLease.getUID();
77 myDurations[i] = ((Long) get(myLease)).longValue();
78 }
79
80 LeaseResults myResults = theLandlord.renew(myUIDs, myDurations);
81
82 long myNow = System.currentTimeMillis();
83
84 HashMap myFailed = null;
85
86 for (int i = 0; i < myResults.theNewDurations.length; i++) {
87 long myDuration = myResults.theNewDurations[i];
88 if (myDuration == -1) {
89 if (myFailed == null)
90 myFailed = new HashMap();
91
92 myFailed.put(myLeases[i], myResults.theFailures[i]);
93
94 // Get this lease out of the map - it's "broken"
95 remove(myLeases[i]);
96 } else {
97 myDuration = myDuration + myNow;
98 if (myDuration < 0)
99 myDuration = Long.MAX_VALUE;
100
101 myLeases[i].setExpiration(myDuration);
102 }
103 }
104
105 if (myFailed != null)
106 throw new LeaseMapException("Renewing", myFailed);
107 }
108 }