Source code: com/fetish/directory/tool/FetishLeaseProxy.java
1 package com.fetish.directory.tool;
2
3 import java.rmi.Remote;
4 import java.rmi.server.RemoteObject;
5 import java.rmi.server.RemoteStub;
6 import java.rmi.RemoteException;
7 import net.jini.core.lease.*;
8 import net.jini.core.lookup.ServiceID;
9 import com.fetish.directory.Directory;
10 import com.fetish.toolkit.LeaseMapImpl;
11
12 /**
13 * Local proxy for the remote Fetish leases.
14 */
15
16 public class FetishLeaseProxy implements Lease, java.io.Serializable {
17
18 FetishLease lease;
19 private long expiration;
20 private int serialFormat;
21 // Initial roundtrip time is unknown. Assume short lease duration.
22 private long roundtrip = 0;
23
24 public FetishLeaseProxy() {
25 }
26
27 public FetishLeaseProxy( FetishLease lease,
28 long duration,
29 int serialFormat )
30 throws RemoteException {
31
32 long now = System.currentTimeMillis();
33 this.lease = lease;
34 this.expiration = now + duration - this.roundtrip;
35 if( this.expiration < 0 )
36 this.expiration = 0;
37 this.serialFormat = serialFormat;
38 }
39
40 public long getExpiration() {
41
42 return this.expiration;
43
44 }
45
46 /**
47 * Cancel this lease and remove the associated service registration.
48 */
49 public void cancel() throws UnknownLeaseException,
50 RemoteException {
51 (( FetishLease )this.lease).cancel();
52 }
53
54 /**
55 * Attempt to renew the lease for the specified duration.
56 * If duration is too high the underlying Jini Lookup Service will
57 * deny it, and the lease will remain untouched.
58 * @param duration The desired lease time in milliseconds.
59 * @throws LeaseDeniedException The amount of time asked is too high
60 * @throws UnknownLeaseException The lease does no longer exist, it
61 * either has expired or has already been cancelled.
62 * @throws RemoteException There was a communication error with FADA
63 */
64 public void renew( long duration ) throws LeaseDeniedException,
65 UnknownLeaseException,
66 RemoteException {
67 long before = System.currentTimeMillis();
68 long grantedDuration = this.lease.renew( duration );
69 long after = System.currentTimeMillis();
70 this.roundtrip = after - before;
71 this.expiration = grantedDuration + after
72 - this.roundtrip ; // quite naive
73 if( this.expiration < 0 ) {
74 this.expiration = 0;
75 }
76 }
77
78 public void setSerialFormat( int format ) {
79 this.serialFormat = format;
80 }
81
82 public int getSerialFormat() {
83 return this.serialFormat;
84 }
85
86 public LeaseMap createLeaseMap( long duration ) {
87 LeaseMap map = ( LeaseMap )new LeaseMapImpl();
88 map.put( this, new Long( duration ) );
89 return map;
90 }
91
92 /**
93 * Checks wether this lease can be batched with another.
94 * <br>Fetish leases cannot be batched, this method returns always false.
95 */
96 public boolean canBatch( Lease lease ) {
97 return false;
98 }
99
100 }