Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/fetish/directory/tool/FetishLeaseImpl.java


1   package com.fetish.directory.tool;
2   
3   import java.io.Serializable;
4   import java.util.Hashtable;
5   import java.rmi.Remote;
6   import java.rmi.RemoteException;
7   import java.rmi.server.UnicastRemoteObject;
8   import net.jini.core.lease.*;
9   import net.jini.core.lookup.ServiceID;
10  import com.fetish.directory.Directory;
11  
12  /**
13   * Implementation of the FetishLease interface
14   */
15  
16  public class FetishLeaseImpl implements FetishLease {
17  
18    private Lease lease;
19    private Directory fada;
20    private Hashtable registry;
21    private ServiceID sid;
22    private Object expirationLock = new Object();
23    private Thread t;
24    private long expiration;
25  
26    public FetishLeaseImpl() throws RemoteException {
27    }
28  
29    public FetishLeaseImpl( Lease lease, Hashtable registry, ServiceID sid ) {
30      this.lease = lease;
31      this.expiration = this.lease.getExpiration();
32      this.registry = registry;
33      this.sid = sid;
34      this.t = new Thread( new ExpirationWatcher() );
35      this.t.start();
36    }
37  
38    class ExpirationWatcher implements Runnable {
39      public ExpirationWatcher() {
40      }
41  
42      public void run() {
43        long sleepTime = expiration - System.currentTimeMillis();
44        try {
45          Thread.sleep( sleepTime );
46          registry.remove( ( Object )sid );
47          try{
48            lease.cancel();
49          } catch( Exception e ) {
50          } finally {
51          }
52        } catch( InterruptedException e ) {
53          // Nothing to be done
54        } catch( Exception e ) {
55          e.printStackTrace();
56        }
57      }
58    }
59  
60    public void cancel() throws UnknownLeaseException,
61                  RemoteException {
62      if( this.lease == null )
63        throw new UnknownLeaseException( "The lease no longer exists" );
64      registry.remove( sid );
65      this.lease.cancel();
66      this.lease = null;
67      this.t.interrupt();
68    }
69  
70    public long renew( long duration ) throws LeaseDeniedException,
71                          UnknownLeaseException,
72                          RemoteException {
73      long now = System.currentTimeMillis();
74      if( this.lease == null )
75        throw new UnknownLeaseException( "The lease no longer exists" );
76      long grantedDuration = 0;
77      try {
78        this.lease.renew( duration );
79        grantedDuration = this.lease.getExpiration() - now;
80      } catch( UnknownLeaseException e ) {
81        throw e;
82      } catch( LeaseDeniedException e ) {
83        throw e;
84      }
85      this.expiration = now + grantedDuration;
86      if( t != null )
87        t.interrupt();
88      this.t = new Thread( new ExpirationWatcher() );
89      this.t.start();
90      return grantedDuration;
91    }
92  
93  }