/*
 * 
 * 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 net.jini.jeri;

import java.rmi.Remote;
import java.rmi.server.ExportException;
import net.jini.core.constraint.MethodConstraints;
import net.jini.security.proxytrust.ProxyTrust;
import net.jini.security.proxytrust.ServerProxyTrust;

/**
 * Invocation layer factory for remote objects exported to use Jini(TM)
 * extensible remote invocation (Jini ERI) that produces proxies that
 * additionally implement the {@link ProxyTrust} interface. The remote object
 * being exported must be an instance of {@link ServerProxyTrust}, and the
 * {@link ProxyTrust#getProxyVerifier ProxyTrust.getProxyVerifier} remote
 * method of the proxy is implemented in the invocation dispatcher by
 * invoking the {@link ServerProxyTrust#getProxyVerifier
 * ServerProxyTrust.getProxyVerifier} local method of the remote object.
 *
 * @author Sun Microsystems, Inc.
 * @since 2.0
 * @see net.jini.security.proxytrust.ProxyTrustExporter
 */
public class ProxyTrustILFactory extends BasicILFactory {

    /**
     * Creates a factory with the specified server constraints, permission
     * class, and a <code>null</code> class loader. The server constraints,
     * if not <code>null</code>, are used to enforce minimum constraints
     * for remote calls. The permission class, if not <code>null</code>, is
     * used to perform server-side access control on incoming remote calls.
     *
     * @param serverConstraints the server constraints, or <code>null</code>
     * @param permissionClass the permission class, or <code>null</code>
     * @throws IllegalArgumentException if the permission class is abstract, is
     * not a subclass of {@link java.security.Permission}, or does not have
     * a public constructor that has either one <code>String</code> parameter
     * or one {@link java.lang.reflect.Method} parameter and has no declared
     * exceptions
     **/
    public ProxyTrustILFactory(MethodConstraints serverConstraints,
			       Class permissionClass)
    {
	this(serverConstraints, permissionClass, null);
    }
    
    /**
     * Creates a factory with the specified server constraints, permission
     * class, and class loader. The server constraints, if not
     * <code>null</code>, are used to enforce minimum constraints for
     * remote calls. The permission class, if not <code>null</code>, is
     * used to perform server-side access control on incoming remote calls.
     * The specified loader is passed to the superclass constructor and
     * used by the {@link #createInstances createInstances} method.
     *
     * @param serverConstraints the server constraints, or <code>null</code>
     * @param permissionClass the permission class, or <code>null</code>
     * @param loader the class loader, or <code>null</code>
     * @throws IllegalArgumentException if the permission class is abstract, is
     * not a subclass of {@link java.security.Permission}, or does not have
     * a public constructor that has either one <code>String</code> parameter
     * or one {@link java.lang.reflect.Method} parameter and has no declared
     * exceptions
     **/
    public ProxyTrustILFactory(MethodConstraints serverConstraints,
			       Class permissionClass,
			       ClassLoader loader)

    {
	super(serverConstraints, permissionClass, loader);
    }

    /**
     * Returns a new array containing the remote interfaces that should be
     * implemented by the proxy.
     *
     * <p><code>ProxyTrustILFactory</code> implements this method to return
     * an array containing the interfaces obtained by invoking {@link
     * BasicILFactory#getRemoteInterfaces super.getRemoteInterfaces}
     * passing <code>impl</code> followed by the {@link ProxyTrust}
     * interface.
     *
     * @throws NullPointerException {@inheritDoc}
     * @throws ExportException if the remote object is not an instance of
     * {@link ServerProxyTrust} or implements any illegal remote interfaces
     **/
    protected Class[] getRemoteInterfaces(Remote impl) throws ExportException {
	if (impl != null && !(impl instanceof ServerProxyTrust)) {
	    throw new ExportException("impl must implement ServerProxyTrust");
	}
	Class[] ifs = super.getRemoteInterfaces(impl);
	if (ifs == null) {
	    return new Class[]{ProxyTrust.class};
	}
	Class[] nifs = new Class[ifs.length + 1];
	System.arraycopy(ifs, 0, nifs, 0, ifs.length);
	nifs[ifs.length] = ProxyTrust.class;
	return nifs;
    }
}
