com.sun.jini.norm
Class ClientLeaseWrapper

java.lang.Object
  extended by com.sun.jini.norm.ClientLeaseWrapper
All Implemented Interfaces:
Serializable, Lease

 class ClientLeaseWrapper
extends Object
implements Lease, Serializable

Class that wraps client Leases. Provides hooks for synchronization and data associated with each client lease while allowing us to use LeaseRenewalManager.

This code assumes most synchronization is being done at the set level, this works because no lease is in more than one set at a given time. The only place where additional synchronization is going on is in renewals and the logging of renewals.

Author:
Sun Microsystems, Inc.

Nested Class Summary
private  class ClientLeaseWrapper.FailureFactory
          Nested top-level implementation of EventFactory that generates RenewalFailureEvent events
 
Field Summary
private  Lease clientLease
          Transient copy of unpacked lease, null if we could not unpack or prepare the lease.
private  long clientLeaseExpiration
          Most current expiration time of client Lease that we know of
private  Throwable lastFailure
          Throwable thrown by the last renew attempt on the client lease.
private static Method[] leaseToLeaseMethods
           
private static Logger logger
          Logger for logging messages for this class
private  MarshalledInstance marshalledClientLease
          Client lease in marshalled form.
private  long membershipExpiration
          Membership expiration of this lease
private  ProxyPreparer recoveredLeasePreparer
          The proxy preparer to use to prepare a newly unmarshalled client lease, or null if this instance was created using an already prepared client lease, which is how instances are created initially.
private  boolean renewalPending
          Flag used to check if lease has been persisted since last renewal, true means it has not been
private  long renewDuration
          renewDuration of this lease
private  List renewedList
          Reference to list of leases that have been renewed, but not persisted
private static long serialVersionUID
           
private  LeaseSet set
          The LeaseSet we belong to
private  long UID
          Sequence number that uniquely identifies this wrapper
 
Fields inherited from interface net.jini.core.lease.Lease
ABSOLUTE, ANY, DURATION, FOREVER
 
Constructor Summary
ClientLeaseWrapper(Lease clientLease, long UID, List renewedList, LeaseSet leaseSet, long membershipDuration, long renewDuration, long now)
          Simple constructor
 
Method Summary
private  void calcMembershipExpiration(long membershipDuration, long now)
          Given the current time and a membershipDuration set membershipExpiration the correct value.
 boolean canBatch(Lease lease)
          Another lease can be batched with this one if it is a ClientLeaseMapWrapper, if it is either a member of the same lease renewal set or sets are not isolated, if neither lease is deformed, if the leases either both have the same client constraints or do not implement RemoteMethodControl, and if the underlying client leases can be batched.
 void cancel()
          Always throws UnsupportedOperationException since a lease renewal service should never cancel a client lease.
private  void changed()
          Called when a renewal thread changes the persistent state of the this wrapper.
(package private)  void clearRenewed()
          Atomically clear the renewalPending flag.
 LeaseMap createLeaseMap(long duration)
          Creates a Map object that can contain leases whose renewal or cancellation can be batched, and adds the current lease to that map.
(package private)  boolean ensureCurrent(long now)
          Called by ClientLeaseMapWrapper to see if the set associated with this wrapper is still valid.
 boolean equals(Object that)
           
(package private)  void failedRenewal(Throwable t)
          Sets lastFailure and adds the this to the renewed list so it so the change can be logged.
(package private)  Lease getClientLease()
          Get a reference to the client lease, unmarshalling and preparing it if necessary.
 long getExpiration()
          Returns a long that indicates the time that the lease will expire.
(package private)  Throwable getLastFailure()
          Return the exception (if any) that occured at the last renewal attempt
(package private)  LeaseSet getLeaseSet()
          The LeaseSet this lease is in
(package private)  MarshalledInstance getMarshalledClientLease()
          Return the client lease in marshalled form.
(package private)  long getMembershipExpiration()
          Return the membershipExpiration of this lease
(package private)  long getRenewDuration()
          Return the renewDuration of this lease
 int getSerialFormat()
          Always throws UnsupportedOperationException.
 int hashCode()
           
(package private)  boolean isDeformed()
          Return true if the underlying client lease has not yet been deserialized
(package private)  EventFactory newFailureFactory(LeaseRenewalSet source)
          Return an EventFactory that will create an appropriate RenewalFailureEvent for the client lease associated with this wrapper.
(package private)  void recoverTransient(List renewedList, UIDGenerator generator, LeaseSet leaseSet, ProxyPreparer recoveredLeasePreparer)
          After recovering a lease wrapper call this method before using any other of the wrappers methods and before allocating any new wrappers can not recover itself.
 void renew(long duration)
          Used to renew a lease for an additional period of time, specified in milliseconds.
private static boolean sameConstraints(Lease l1, Lease l2)
          Returns true if the two leases both implement RemoteMethodControl and have the same constraints for Lease methods, or both don't implement RemoteMethodControl, else returns false.
 void setSerialFormat(int format)
          Always throws UnsupportedOperationException.
(package private)  void successfulRenewal()
          Log a successful lease renewalNote this is only called from from renew methods that are renewing the client lease associated with this wrapper so additional synchronization should not be necessary.
private  boolean testAndSetRenewalPending()
          Atomically test and set the renewalPending flag.
 String toString()
           
(package private)  void update(long membershipDuration, long renewDuration, long now)
          Update the membership expiration and renewDuration of this lease
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

logger

private static final Logger logger
Logger for logging messages for this class


leaseToLeaseMethods

private static final Method[] leaseToLeaseMethods

lastFailure

private Throwable lastFailure
Throwable thrown by the last renew attempt on the client lease. null if the renew has not been called yet or the last renewal call succeeded.


marshalledClientLease

private MarshalledInstance marshalledClientLease
Client lease in marshalled form.


clientLeaseExpiration

private long clientLeaseExpiration
Most current expiration time of client Lease that we know of


UID

private long UID
Sequence number that uniquely identifies this wrapper


membershipExpiration

private long membershipExpiration
Membership expiration of this lease


renewDuration

private long renewDuration
renewDuration of this lease


set

private transient LeaseSet set
The LeaseSet we belong to


clientLease

private transient Lease clientLease
Transient copy of unpacked lease, null if we could not unpack or prepare the lease.


recoveredLeasePreparer

private transient ProxyPreparer recoveredLeasePreparer
The proxy preparer to use to prepare a newly unmarshalled client lease, or null if this instance was created using an already prepared client lease, which is how instances are created initially.


renewalPending

private transient boolean renewalPending
Flag used to check if lease has been persisted since last renewal, true means it has not been


renewedList

private transient List renewedList
Reference to list of leases that have been renewed, but not persisted

Constructor Detail

ClientLeaseWrapper

ClientLeaseWrapper(Lease clientLease,
                   long UID,
                   List renewedList,
                   LeaseSet leaseSet,
                   long membershipDuration,
                   long renewDuration,
                   long now)
             throws IOException
Simple constructor

Parameters:
clientLease - lease from client that is to be renewed
UID - ID number for this wrapper unique for all wrappers in a given server
renewedList - list that wrapper should go on after renewing their client lease is renewed
leaseSet - the LeaseSet this lease is in
membershipDuration - initial membership duration for this lease
renewDuration - initial membership expiration for the lease
now - the current time
Throws:
IOException
Method Detail

calcMembershipExpiration

private void calcMembershipExpiration(long membershipDuration,
                                      long now)
Given the current time and a membershipDuration set membershipExpiration the correct value.


update

void update(long membershipDuration,
            long renewDuration,
            long now)
Update the membership expiration and renewDuration of this lease


getMembershipExpiration

long getMembershipExpiration()
Return the membershipExpiration of this lease


getRenewDuration

long getRenewDuration()
Return the renewDuration of this lease


clearRenewed

void clearRenewed()
Atomically clear the renewalPending flag.


getClientLease

Lease getClientLease()
Get a reference to the client lease, unmarshalling and preparing it if necessary. If it can't be unpacked or prepared, return null.


getMarshalledClientLease

MarshalledInstance getMarshalledClientLease()
Return the client lease in marshalled form. If possible marshal using the Lease.DURATION serialization format.

Assumes that no one else will be serializing the lease during this call.


equals

public boolean equals(Object that)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

getExpiration

public long getExpiration()
Description copied from interface: Lease
Returns a long that indicates the time that the lease will expire. This time is represented as milliseconds from the beginning of the epoch, relative to the local clock.

Specified by:
getExpiration in interface Lease
Returns:
a long that indicates the time that the lease will expire

cancel

public void cancel()
Always throws UnsupportedOperationException since a lease renewal service should never cancel a client lease.

Specified by:
cancel in interface Lease

getLastFailure

Throwable getLastFailure()
Return the exception (if any) that occured at the last renewal attempt


testAndSetRenewalPending

private boolean testAndSetRenewalPending()
Atomically test and set the renewalPending flag.

Returns:
the state of the renewalPending flag before it was set

changed

private void changed()
Called when a renewal thread changes the persistent state of the this wrapper. If necessary places this object on the queue of wrappers to be persisted.


ensureCurrent

boolean ensureCurrent(long now)
Called by ClientLeaseMapWrapper to see if the set associated with this wrapper is still valid. Returns true if it still is


failedRenewal

void failedRenewal(Throwable t)
Sets lastFailure and adds the this to the renewed list so it so the change can be logged. Note this is only called from from renew methods that are renewing the client lease associated with this wrapper so additional synchronization should not be necessary.


successfulRenewal

void successfulRenewal()
Log a successful lease renewalNote this is only called from from renew methods that are renewing the client lease associated with this wrapper so additional synchronization should not be necessary.


renew

public void renew(long duration)
           throws LeaseDeniedException,
                  UnknownLeaseException,
                  RemoteException
Description copied from interface: Lease
Used to renew a lease for an additional period of time, specified in milliseconds. This duration is not added to the original lease, but is used to determine a new expiration time for the existing lease. If the renewal is granted this is reflected in value returned by getExpiration. If the renewal fails, the lease is left intact for the same duration that was in force prior to the call to renew.

Specified by:
renew in interface Lease
Parameters:
duration - the requested duration in milliseconds
Throws:
LeaseDeniedException - the lease grantor is unable or unwilling to renew the lease
UnknownLeaseException - the lease being renewed is unknown to the lease grantor
RemoteException

setSerialFormat

public void setSerialFormat(int format)
Always throws UnsupportedOperationException. The LeaseRenewalManager should never call this method and norm always serializes the wrapper with absolute times.

Specified by:
setSerialFormat in interface Lease
Parameters:
format - DURATION or ABSOLUTE
See Also:
Lease.getSerialFormat()

getSerialFormat

public int getSerialFormat()
Always throws UnsupportedOperationException. The LeaseRenewalManager should never call this method and norm always serializes the wrapper with absolute times.

Specified by:
getSerialFormat in interface Lease
Returns:
an int representing the serial format value
See Also:
Lease.setSerialFormat(int)

createLeaseMap

public LeaseMap createLeaseMap(long duration)
Description copied from interface: Lease
Creates a Map object that can contain leases whose renewal or cancellation can be batched, and adds the current lease to that map. The current lease is put in the map with the duration value given by the parameter.

Specified by:
createLeaseMap in interface Lease
Parameters:
duration - the duration to put into a Long and use as the value for the current lease in the created LeaseMap
Returns:
the created LeaseMap object

canBatch

public boolean canBatch(Lease lease)
Another lease can be batched with this one if it is a ClientLeaseMapWrapper, if it is either a member of the same lease renewal set or sets are not isolated, if neither lease is deformed, if the leases either both have the same client constraints or do not implement RemoteMethodControl, and if the underlying client leases can be batched.

Specified by:
canBatch in interface Lease
Parameters:
lease - the Lease to be evaluated
Returns:
a boolean indicating whether or not the lease given as a parameter can be batched (placed in the same LeaseMap) with the current lease

sameConstraints

private static boolean sameConstraints(Lease l1,
                                       Lease l2)
Returns true if the two leases both implement RemoteMethodControl and have the same constraints for Lease methods, or both don't implement RemoteMethodControl, else returns false.


recoverTransient

void recoverTransient(List renewedList,
                      UIDGenerator generator,
                      LeaseSet leaseSet,
                      ProxyPreparer recoveredLeasePreparer)
After recovering a lease wrapper call this method before using any other of the wrappers methods and before allocating any new wrappers can not recover itself.

Parameters:
renewedList - List that wrapper should go on after renewing their client lease is renewed
generator - ID generator being used to generate IDs client lease wrappers
leaseSet - the set this wrapper is associated with
recoveredLeasePreparer - the proxy preparer to use to prepare client leases recovered from persistent storage

isDeformed

boolean isDeformed()
Return true if the underlying client lease has not yet been deserialized


getLeaseSet

LeaseSet getLeaseSet()
The LeaseSet this lease is in


newFailureFactory

EventFactory newFailureFactory(LeaseRenewalSet source)
                         throws IOException
Return an EventFactory that will create an appropriate RenewalFailureEvent for the client lease associated with this wrapper. This method assumes that no one else will be setting the serialization format of the lease during the course of the call.

Parameters:
source - Source object for the event
Throws:
IOException - if the client lease could not be pre marshalled.


Copyright 2007-2010, multiple authors.
Licensed under the Apache License, Version 2.0, see the NOTICE file for attributions.