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

import java.lang.reflect.Method;
import java.rmi.server.ExportException;
import java.rmi.Remote;
import java.util.Collection;
import net.jini.core.constraint.InvocationConstraints;
import net.jini.jeri.BasicILFactory;
import net.jini.jeri.BasicInvocationDispatcher;
import net.jini.jeri.InvocationDispatcher;
import net.jini.jeri.ServerCapabilities;

/**
 * A basic invocation layer factory, used in exporting an
 * <code>ActivationMonitor</code> or <code>ActivationInstantiator</code> to
 * use Jini(TM) extensible remote invocation (Jini ERI), that is similar to
 * {@link BasicILFactory} except the returned invocation dispatcher only
 * accepts calls from the local host.
 *
 * @author Sun Microsystems, Inc.
 * 
 * @since 2.0
 **/
public class AccessILFactory extends BasicILFactory {

    /**
     * Creates an <code>AccessILFactory</code>instance with no server
     * constraints, no permission class, and a <code>null</code> class
     * loader.
     **/
    public AccessILFactory() {
    }

    /**
     * Creates an <code>AccessILFactory</code>instance with no server
     * constraints, no permission class, and the specified class loader.
     * The specified class loader is used by the {@link #createInstances
     * createInstances} method.
     *
     * @param loader the class loader, or <code>null</code>
     **/
    public AccessILFactory(ClassLoader loader) {
	super(null, null, loader);
    }

    /**
     * Returns an {@link AccessDispatcher} instance constructed with the
     * specified methods, the specified server capabilities, and the class
     * loader specified at construction.
     *
     * @return the {@link AccessDispatcher} instance
     * @throws NullPointerException {@inheritDoc}
     **/
    protected InvocationDispatcher
        createInvocationDispatcher(Collection methods,
				   Remote impl,
				   ServerCapabilities caps)
	throws ExportException
    {
	if (impl == null) {
	    throw new NullPointerException("impl is null");
	}
	return new AccessDispatcher(methods, caps, getClassLoader());
    }

    /**
     * A subclass of {@link BasicInvocationDispatcher} that only accepts
     * calls from the local host.
     */
    public static class AccessDispatcher extends BasicInvocationDispatcher {
	/**
	 * Constructs an invocation dispatcher for the specified methods.
	 *
	 * @param	methods a collection of {@link Method} instances
	 *		for the	remote methods
	 * @param	caps the transport capabilities of the server
	 * @param	loader the class loader, or <code>null</code>
	 * @throws	NullPointerException if <code>methods</code> is
	 *		<code>null</code> or if <code>methods</code> contains a
	 *		<code>null</code> elememt
	 * @throws	IllegalArgumentException if <code>methods</code>
	 *		contains an element that is not a <code>Method</code>
	 *		instance
	 */
	public AccessDispatcher(Collection methods,
				ServerCapabilities caps,
				ClassLoader loader)
	    throws ExportException
	{
	    super(methods, caps, null, null, loader);
	}

	/**
	 * Checks that the client is calling from the local host.
	 *
	 * @throws java.security.AccessControlException if the client is not
	 * calling from the local host
	 */
	protected void checkAccess(Remote impl,
				   Method method,
				   InvocationConstraints constraints,
				   Collection context)
	{
	    LocalAccess.check();
	}
    }
}
