package org.jboss.tm.iiop;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.Transaction;
import org.omg.CORBA.Any;
import org.omg.CORBA.BAD_PARAM;
import org.omg.CORBA.TCKind;
import org.omg.CORBA.LocalObject;
import org.omg.CosTransactions.PropagationContext;
import org.omg.CosTransactions.PropagationContextHelper;
import org.omg.CosTransactions.otid_t;
import org.omg.IOP.Codec;
import org.omg.IOP.CodecPackage.FormatMismatch;
import org.omg.IOP.CodecPackage.TypeMismatch;
import org.omg.IOP.ServiceContext;
import org.omg.PortableInterceptor.InvalidSlot;
import org.omg.PortableInterceptor.ServerRequestInfo;
import org.omg.PortableInterceptor.ServerRequestInterceptor;
import org.jboss.proxy.ejb.ForeignTransaction;
import org.jboss.tm.GlobalId;
import org.jboss.tm.TransactionPropagationContextImporter;
public class TxServerInterceptor
extends LocalObject
implements ServerRequestInterceptor
{
static final long serialVersionUID = 7474707114565659371L;
private static final int txContextId = org.omg.IOP.TransactionService.value;
private static int slotId;
private static Codec codec;
private static org.omg.PortableInterceptor.Current piCurrent = null;
private static TransactionPropagationContextImporter tpcImporter = null;
static void init(int slotId, Codec codec,
org.omg.PortableInterceptor.Current piCurrent)
{
TxServerInterceptor.slotId = slotId;
TxServerInterceptor.codec = codec;
TxServerInterceptor.piCurrent = piCurrent;
}
public static Transaction getCurrentTransaction()
{
Transaction tx = null;
if (piCurrent != null)
{
try
{
Any any = piCurrent.get_slot(slotId);
if (any.type().kind().value() != TCKind._tk_null)
{
PropagationContext pc = PropagationContextHelper.extract(any);
if (pc.current.coord == null)
tx = ForeignTransaction.instance;
otid_t otid = pc.current.otid;
GlobalId globalId =
new GlobalId(otid.formatID, otid.bqual_length, otid.tid);
tx = getTPCImporter().importTransactionPropagationContext(globalId);
if (tx == null)
tx = ForeignTransaction.instance;
}
}
catch (InvalidSlot e)
{
throw new RuntimeException("Exception getting slot in " +
"TxServerInterceptor: " + e);
}
}
return tx;
}
private static TransactionPropagationContextImporter getTPCImporter()
{
if (tpcImporter == null)
{
try
{
Context ctx = new InitialContext();
tpcImporter = (TransactionPropagationContextImporter)ctx.lookup(
"java:/TransactionPropagationContextImporter");
}
catch (NamingException e)
{
throw new RuntimeException(
"java:/TransactionPropagationContextImporter lookup failed",
e);
}
}
return tpcImporter;
}
public TxServerInterceptor()
{
}
public String name()
{
return "TxServerInterceptor";
}
public void destroy()
{
}
public void receive_request_service_contexts(ServerRequestInfo ri)
{
try
{
ServiceContext sc = ri.get_request_service_context(txContextId);
Any any = codec.decode_value(sc.context_data,
PropagationContextHelper.type());
ri.set_slot(slotId, any);
}
catch (BAD_PARAM e)
{
}
catch (FormatMismatch e)
{
throw new RuntimeException("Exception decoding context data in " +
"TxServerInterceptor: " + e);
}
catch (TypeMismatch e)
{
throw new RuntimeException("Exception decoding context data in " +
"TxServerInterceptor: " + e);
}
catch (InvalidSlot e)
{
throw new RuntimeException("Exception setting slot in " +
"TxServerInterceptor: " + e);
}
}
public void receive_request(ServerRequestInfo ri)
{
}
public void send_reply(ServerRequestInfo ri)
{
}
public void send_exception(ServerRequestInfo ri)
{
}
public void send_other(ServerRequestInfo ri)
{
}
}