/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Initial developer(s): ____________________________________.
 * Contributor(s): ______________________________________.
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.security.propagation;

import org.objectweb.jonathan.apis.kernel.Context;
import org.objectweb.jonathan.apis.kernel.InternalException;
import org.objectweb.jonathan.apis.kernel.JonathanException;
import org.objectweb.jonathan.presentation.api.MarshallerFactory;
import org.objectweb.jonathan.libs.kernel.GenericFactory;


/** 
 * {@link SSHandler SSHandler} 
 */
public class SSHandlerFactory extends GenericFactory {

    /** 
     * Name used to designate the context containing the SSHandler components. 
     */
    static public final String jonasss_context_name = "/jonas/security/SSHandler";

    /** 
     * Returns the components required to create a new 
     * {@link SSHandler SSHandler} instance.
     * <p>
     * <code>c</code> must contain a (non null) component of name 
     * "MarshallerFactory" of type {@link MarshallerFactory MarshallerFactory}.
     * <p>
     * It may contain a component of name "id", of type Integer; this id
     * should then represent the service id to use for the created service.
     * <p>
     * It may contain a component of name "sender", of type {@link Sender Sender},
     * and a component of name "receiver", of type {@link Receiver Receiver}.
     * 
     * @param c a {@link Context Context} instance;
     * @return the components needed to create an SSHandler instance.
     */
    protected Object[] getUsedComponents(Context c) {
      
        Object[] used_components = { 
            Context.NO_VALUE, Context.NO_VALUE, Context.NO_VALUE, Context.NO_VALUE 
        };

        if (c != null) {
            used_components[0] = c.getValue("id", (char) 0);
            used_components[1] = c.getValue("sender", (char) 0);
            used_components[2] = c.getValue("receiver", (char) 0);
            used_components[3] = c.getValue("MarshallerFactory", (char) 0);
        }
        if (! (used_components[0] instanceof Integer)) {
            used_components[0] = new Integer(Integer.MAX_VALUE);
        }
        if (! (used_components[1] instanceof SecuritySender)) {
            used_components[1] = null;
        }
        if (! (used_components[2] instanceof SecurityReceiver)) {
            used_components[2] = null;
        }
        if (! (used_components[3] instanceof MarshallerFactory)) {
            throw new InternalException("Component MarshallerFactory of type MarshallerFactory is required by SSHandler.");
        }
        return used_components;
    }
   
    /** 
     * Returns a new SSHandler instance created using the provided 
     * components.
     *
     * @param c a context; 
     * @param components components to be used to create a new SSHandler instance;
     * @return a new SSHandler instance;
     * @exception JonathanException if something goes wrong.
     */
    protected Object newInstance(Context c, Object[] components) throws JonathanException {
        return new SSHandler(c, components);
    }
}
