/*
 * 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 JonasTransactionReceiver {
   
    static private Logger logger = null;

    public JonasTransactionReceiver() {
        logger = Log.getLogger(Log.JONAS_PROPCTX_PREFIX);
    }
  
    /**
     * Receiver is receiving a request with an attached propagation context.
     */
    public void received_request(TransactionContext pctx) {
	// Gets Current object (always existing in EJBServer)
	Current ut = Current.getCurrent();
	if (ut == null) {
	    logger.log(BasicLevel.DEBUG, "no current -> forget.");
	    return;
	}
	logger.log(BasicLevel.DEBUG, "");
	// Associates the current thread with this propagation context
	ut.setPropagationContext(pctx, false);
    }
  
    /** 
     * Receiver is sending a reply.
     * Return the propagation context to the Orb.
     */
    public TransactionContext sending_reply() {
	// Gets Current object (always existing in EJBServer)
	TransactionContext pctx = null;
	Current ut = Current.getCurrent();  
	if (ut != null) {
	    pctx = ut.getPropagationContext(false);
	} else {
	    logger.log(BasicLevel.DEBUG, "no current");
	}
	if (pctx != null) {
	    logger.log(BasicLevel.DEBUG, "");
	    // 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;
    }
   
}
