1 22 package org.jboss.tm.iiop; 23 24 import javax.naming.Context ; 25 import javax.naming.InitialContext ; 26 import javax.naming.NamingException ; 27 import javax.transaction.Transaction ; 28 29 import org.omg.CORBA.Any ; 30 import org.omg.CORBA.BAD_PARAM ; 31 import org.omg.CORBA.TCKind ; 32 import org.omg.CORBA.LocalObject ; 33 import org.omg.CosTransactions.PropagationContext; 34 import org.omg.CosTransactions.PropagationContextHelper; 35 import org.omg.CosTransactions.otid_t; 36 import org.omg.IOP.Codec ; 37 import org.omg.IOP.CodecPackage.FormatMismatch ; 38 import org.omg.IOP.CodecPackage.TypeMismatch ; 39 import org.omg.IOP.ServiceContext ; 40 import org.omg.PortableInterceptor.InvalidSlot ; 41 import org.omg.PortableInterceptor.ServerRequestInfo ; 42 import org.omg.PortableInterceptor.ServerRequestInterceptor ; 43 44 import org.jboss.logging.Logger; 45 import org.jboss.proxy.ejb.ForeignTransaction; 46 import org.jboss.tm.TransactionPropagationContextImporter; 47 import org.jboss.tm.iiop.wrapper.OTSCoordinatorWrapper; 48 import org.jboss.tm.remoting.interfaces.Coordinator; 49 import org.jboss.tm.remoting.interfaces.TxPropagationContext; 50 51 61 public class TxServerInterceptor 62 extends LocalObject 63 implements ServerRequestInterceptor 64 { 65 66 static final long serialVersionUID = 7474707114565659371L; 67 68 70 private static final Logger log = 71 Logger.getLogger(TxServerInterceptor.class); 72 private static final boolean traceEnabled = log.isTraceEnabled(); 73 74 private static final int txContextId = org.omg.IOP.TransactionService.value; 75 private static int slotId; 76 private static Codec codec; 77 private static org.omg.PortableInterceptor.Current piCurrent = null; 78 private static TransactionPropagationContextImporter tpcImporter = null; 79 80 82 86 static void init(int slotId, Codec codec, 87 org.omg.PortableInterceptor.Current piCurrent) 88 { 89 TxServerInterceptor.slotId = slotId; 90 TxServerInterceptor.codec = codec; 91 TxServerInterceptor.piCurrent = piCurrent; 92 } 93 94 98 public static Transaction getCurrentTransaction() 99 { 100 Transaction tx = null; 101 if (piCurrent != null) 102 { 103 try 106 { 107 Any any = piCurrent.get_slot(slotId); 108 if (any.type().kind().value() != TCKind._tk_null) 109 { 110 PropagationContext pc = PropagationContextHelper.extract(any); 112 if (pc.current.coord != null) 113 { 114 117 byte[] globalId; 119 otid_t otid = pc.current.otid; 120 121 if (otid.bqual_length == 0) 122 globalId = otid.tid; 123 else 124 { 125 int len = otid.tid.length - otid.bqual_length; 127 globalId = new byte[len]; 128 System.arraycopy(otid.tid, 0, globalId, 0, len); 129 } 130 131 Coordinator coordinatorWrapper = 133 new OTSCoordinatorWrapper(pc.current.coord); 134 135 TxPropagationContext tpc = 137 new TxPropagationContext(otid.formatID, 138 globalId, 139 pc.timeout, 140 coordinatorWrapper, 141 null); 142 143 tx = getTPCImporter().importTransactionPropagationContext(tpc); 145 } 146 147 if (tx == null) 149 tx = ForeignTransaction.instance; 150 } 151 } 152 catch (InvalidSlot e) 153 { 154 throw new RuntimeException ("Exception getting slot in " + 155 "TxServerInterceptor: " + e); 156 } 157 158 } 159 return tx; 160 } 161 162 165 private static TransactionPropagationContextImporter getTPCImporter() 166 { 167 if (tpcImporter == null) 168 { 169 try 170 { 171 Context ctx = new InitialContext (); 172 tpcImporter = (TransactionPropagationContextImporter)ctx.lookup( 173 "java:/TransactionPropagationContextImporter"); 174 } 175 catch (NamingException e) 176 { 177 throw new RuntimeException ( 178 "java:/TransactionPropagationContextImporter lookup failed", 179 e); 180 } 181 } 182 return tpcImporter; 183 } 184 185 187 public TxServerInterceptor() 188 { 189 } 191 192 194 public String name() 195 { 196 return "TxServerInterceptor"; 197 } 198 199 public void destroy() 200 { 201 } 203 204 206 public void receive_request_service_contexts(ServerRequestInfo ri) 207 { 208 if (traceEnabled) 209 log.trace("Intercepting receive_request_service_contexts, " + 210 "operation: " + ri.operation()); 211 try 212 { 213 ServiceContext sc = ri.get_request_service_context(txContextId); 214 Any any = codec.decode_value(sc.context_data, 215 PropagationContextHelper.type()); 216 ri.set_slot(slotId, any); 217 } 218 catch (BAD_PARAM e) 219 { 220 } 222 catch (FormatMismatch e) 223 { 224 throw new RuntimeException ("Exception decoding context data in " + 225 "TxServerInterceptor: " + e); 226 } 227 catch (TypeMismatch e) 228 { 229 throw new RuntimeException ("Exception decoding context data in " + 230 "TxServerInterceptor: " + e); 231 } 232 catch (InvalidSlot e) 233 { 234 throw new RuntimeException ("Exception setting slot in " + 235 "TxServerInterceptor: " + e); 236 } 237 } 238 239 public void receive_request(ServerRequestInfo ri) 240 { 241 } 243 244 public void send_reply(ServerRequestInfo ri) 245 { 246 } 248 249 public void send_exception(ServerRequestInfo ri) 250 { 251 } 253 254 public void send_other(ServerRequestInfo ri) 255 { 256 } 258 } 259 | Popular Tags |