import com.sun.jini.lease.landlord.Landlord; import com.sun.jini.lease.landlord.LandlordLease; import net.jini.core.lease.Lease; import net.jini.core.lease.LeaseDeniedException; import net.jini.core.lease.UnknownLeaseException; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.util.HashMap; import java.util.List; import java.util.Map; /** * The landlord which implements the Jini Landlord protocol, handles leases, etc. * * @author Cormac Redmond -- credmond85 /at/ gmail */ public class RegistrationLandlord extends UnicastRemoteObject implements Landlord { // A simple leasing policy...10 minute leases. protected static final int DEFAULT_MAX_LEASE = 1000 * 60 * 10; protected int maxLease = DEFAULT_MAX_LEASE; // Assume that registrations are kept in // a list maintained by our clients. protected List usersRegdForEvents; // A factory for making landlord leases. protected LandlordLease.Factory factory; public RegistrationLandlord(List regs, LandlordLease.Factory factory) throws RemoteException { this.usersRegdForEvents = regs; this.factory = factory; } // Change the maximum lease time from the default. public void setMaxLease(int maxLease) { this.maxLease = maxLease; } // Apply the policy to a requested duration // to get an actual expiration time. public long getExpiration(long request) { if (request > maxLease || request == Lease.ANY) return System.currentTimeMillis() + maxLease; else return System.currentTimeMillis() + request; } // Cancel all leases represented by 'cookie' public void cancel(Object cookie) throws UnknownLeaseException, RemoteException { int size = usersRegdForEvents.size(); int[] toRemoveIndexes = new int[size]; System.out.println("Cancelling a lease..."); for (int i = 0; i < size; i++) { //Get Registration details Registration reg = (Registration) usersRegdForEvents.get(i); //If concerned with this cookie if (reg.cookie.equals(cookie)) { //reg.cancelled();//Does nothing currently //Record what we want removed.. toRemoveIndexes[i] = i; } else toRemoveIndexes[i] = -1; //Leave it where it is } int offset = 0; //Remove the cancelled leases from registration list for (int i = 0; i < toRemoveIndexes.length; i++) { if (toRemoveIndexes[i] != -1) { usersRegdForEvents.remove(toRemoveIndexes[i] + offset); offset--; } } return; //throw new UnknownLeaseException(cookie.toString()); } // Cancel a set of leases public Map cancelAll(Object[] cookies) throws RemoteException { Map exceptionMap = null; for (int i = 0; i < cookies.length; i++) { try { cancel(cookies[i]); } catch (UnknownLeaseException ex) { if (exceptionMap == null) { exceptionMap = new HashMap(); } exceptionMap.put(cookies[i], ex); } } return exceptionMap; } // Renew the lease specified by 'cookie' public long renew(Object cookie, long extension) throws UnknownLeaseException, LeaseDeniedException, RemoteException { System.out.println("Renewing a lease..."); for (int i = 0, size = usersRegdForEvents.size(); i < size; i++) { System.out.println("i: " + i); Registration reg = (Registration) usersRegdForEvents.get(i); if (reg.getCookie().equals(cookie)) { long expiration = getExpiration(extension); reg.setExpiration(expiration); return expiration - System.currentTimeMillis(); } } throw new UnknownLeaseException(cookie.toString()); } // Renew a set of leases. public Landlord.RenewResults renewAll(Object[] cookies, long[] extensions) throws RemoteException { long[] granted = new long[cookies.length]; Exception[] denied = null; System.out.println("Renewing all leases..."); for (int i = 0; i < cookies.length; i++) { try { granted[i] = renew(cookies[i], extensions[i]); } catch (Exception ex) { if (denied == null) { denied = new Exception[cookies.length + 1]; } denied[i + 1] = ex; } } Landlord.RenewResults results = new Landlord.RenewResults(granted, denied); return results; } }