FileDocCategorySizeDatePackage
ServerLandlord.javaAPI DocExample7759Thu Mar 16 11:52:34 GMT 2000None

ServerLandlord

public class ServerLandlord extends UnicastRemoteObject implements Landlord

Fields Summary
protected Hashtable
expiration
protected Hashtable
leases
protected Hashtable
session
protected long
MAXLEASETIME
protected LandlordLease.Factory
factory
static int
token
Constructors Summary
public ServerLandlord()


        
    
Methods Summary
public voidcancel(java.lang.Object cookie)

        synchronized (this) {
            if (expiration.get(cookie) == null) 
                throw new UnknownLeaseException();
            expiration.remove(cookie);
            leases.remove(cookie);
            session.remove(cookie);
        }
    
public voidcancelAll(java.lang.Object[] cookie)

        Map map = null;
        // Cancel all the leases. If one or more has an exception,
        // then continue but save the exception in a map to be
        // thrown back to the user.
        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 LeaseMapException("Can't cancel all leases", map);
    
public voidexpireLeases()

        long now = System.currentTimeMillis();
        now = now - 60 * 1000;          // Be conservative by one minute

        synchronized (this) {
            Vector deleteList = new Vector();
            for (Enumeration e = expiration.keys();
                             e.hasMoreElements();) {
                Object cookie = e.nextElement();
                Long expire = (Long) expiration.get(cookie);
                if (expire.longValue() < now) {
                    deleteList.addElement(cookie);
                }
            }
            for (Enumeration e = deleteList.elements();
                             e.hasMoreElements();) {
                Object cookie = e.nextElement();
                expiration.remove(cookie);
                leases.remove(cookie);
                session.remove(cookie);
            }
        }
    
public java.lang.ObjectgetSessionData(Lease lease)

        Object sessiondata = null;
        if (lease instanceof LandlordLease) {
            expireLeases();        // Make sure the lease is still valid
            synchronized (this) {
                for (Enumeration e = leases.keys();
                                 e.hasMoreElements();) {
                    Object cookie = e.nextElement();
                    if (lease.equals(leases.get(cookie))) {
                        sessiondata = session.get(cookie);
                        break;
                    }
                }
            }
        }
        return sessiondata;
    
public LeasenewLease(java.lang.Object sessionData, long duration)

            // Unique ID for cookie
          
        long now = System.currentTimeMillis();  // Time when requested

        Integer cookie;                // Get unique cookie
        synchronized(ServerLandlord.class) {
            cookie = new Integer(token++);
        }

        expireLeases();                // Make room for new Leases
        if (duration == Lease.ANY ||
            duration == Lease.FOREVER ||
            duration > MAXLEASETIME)
            duration = MAXLEASETIME;

        synchronized(this) {
            Lease l = factory.newLease(cookie, this, duration);
            expiration.put(cookie, new Long(now + duration));
            leases.put(cookie, l);
            if (sessionData != null) {
                session.put(cookie, sessionData);
            }
            return l;
        }
    
public longrenew(java.lang.Object cookie, long duration)


        // Time when the client first attempted renewal
        long now = System.currentTimeMillis();

        // Reset the duration to the maximum that we