/*
 * 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): ______________________________________.
 * 
*     JOnAS 2.2.x: John Ellis contribution.
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */


package org.objectweb.jonas.rmifilters;

import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.IOException;
import java.rmi.MarshalException;
import java.rmi.UnmarshalException;

import org.objectweb.jonas.common.Log;
import org.objectweb.jonas_tm.Current;
import org.objectweb.jonas_tm.TransactionContext;
import org.objectweb.jonas_tm.TransactionImpl;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;

/**
 * The RemoteStub class is the common superclass to all client stubs
 * generated by the rmic compiler used for the implicit transactional
 * context propagation.
 *
 * invoke() inserts in the ObjectOutputStream the Control object corresponding
 * to the current transaction.
 */

public class SecurityRemoteStub extends java.rmi.server.RemoteStub {

    static protected Logger logger = Log.getLogger(Log.JONAS_PROPCTX_PREFIX);

    /**
     * Default constructor
     */
    public SecurityRemoteStub() {
	super();
    }


    /**
     * Constructor for RemoteStub, with the specified remote reference. 
     */
    public SecurityRemoteStub(java.rmi.server.RemoteRef ref) {
	super(ref);
    }

    /**
     * invoke() inserts the correct transactional context before executing the remote call.
     * A null object means "no transactional context".
     */
    public void invoke(java.rmi.server.RemoteCall call) 
	throws java.lang.Exception, java.rmi.MarshalException {

	logger.log(BasicLevel.DEBUG, "entering");

	// Get the Propagation Context
	TransactionContext pctx = null;
	Current ut = Current.getCurrent(); 
	if (ut != null) {
	    pctx = ut.getPropagationContext(true);
	}
	// Add it to output args
	try {
	    ObjectOutput out = call.getOutputStream();
	    out.writeObject(pctx);
	    RMISecurityPropagationManager.getInstance().sendingRequest(out);
	} catch (IOException e) {
	    throw new MarshalException("error marshalling propagation context", e);
	}

	// Invoke remote method call
	ref.invoke(call);

        // Extracts TransactionContext from input args
        try {
            ObjectInput in = call.getInputStream();
	    pctx = (TransactionContext) in.readObject();
	    RMISecurityPropagationManager.getInstance().receivedReply(in);
	} catch (IOException e) {
	    throw new UnmarshalException("error unmarshalling propagation context", e);
	}	

	// Associates the current thread with this propagation context
        if (pctx != null && ut != null) {
	    ut.setPropagationContext(pctx, true);
        }
    }
}
