/*
*
* Copyright (c) 2000 Scott Oaks and Henry Wong. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and
* without fee is hereby granted.
*
* This sample source code is provided for example only,
* on an unsupported, as-is basis.
*
* AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. AUTHOR SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*
* THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
* CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
* PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
* NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
* SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
* SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
* PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). AUTHOR
* SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
* HIGH RISK ACTIVITIES.
*/
import java.util.*;
import com.sun.jini.thread.*;
import com.sun.jini.lease.landlord.*;
public class Expirer implements LeaseManager {
Landlord landlord;
WakeupManager manager;
HashMap tickets;
// Instances of this class are scheduled to run at a specific
// time by the wakeup manager -- they will run when the lease
// has expired, and they will cancel the lease. A lease renewal
// will remove the task from the wakeup managers queue
class ExpirerTask implements Runnable {
LeasedResource resource;
ExpirerTask(LeasedResource r) {
resource = r;
}
public void run() {
long now = System.currentTimeMillis();
long expir = resource.getExpiration();
if (expir > now) {
// It was renewed, but we didn't cancel the task in time.
WakeupManager.Ticket ticket = manager.schedule(expir, this);
tickets.put(resource, ticket);
}
else try {
landlord.cancel(resource.getCookie());
} catch (Exception e) {
}
}
}
public Expirer(Landlord l) {
landlord = l;
tickets = new HashMap(13);
manager = new WakeupManager();
}
// The lease has been renewed. A side-effect of registering
// the new duration will be to cancel the old task from the
// taskmanager queue.
public void renewed(LeasedResource resource,
long duration, long oldExpiration) {
register(resource, duration);
}
// A lease has been created or renewed. Cancel its old
// expiration task and create a new expiration task to
// cancel the lease after it expires.
public void register(LeasedResource resource, long duration) {
WakeupManager.Ticket ticket;
ticket = (WakeupManager.Ticket) tickets.remove(resource);
if (ticket != null)
manager.cancel(ticket);
ExpirerTask task = new ExpirerTask(resource);
ticket = manager.schedule(resource.getExpiration(), task);
tickets.put(resource, ticket);
}
}
|