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

import org.omg.CORBA.Environment;
import org.omg.CosTransactions.PropagationContext;
import org.omg.CosTSPortability.Sender;
import org.objectweb.jonas_tm.Current;
import org.objectweb.jonas_tm.TransactionContext;
import org.objectweb.jonas_tm.TransactionContextCORBA;
import org.objectweb.jonas.common.Log;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;
/**
 * JonasSender used in case of DAVID orb only.
 * - based on CORBA interfaces.
 * - contains the glue to map CORBA contexts to JONAS contexts.
 * @author Philippe Durieux
 */
public class JonasSender implements Sender {

    static private Logger logger = null;
   
    public JonasSender() {
        logger = Log.getLogger(Log.JONAS_PROPCTX_PREFIX);
    }

    /**
     * Sender is sending a request.
     * Return a CORBA propagation context to the Orb.
     */
    public PropagationContext sending_request(int reqId) {
	// Get the Propagation Context
	PropagationContext pctx = null;
	Current ut = Current.getCurrent();
	if (ut != null) {
	    logger.log(BasicLevel.DEBUG, ""+reqId);
	    TransactionContextCORBA tctx = (TransactionContextCORBA) ut.getPropagationContext(true);
	    if (tctx != null) {
		pctx = tctx.getCorbaPropagationContext();
	    }
	}
	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(int reqId, PropagationContext pctx, Environment env) {
	Current ut = Current.getCurrent();
	if (ut == null) {
	    logger.log(BasicLevel.DEBUG, "for "+reqId+" :  no current -> forget.");
	    return;
	}
	if (pctx != null) {
	    logger.log(BasicLevel.DEBUG, "JonasSender received_reply "+reqId);
	    TransactionContext tctx = new TransactionContextCORBA(pctx);
	    // updates the propagation context for this transaction.
	    ut.setPropagationContext(tctx, true);
	} else {
	    logger.log(BasicLevel.DEBUG, "for "+reqId+" : pcontext=null");
	}
    }
   
}
