/*
 * 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 org.objectweb.jonas_tm.Current;
import org.objectweb.jonas_tm.TransactionContext;
import org.objectweb.jonas.common.Log;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;

/**
 * @author Philippe Durieux
 * Contributor(s): 
 */
public class JonasTransactionSender {

    static private Logger logger = null;

    public JonasTransactionSender() {
        logger = Log.getLogger(Log.JONAS_PROPCTX_PREFIX);
    }

    /**
     * Sender is sending a request.
     * Return the propagation context to the Orb.
     */
    public TransactionContext sending_request() {
	// Get the Propagation Context
	TransactionContext pctx = null;
	Current ut = Current.getCurrent();
	if (ut != null) {
	    logger.log(BasicLevel.DEBUG, "");
	    pctx = ut.getPropagationContext(true);
	} else {
	    logger.log(BasicLevel.DEBUG, "no current");
	}
	return pctx;
    }

    /**
     * Sender is receiving a reply with an attached propagation context.
     * WARNING: In case of Jeremie orb, we are not yet in the thread that made the send.
     */
    public void received_reply(TransactionContext pctx) {
	Current ut = Current.getCurrent();
	if (ut == null) {
	    logger.log(BasicLevel.DEBUG, "no current -> forget.");
	    return;
	}
	if (pctx != null) {
	    logger.log(BasicLevel.DEBUG, "");
	    // updates the propagation context for this transaction.
	    ut.setPropagationContext(pctx, true);
	} else {
	    logger.log(BasicLevel.DEBUG, "pcontext=null");
	}
    }
   
}
