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


import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import net.jini.core.constraint.MethodConstraints;
import net.jini.core.constraint.RemoteMethodControl;
import net.jini.security.TrustVerifier;
import net.jini.security.proxytrust.ServerProxyTrust;
import net.jini.security.proxytrust.TrustEquivalence;

/**
 * A basic trust verifier for proxies.  This trust verifier is used to
 * verify that object passed to its {@link #isTrustedObject
 * isTrustedObject} method is equivalent in trust, content, and function to
 * the known trusted object that the trust verifier is constructed with.
 * This trust verifier is typically returned by an implementation of the
 * {@link ServerProxyTrust#getProxyVerifier
 * ServerProxyTrust.getProxyVerifier} method.
 *
 * @author Sun Microsystems, Inc.
 * @since 2.0
 **/
public final class BasicProxyTrustVerifier
    implements TrustVerifier, Serializable
{
    private static final long serialVersionUID = 2L;
    
    /**
     * The trusted proxy.
     *
     * @serial
     */
    private final RemoteMethodControl proxy;

    /**
     * Creates a trust verifier containing the specified trusted proxy. 
     *
     * @param proxy the trusted proxy
     * @throws IllegalArgumentException if the specified proxy is
     * not an instance of {@link RemoteMethodControl} or
     * {@link TrustEquivalence}
     */
    public BasicProxyTrustVerifier(Object proxy) {
	if (!(proxy instanceof RemoteMethodControl)) {
	    throw new IllegalArgumentException(
		"proxy not a RemoteMethodControl instance");
	} else if  (!(proxy instanceof TrustEquivalence)) {
	    throw new IllegalArgumentException(
		"proxy not a TrustEquivalence instance");
	}
	this.proxy = (RemoteMethodControl) proxy;
    }
	
    /**
     * Verifies trust in a proxy. Returns <code>true</code> if and only if
     * the specified object is an instance of {@link RemoteMethodControl}
     * and invoking the {@link TrustEquivalence#checkTrustEquivalence
     * checkTrustEquivalence} method on a proxy that is this verifier's
     * proxy with the same method constraints of the specified object,
     * passing the specified object returns <code>true</code>.
     **/
    public boolean isTrustedObject(Object obj, TrustVerifier.Context ctx) {
	if (obj == null || ctx == null) {
	    throw new NullPointerException();
	} else if (!(obj instanceof RemoteMethodControl)) {
	    return false;
	}
	RemoteMethodControl unverifiedProxy = (RemoteMethodControl) obj;
	MethodConstraints constraints = unverifiedProxy.getConstraints();
	TrustEquivalence trustedProxy =
	    (TrustEquivalence) proxy.setConstraints(constraints);
	return trustedProxy.checkTrustEquivalence(unverifiedProxy);
    }

    /**
     * @throws InvalidObjectException if the proxy is not an instance of
     * {@link RemoteMethodControl} and {@link TrustEquivalence}
     **/
    private void readObject(ObjectInputStream in)
	throws IOException, ClassNotFoundException
    {
	in.defaultReadObject();
	if (!(proxy instanceof RemoteMethodControl)) {
	    throw new InvalidObjectException(
		"proxy not a RemoteMethodControl instance");
	} else if  (!(proxy instanceof TrustEquivalence)) {
	    throw new InvalidObjectException(
		"proxy not a TrustEquivalence instance");
	}
    }
}
    
