/*
 * 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.omg.CosTransactions.PropagationContext;
import org.omg.CosTSPortability.Receiver;
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;

/*
 * JonasReceiver used in case of DAVID orb only.
 * - based on CORBA interfaces.
 * - contains the glue to map CORBA contexts to JONAS contexts.
 * @author Philippe Durieux
 * Contributor(s): 
 */

public class JonasReceiver implements Receiver {
   
    static private Logger logger = null;

    public JonasReceiver() {
        logger = Log.getLogger(Log.JONAS_PROPCTX_PREFIX);
    }
  
    /**
     * Receiver is receiving a request with an attached propagation context.
     */
    public void received_request(int reqId, PropagationContext pctx) {
	// Gets Current object (always existing in EJBServer)
	Current ut = Current.getCurrent();
	if (ut == null) {
	    logger.log(BasicLevel.DEBUG, "no current -> forget.");
	    return;
	}
	// Associates the current thread with this propagation context
	logger.log(BasicLevel.DEBUG, ""+reqId);
	TransactionContext tctx = new TransactionContextCORBA(pctx);
	ut.setPropagationContext(tctx, false);
    }
  

    /** 
     * Receiver is sending a reply.
     * Return the propagation context to the Orb.
     */
    public PropagationContext sending_reply (int reqId) {
	// Gets Current object (always existing in EJBServer)
	PropagationContext pctx = null;
	Current ut = Current.getCurrent();  
	if (ut != null) {
	    TransactionContextCORBA tctx = (TransactionContextCORBA) ut.getPropagationContext(false);
	    if (tctx != null) {
		pctx = tctx.getCorbaPropagationContext();
	    }
	}
	if (pctx != null) {
	    logger.log(BasicLevel.DEBUG, ""+reqId);
	    // Dissociates first the current thread from any propagation context
	    // to make sure the thread will not be kept associated with an old tx.
	    ut.setPropagationContext(null, false);
	} else {
	    logger.log(BasicLevel.DEBUG, " pcontext=null");
	}
	return pctx;
    }
   
}
