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

ServerLandlord.java

/*
 *
 * 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.rmi.*;
import java.rmi.server.*;
import java.util.*;
import net.jini.core.lease.*;
import com.sun.jini.lease.landlord.*;

public class ServerLandlord extends UnicastRemoteObject implements Landlord {
              // cookie -> ServerResource
    protected Hashtable resources = new Hashtable();
    protected long MAXLEASETIME = 10*60*1000;        // 10 Minutes
    protected LeasePolicy policy;

    public ServerLandlord() throws RemoteException {
        policy = new LeaseDurationPolicy(MAXLEASETIME, MAXLEASETIME, this, new Expirer(this), null);
    }

    public long renew(Object cookie, long duration)
            throws LeaseDeniedException, UnknownLeaseException, RemoteException {
        synchronized (this) {
            ServerResource sr = (ServerResource)resources.get(cookie);
            if (sr == null)
                throw new UnknownLeaseException();

            return policy.renew(sr, duration);
        }
    }

    public Landlord.RenewResults renewAll(Object[] cookie, long[] duration)
            throws RemoteException {
        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());
    }

    public void cancel(Object cookie) 
            throws UnknownLeaseException, RemoteException {
        synchronized (this) {
            ServerResource sr = (ServerResource)resources.get(cookie);
            if (sr == null)
                throw new UnknownLeaseException();

            resources.remove(cookie);
        }
    }

    public void cancelAll(Object[] cookie) 
            throws LeaseMapException, RemoteException {
        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 LeaseMapException("Can't cancel all leases", map);
    }

    // The following methods are not part of the Landlord interface
    //    and can only be called by directly using this object.
    public Lease newLease(Object sessionData, long duration) {
        ServerResource sr = new ServerResource(sessionData);

        try {
            sr.lease = policy.leaseFor(sr, duration);
            synchronized(this) {
                resources.put(sr.getCookie(), sr);
            }
            return sr.lease;
        } catch (LeaseDeniedException e) {};
        return null;
    }

    // The cookie is an *internal* reference for the LandlordLease. No one outside
    //    the lease can access it.
    public Object getSessionData(Lease lease) {
        Object sessiondata = null;
        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;
    }
}