/*
 * 
 * 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 net.jini.core.constraint.RemoteMethodControl;
import net.jini.security.TrustVerifier;
import net.jini.security.proxytrust.TrustEquivalence;
import net.jini.id.Uuid;
import net.jini.id.ReferentUuid;

/**
 * Factory for {@link LandlordLease} instances.
 *
 * @author Sun Microsystems, Inc.
 * @since 2.0
 */
public class LeaseFactory {
    /** The inner proxy for the leases we create */
    final private Landlord landlord;

    /** The <code>Uuid</code> of the server granting the leases */
    final private Uuid landlordUuid;

    /**
     * Create a new <code>LeaseFactory</code> that will create
     * <code>LandlordLease</code>s with the specified
     * <code>Landlord</code> and landlord <code>Uuid</code>.
     * @param landlord the inner proxy that the leases will
     *        use to communicate back to the server.
     * @param landlordUuid a universally unique id that has
     *        been assigned to the server granting of the lease.
     *        Ideally the <code>Uuid</code> {@link 
     *        ReferentUuid#getReferentUuid landlord.getUuid}
     *        would return if <code>landlord</code> implemented
     *        {@link ReferentUuid}. Used
     *        to determine when two leases can be batched together.
     * @throws NullPointerException if either argument is 
     *         <code>null</code>
     */
    public LeaseFactory(Landlord landlord, Uuid landlordUuid) {
	if (landlord == null)
	    throw new NullPointerException("landlord must be non-null");

	if (landlordUuid == null)
	    throw new NullPointerException("landlordUuid must be non-null");

	this.landlord = landlord;
	this.landlordUuid = landlordUuid;
    }

    /**
     * Return a new <code>LandlordLease</code> with the specified
     * initial expiration and cookie using the inner proxy and 
     * <code>Uuid</code> the factory was created with. Will return
     * a {@link ConstrainableLandlordLease} if inner proxy this
     * factory was created with implements {@link RemoteMethodControl}.
     * @param cookie a <code>Uuid</code> that will universally and uniquely 
     *        identify the lease
     * @param expiration the initial expiration time of the lease.
     * @return a new <code>LandlordLease</code>.
     * @throws NullPointerException if the <code>cookie</code> argument is 
     *         <code>null</code>.
     */
    public LandlordLease newLease(Uuid cookie, long expiration) {
	if (landlord instanceof RemoteMethodControl)
	    return new ConstrainableLandlordLease(cookie, landlord,
	        landlordUuid, expiration, null);
	return new LandlordLease(cookie, landlord, landlordUuid, expiration);
    }

    /**
     * Return a <code>TrustVerifier</code> that will verify the 
     * proxies produced by this factory. Currently the verifier
     * returned will be an instance of {@link LandlordProxyVerifier}.
     * @return a new <code>TrustVerifier</code>.
     * @throws UnsupportedOperationException if the {@link Landlord} this
     * factory was created with does not implement both
     * {@link RemoteMethodControl} and {@link TrustEquivalence}.
     */
    public TrustVerifier getVerifier() {
	return new LandlordProxyVerifier(landlord, landlordUuid);
    }
}
