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

import java.util.HashMap;
import java.util.Map;
import java.rmi.RemoteException;

import net.jini.core.lease.LeaseException;
import net.jini.core.lease.LeaseMapException;

import com.sun.jini.lease.AbstractLeaseMap;

/**
 * An implementation of LeaseMap that holds exactly one lease.  Used when
 * we have a deformed lease that we won't be able to batch with anyone
 * else and we want to call ClientLeaseWrapper.renew() instead of
 * LeaseMap.renewAll().
 * <p>
 * Provides hooks for synchronization and data associated with each
 * client lease while allowing us to use
 * <code>LeaseRenewalManager</code>.  Objects of this class are
 * returned by <code>createLeaseMap</code> calls made on
 * <code>ClientLeaseWrapper</code> objects that are deformed.
 *
 * @author Sun Microsystems, Inc.
 * @see ClientLeaseWrapper 
 */
class DeformedClientLeaseMapWrapper extends AbstractLeaseMap {
    private static final long serialVersionUID = 1L;

    /**
     * Create a DeformedClientLeaseMapWrapper.
     * @param lease a Wrapper for the lease that wants to be renewed.
     *              May be deformed.
     * @param duration the duration to associate with lease
     */
    DeformedClientLeaseMapWrapper(ClientLeaseWrapper lease, long duration) {
	super(new HashMap(1), lease, duration);	
    }

    // inherit javadoc
    public void cancelAll() {
	throw new UnsupportedOperationException(
	     "ClientLeaseMapWrapper.cancelAll: " + 
	     "LRS should not being canceling client leases");
    }

    // inherit javadoc
    public void renewAll() throws LeaseMapException, RemoteException {
	ClientLeaseWrapper l = 
	    (ClientLeaseWrapper) (map.keySet().iterator().next());
	long d = ((Integer) (map.get(l))).longValue();
	try {
	    l.renew(d);
	} catch (LeaseException e) {
	    final Map m = new HashMap(1);
	    m.put(l, e);
	    throw new LeaseMapException(e.getMessage(), m); 
	} 
    }

    // inherit javadoc
    public boolean canContainKey(Object key) {
	// DeformedClientLeaseMapWrapper can only be created with exactly one
	// lease, and currently they can only contain one lease, so
	// return false unless the lease passed in is the one we already
	// have.
	return map.containsKey(key);
    }
}
