/*
 * 
 * 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 java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.rmi.RemoteException;
import net.jini.core.constraint.RemoteMethodControl;
import net.jini.core.lookup.ServiceID;
import net.jini.security.TrustVerifier;
import net.jini.security.proxytrust.TrustEquivalence;

/**
 * Trust verifier for smart proxies used by Reggie.
 *
 * @author Sun Microsystems, Inc.
 *
 */
final class ProxyVerifier implements TrustVerifier, Serializable {

    private static final long serialVersionUID = 2L;

    /**
     * Canonical service reference, used for comparison with inner server
     * references extracted from smart proxies to verify.
     *
     * @serial
     */
    private final RemoteMethodControl server;
    /**
     * The registrar's service ID, used for comparison with registrar service
     * IDs extracted from smart proxies to verify.
     */
    private transient ServiceID registrarID;

    /**
     * Constructs proxy verifier which compares server references extracted
     * from smart proxies with the given canonical server reference, which must
     * implement both RemoteMethodControl and TrustEquivalence.  For proxies
     * which contain a copy of the registrar's service ID, that copy is
     * compared against the given service ID to ensure consistency.
     */
    ProxyVerifier(Registrar server, ServiceID registrarID) {
	if (!(server instanceof RemoteMethodControl)) {
	    throw new UnsupportedOperationException(
		"server does not implement RemoteMethodControl");
	} else if (!(server instanceof TrustEquivalence)) {
	    throw new UnsupportedOperationException(
		"server does not implement TrustEquivalence");
	}
	this.server = (RemoteMethodControl) server;
	this.registrarID = registrarID;
    }

    /**
     * Returns true if the given object is a trusted proxy, or false otherwise.
     * The given object is trusted if it is trust equivalent to the canonical
     * server reference carried by this trust verifier, or if it is an instance
     * of one of Reggie's constrainable smart proxy classes, and all component
     * proxies it contains are trusted, and its inner server reference is trust
     * equivalent to the canonical server reference, and its inner copy of the
     * registrar's service ID (if it has one) is equal to the service ID
     * carried by this verifier.
     */
    public boolean isTrustedObject(Object obj, TrustVerifier.Context ctx)
	throws RemoteException
    {
	if (obj == null || ctx == null) {
	    throw new NullPointerException();
	}
	RemoteMethodControl inputServer;
	ServiceID inputRegistrarID;
	if (obj instanceof ConstrainableRegistrarProxy) {
	    RegistrarProxy proxy = (RegistrarProxy) obj;
	    inputServer = (RemoteMethodControl) proxy.server;
	    inputRegistrarID = proxy.registrarID;
	} else if (obj instanceof ConstrainableAdminProxy) {
	    AdminProxy proxy = (AdminProxy) obj;
	    inputServer = (RemoteMethodControl) proxy.server;
	    inputRegistrarID = proxy.registrarID;
	} else if (obj instanceof ConstrainableRegistration) {
	    Registration reg = (Registration) obj;
	    if (!isTrustedObject(reg.lease, ctx)) {
		return false;
	    }
	    inputServer = (RemoteMethodControl) reg.server;
	    inputRegistrarID = registrarID;
	} else if (obj instanceof ConstrainableEventLease ||
		   obj instanceof ConstrainableServiceLease)
	{
	    RegistrarLease lease = (RegistrarLease) obj;
	    inputServer = (RemoteMethodControl) lease.server;
	    inputRegistrarID = lease.registrarID;
	} else if (obj instanceof RemoteMethodControl) {
	    inputServer = (RemoteMethodControl) obj;
	    inputRegistrarID = registrarID;
	} else {
	    return false;
	}

	TrustEquivalence trustEquiv = (TrustEquivalence)
	    server.setConstraints(inputServer.getConstraints());
	return trustEquiv.checkTrustEquivalence(inputServer) &&
	       registrarID.equals(inputRegistrarID);
    }

    /**
     * Writes the default serializable field value for this instance, followed
     * by the registrar's service ID encoded as specified by the
     * ServiceID.writeBytes method.
     */
    private void writeObject(ObjectOutputStream out) throws IOException {
	out.defaultWriteObject();
	registrarID.writeBytes(out);
    }

    /**
     * Reads the default serializable field value for this instance, followed
     * by the registrar's service ID encoded as specified by the
     * ServiceID.writeBytes method.  Verifies that the deserialized registrar
     * reference implements both RemoteMethodControl and TrustEquivalence.
     */
    private void readObject(ObjectInputStream in)
	throws IOException, ClassNotFoundException
    {
	in.defaultReadObject();
	registrarID = new ServiceID(in);
	if (!(server instanceof RemoteMethodControl)) {
	    throw new InvalidObjectException(
		"server does not implement RemoteMethodControl");
	} else if (!(server instanceof TrustEquivalence)) {
	    throw new InvalidObjectException(
		"server does not implement TrustEquivalence");
	}
    }
}
