FileDocCategorySizeDatePackage
ServerLandlord.javaAPI DocExample5546Thu Mar 16 11:52:26 GMT 2000None

ServerLandlord

public class ServerLandlord extends UnicastRemoteObject implements Landlord

Fields Summary
protected Hashtable
resources
protected long
MAXLEASETIME
protected LeasePolicy
policy
Constructors Summary
public ServerLandlord()


        
        policy = new LeaseDurationPolicy(MAXLEASETIME, MAXLEASETIME, this, null, null);
    
Methods Summary
public voidcancel(java.lang.Object cookie)

        synchronized (this) {
            ServerResource sr = (ServerResource)resources.get(cookie);
            if (sr == null)
                throw new UnknownLeaseException();

            resources.remove(cookie);
        }
    
public voidcancelAll(java.lang.Object[] cookie)

        Map map = null;
        for (int i = 0; i < cookie.length; i++) {
            try {
                cancel(cookie[i]);
            } catch (LeaseException ex) {
                if (map == null)
                    map = new HashMap();
                map.put(cookie[i], ex);
            }
        }
	if (map != null)
	    throw new Exception("Can't cancel all leases", map);
    
public voidexpireLeases()

        synchronized (this) {
            Vector deleteList = new Vector();
            for (Enumeration e = resources.elements(); e.hasMoreElements();) {
                ServerResource sr = (ServerResource) e.nextElement();
                if (!policy.ensureCurrent(sr)) {
                    deleteList.addElement(sr.getCookie());
                }
            }
            for (Enumeration e = deleteList.elements(); e.hasMoreElements();) {
                Object cookie = e.nextElement();
                resources.remove(cookie);
            }
        }
    
public java.lang.ObjectgetSessionData(Lease lease)

        Object sessiondata = null;
        expireLeases();                // Make sure this lease hasn't expired
        synchronized (this) {
            for (Enumeration e = resources.elements();e.hasMoreElements();) {
                ServerResource sr = (ServerResource) e.nextElement();
                if (lease.equals(sr.lease)) {
                    sessiondata = sr.sessionData;
                    break;
                }
            }
        }
        return sessiondata;
    
public LeasenewLease(java.lang.Object sessionData, long duration)

        ServerResource sr = new ServerResource(sessionData);

        expireLeases();                // Make room for new Leases

        try {
            sr.lease = policy.leaseFor(sr, duration);
            synchronized(this) {
                resources.put(sr.getCookie(), sr);
            }
            return sr.lease;
        } catch (LeaseDeniedException e) {};
        return null;
    
public longrenew(java.lang.Object cookie, long duration)

        synchronized (this) {
            ServerResource sr = (ServerResource)resources.get(cookie);
            if (sr == null)
                throw new UnknownLeaseException();

            return policy.renew(sr, duration);
        }
    
public Landlord.RenewResultsrenewAll(java.lang.Object[] cookie, long[] duration)

        long[] granted = new long[cookie.length];
        Vector denied = new Vector();

        for (int i = 0; i < cookie.length; i++) {
            try {
                granted[i] = renew(cookie[i], duration[i]);
            } catch (LeaseException lex) {
                granted[i] = -1;
                denied.add(lex);
            }
        }
        return new Landlord.RenewResults(granted,
                denied.isEmpty() ? null : (Exception[]) denied.toArray());