/*
 * 
 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * The contents of this file are subject to the Sun Community Source
 * License v3.0/Jini Technology Specific Attachment v 1.0 (the
 * "License"). You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://wwws.sun.com/software/jini/licensing/SCSL3_JiniTSA1.html.
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Reference Code is Jini Technology Core Platform code, v2.0. The
 * Developer of the Reference Code is Sun Microsystems, Inc.
 * 
 * Contributor(s): Sun Microsystems, Inc.
 * 
 * The contents of this file comply with the Jini Technology Core
 * Platform Compatibility Kit, v2.0A.
 * 
 * Tester(s): Sun Microsystems, Inc.
 * 
 * Test Platform(s):
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Solaris(TM)SPARC/x86
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Linux (Intel x86)
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Windows (Intel Platform)
 * 
 */
package com.sun.jini.landlord;

import java.io.Serializable; 
import java.rmi.Remote;
import java.rmi.RemoteException;
import net.jini.core.lease.Lease;
import net.jini.core.lease.LeaseMap;
import net.jini.core.lease.LeaseDeniedException;
import net.jini.core.lease.UnknownLeaseException;
import net.jini.id.Uuid;

/** 
 * Interface that granters of leases must implement in order to work
 * with the <code>LandlordLease</code> implementation of the
 * <code>Lease</code> interface.
 *
 * @author Sun Microsystems, Inc.
 *
 * @see net.jini.core.lease.Lease
 * @see LandlordLease 
 * @since 2.0
 */
public interface Landlord extends Remote {
    /**
     * Called by the lease when its {@link Lease#renew renew} method is called.
     * Renews the lease that is associated with the given <code>cookie</code>.
     *
     * @param cookie the <code>Uuid</code> associated with the lease who's 
     *               <code>renew</code> method was called
     * @param duration argument passed to the <code>renew</code> call
     * @return The new duration the lease should have 
     * @throws LeaseDeniedException if the landlord is unwilling to
     *         renew the lease
     * @throws UnknownLeaseException if landlord does not know about
     *         a lease with the specified <code>cookie</code>
     * @throws RemoteException if a communications failure occurs
     */
    public long renew(Uuid cookie, long duration)
	throws LeaseDeniedException, UnknownLeaseException, RemoteException;
       
    /**
     * Called by the lease when its {@link Lease#cancel cancel} method is 
     * called. Cancels the lease that is associated with the given
     * <code>cookie</code>.
     *
     * @param cookie the <code>Uuid</code> associated with the lease who's 
     *               <code>renew</code> method was called
     * @throws UnknownLeaseException if landlord does not know about
     *         a lease with the specified <code>cookie</code>
     * @throws RemoteException if a communications failure occurs
     */
    public void cancel(Uuid cookie)
	throws UnknownLeaseException, RemoteException;

    /**
     * Called by the lease map when its {@link LeaseMap#renewAll
     * renewAll} method is called.  Should renew the lease that is
     * associated with each element of <code>cookies</code>
     *
     * @param cookies an array of <code>Uuid</code>s, each universally and 
     *                uniquely identifying a lease granted by this
     *                <code>Landlord</code>
     * @param durations
     *                an array of longs, each representing an a
     *                duration in milliseconds that the client
     *                wants the lease associated with the <code>Uuid</code>
     *                from the corresponding element of
     *                <code>cookies</code> renewed for
     * @return A RenewResults object that contains the new
     *         duration of each lease that was successfully renewed or
     *         the exception encountered for each lease that could not
     *         be renewed
     * @throws RemoteException if a communications failure occurs
     */
    public RenewResults renewAll(Uuid[] cookies, long[] durations)
	throws RemoteException;
       
    /**
     * Called by the lease map when its {@link LeaseMap#cancelAll
     * cancelAll} method is called.  Should cancel the lease that is
     * associated with each element of <code>cookies</code>
     * 
     * @param cookies an array of <code>Uuid</code>s, each universally and 
     *                uniquely identifying a lease granted by this
     *                <code>Landlord</code>
     * @return If all the leases specified in the <code>cookies</code>
     *         could be cancelled return <code>null</code>.  Otherwise,
     *         return a <code>Map</code> that for each failed cancel
     *         attempt maps the corresponding cookie object to an
     *         exception describing the failure.  
     * @throws RemoteException if a communications failure occurs
     */
    public java.util.Map cancelAll(Uuid[] cookies) throws RemoteException;

    /** 
     * Simple class that holds return values of
     * the {@link Landlord#renewAll Landlord.renewAll} method.
     */
    public class RenewResults implements java.io.Serializable {
	static final long serialVersionUID = 2L;

	/**
	 * For each cookie passed to {@link Landlord#renewAll renewAll},
	 * <code>granted[i]</code> is the granted lease time, or -1 if the
	 * renewal for that lease generated an exception.  If there was
	 * an exception, the exception is held in <code>denied</code>.
	 *
	 * @see #denied
	 * @serial
	 */
	public long[] granted;

	/**
	 * The <code>i</code><sup><i>th</i></sup> -1 in <code>granted</code>
	 * was denied because of <code>denied[i]</code>.  If nothing was 
	 * denied, this field is <code>null</code>.
	 *
	 * @serial
	 */
	public Exception[] denied;

	/**
	 * Create a <code>RenewResults</code> object setting the field
	 * <code>granted</code> to the passed value, and <code>denied</code>
	 * to <code>null</code>.
	 *
	 * @param granted	The value for the field <code>granted</code>
	 */
	public RenewResults(long[] granted) {
	    this(granted, null);
	}

	/**
	 * Create a <code>RenewResults</code> object setting the field
	 * <code>granted</code> and <code>denied</code> fields to the
	 * passed values.
	 *
	 * @param granted	the value for the field <code>granted</code>
	 * @param denied	the value for the field <code>denied</code>
	 */
	public RenewResults(long[] granted, Exception[] denied) {
	    this.granted = granted;
	    this.denied = denied;
	}
    }
}
