/*
 * 
 * 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.reggie;

import com.sun.jini.lease.AbstractLeaseMap;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import net.jini.core.lease.LeaseMapException;
import net.jini.core.lookup.ServiceID;
import net.jini.id.Uuid;

/**
 * The LeaseMap implementation class for registrar leases.  Clients only see
 * instances via the LeaseMap interface.
 *
 * @author Sun Microsystems, Inc.
 *
 */
class RegistrarLeaseMap extends AbstractLeaseMap {

    private static final long serialVersionUID = 2L;

    /**
     * The registrar.
     *
     * @serial
     */
    final Registrar server;
    /**
     * The registrar's service ID.
     *
     * @serial
     */
    final ServiceID registrarID;

    /** Simple constructor */
    RegistrarLeaseMap(RegistrarLease lease, long duration) {
	this(lease.getRegistrar(), lease, duration);
    }

    /** Constructor used by ConstrainableRegistrarLeaseMap */
    RegistrarLeaseMap(Registrar server, RegistrarLease lease, long duration) {
	super(lease, duration);
	this.server = server;
	registrarID = lease.getRegistrarID();
    }

    /** Any RegistrarLease from the same server can be in the map */
    public boolean canContainKey(Object key) {
	return (key instanceof RegistrarLease &&
		registrarID.equals(((RegistrarLease) key).getRegistrarID()));
    }

    // This method's javadoc is inherited from an interface of this class
    public void renewAll() throws LeaseMapException, RemoteException {
	int size = map.size();
	if (size == 0)
	    return;
	Object[] regIDs = new Object[size];
	Uuid[] leaseIDs = new Uuid[size];
	long[] durations = new long[size];
	int i = 0;
	for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); i++) {
	    Map.Entry e = (Map.Entry)iter.next();
	    RegistrarLease ls = (RegistrarLease)e.getKey();
	    regIDs[i] = ls.getRegID();
	    leaseIDs[i] = ls.getReferentUuid();
	    durations[i] = ((Long)e.getValue()).longValue();
	}
	RenewResults results = server.renewLeases(regIDs, leaseIDs, durations);
	long now = System.currentTimeMillis();
	HashMap emap = (results.exceptions != null) ?
	    	       new HashMap(2 * results.exceptions.length + 1) : null;
	i = 0;
	int j = 0;
	for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); i++) {
	    Map.Entry e = (Map.Entry)iter.next();
	    long duration = results.durations[i];
	    if (duration >= 0) {
		((RegistrarLease)e.getKey()).setExpiration(duration + now);
	    } else {
		emap.put(e.getKey(), results.exceptions[j++]);
		iter.remove();
	    }
	}
	if (emap != null)
	    throw new LeaseMapException("lease renewal failures", emap);
    }

    // This method's javadoc is inherited from an interface of this class
    public void cancelAll() throws LeaseMapException, RemoteException {
	int size = map.size();
	if (size == 0)
	    return;
	Object[] regIDs = new Object[size];
	Uuid[] leaseIDs = new Uuid[size];
	int i = 0;
	for (Iterator iter = map.keySet().iterator(); iter.hasNext(); i++) {
	    RegistrarLease ls = (RegistrarLease)iter.next();
	    regIDs[i] = ls.getRegID();
	    leaseIDs[i] = ls.getReferentUuid();
	}
	Exception[] exceptions = server.cancelLeases(regIDs, leaseIDs);
	if (exceptions == null)
	    return;
	i = 0;
	HashMap emap = new HashMap(13);
	for (Iterator iter = map.keySet().iterator(); iter.hasNext(); i++) {
	    RegistrarLease ls = (RegistrarLease)iter.next();
	    Exception ex = exceptions[i];
	    if (ex != null) {
		emap.put(ls, ex);
		iter.remove();
	    }
	}
	throw new LeaseMapException("lease cancellation failures", emap);
    }
}
