/*
 * 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
 *
 * --------------------------------------------------------------------------
 * $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 Skeleton class is the common superclass to all server
 *  skeletons generated by the the Bull rmic compiler used for the
 *  implicit transactional context propagation.
 *
 * inRequest() extracts in the ObjectInputStream the Control
 *  object corresponding to the propagated transaction (it may be null).
 *  and attaches the current thread to the transaction.
 * outReply() detaches the association thread/transaction.
 * @author Philippe Coq
 * Contributor(s):
 */
public class Skeleton {

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

    /**
     * inRequest is called before invoking the operation in the implementation
     *  object. 
     * It extracts the transactional context propagated as the last parameter 
     *  of the remote operation then resumes the corresponding transaction 
     *  in the server.
     * It releases the Input Stream associated to the Remote Call object.
     */
    public void inRequest(java.rmi.server.RemoteCall call)
	throws java.lang.ClassNotFoundException, java.rmi.UnmarshalException {

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

        // Extracts TransactionContext from input args
        TransactionContext pc;
        try {
            ObjectInput in = call.getInputStream();
            pc = (TransactionContext) in.readObject();
            call.releaseInputStream();
        } catch (IOException e) {
            throw new UnmarshalException("error unmarshalling arguments ", e);
        }

	// Gets Current object (always existing in EJBServer)
        Current ut = Current.getCurrent();
	if (ut == null) {
	    logger.log(BasicLevel.DEBUG, "No Current in this server!");
	    return;
	}

	// Associates the current thread with this propagation context
        if (pc != null) {
	    logger.log(BasicLevel.DEBUG, "valid transaction context");
        }
	ut.setPropagationContext(pc, false);
    }


    /**
     * outReply is called before return to the caller.
     * The current transaction is suspended.
     */
    public ObjectOutput outReply(java.rmi.server.RemoteCall call) 
	throws java.rmi.MarshalException, java.io.IOException, java.io.StreamCorruptedException {

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

	ObjectOutput out = call.getResultStream(true);

	// Gets Current object (always existing in EJBServer)
        Current ut = Current.getCurrent();
	if (ut == null) {
	    logger.log(BasicLevel.DEBUG, "No Current in this server!");
	    throw new MarshalException("No Current in this server");
	}
	// Get the Propagation Context
	TransactionContext pctx = ut.getPropagationContext(false);

	// Dissociates the current thread from any propagation context
	if (pctx != null) {
	    logger.log(BasicLevel.DEBUG, "passing back a non null Prop.Ctxt to client");
	    ut.setPropagationContext(null, false);
	}

	// Add the propagation context to result stream
	try {
	    out.writeObject(pctx);
	} catch (IOException e) {
	    throw new MarshalException("error marshalling propagation context on reply", e);
	}

	// Used by skeleton to put the returned value, if any.
	return out;
    }
}
