/*
 * Filename: GenericLandlord.java
 *
 * Author: Joe Wang
 *
 * Copyright (c) 2003-2005 
 * The Trustees of Columbia University in the City of New York
 * Columbia University Information Technology
 *
 * This file is part of NINJa.
 *
 * NINJa is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * NINJa is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with NINJa; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

package edu.columbia.print.ninja.core.service;

import net.jini.core.lease.*;
import com.sun.jini.lease.landlord.LandlordUtil;

/**
 * Landlord to control renewal of licenses associated with displays
 * @author Joe Wang <jw168@columbia.edu>
 * @since 1.0
 */
class GenericLandlord implements com.sun.jini.lease.landlord.LocalLandlord, com.sun.jini.lease.landlord.Landlord, java.io.Serializable {
    //	transient MultiLeasedResource rsrc;
    transient MultiLeasedResource rsrc;
    transient long renewDuration;
    transient CancelListener listener;
    
    /**
     * @param parent the list the contains the set of displays
     */
    GenericLandlord(MultiLeasedResource rsrc, CancelListener listener, long renewDuration) {
        this.rsrc=rsrc;
        this.listener = listener;
        this.renewDuration = renewDuration;
    }
    public void cancel(Object cookie) throws UnknownLeaseException {
        Object value = rsrc.getValue(cookie);
        rsrc.releaseResource(cookie);
        System.out.println("cancel "+cookie);
        if(listener!=null) listener.leaseCancelled(value, rsrc);
    }
    void debug(Object i) {com.millipad.util.Debug.print(GenericLandlord.class, i);}
    
    public long renew(Object cookie, long duration)
    throws LeaseDeniedException, UnknownLeaseException {
        //lookup lease
        if(renewDuration>=0) {
            if(duration==Lease.ANY || duration>renewDuration) duration=renewDuration;
            com.sun.jini.lease.landlord.LeasedResource tmp = rsrc.getLeasedResource(cookie);
            if(tmp==null) throw new UnknownLeaseException("no leasedResource for "+cookie);
//            System.out.println("renew "+rsrc.getName()+": +"+duration+" ="+(System.currentTimeMillis()-tmp.getExpiration()));
            tmp.setExpiration(System.currentTimeMillis()+duration);
            return duration;
        }
        try {
            long expire = rsrc.getLeasedResource(cookie).getExpiration();
            long nextDuration = expire-System.currentTimeMillis();
            if(duration==Lease.ANY || duration>nextDuration) duration = nextDuration;
            if(duration<=0) throw new Exception("out of time");
        }
        catch(Exception e) {
            cancel(cookie);
            throw new LeaseDeniedException();
        }
        return duration;
    }
    public java.util.Map cancelAll(Object[] cookies) {
        return LandlordUtil.cancelAll(this, cookies);
    }
    public com.sun.jini.lease.landlord.Landlord.RenewResults renewAll(Object[] cookies, long[] durations) {
        return LandlordUtil.renewAll(this, cookies, durations);
    }
}
