1 39 package org.objectweb.jotm.ots; 40 41 import javax.rmi.PortableRemoteObject ; 42 import javax.transaction.xa.Xid ; 43 44 import org.omg.CORBA.Any ; 45 import org.omg.CORBA.LocalObject ; 46 import org.omg.CORBA.ORB ; 47 import org.omg.CORBA.TCKind ; 48 import org.omg.CosTransactions.PropagationContext; 49 import org.omg.CosTransactions.PropagationContextHelper; 50 import org.omg.CosTransactions.TransIdentity; 51 import org.omg.CosTransactions.otid_t; 52 import org.omg.DynamicAny.DynAnyFactory ; 53 import org.omg.DynamicAny.DynAnyFactoryHelper ; 54 import org.omg.IOP.Codec ; 55 import org.omg.IOP.ServiceContext ; 56 import org.omg.PortableInterceptor.ForwardRequest ; 57 import org.omg.PortableInterceptor.ORBInitInfo ; 58 import org.omg.PortableInterceptor.ORBInitInfoPackage.InvalidName ; 59 60 import org.objectweb.jotm.Coordinator; 61 import org.objectweb.jotm.InternalTransactionContext; 62 import org.objectweb.jotm.Terminator; 63 import org.objectweb.jotm.TransactionContext; 64 import org.objectweb.jotm.XidImpl; 65 66 public abstract class OTSInterceptor extends LocalObject { 67 68 protected Codec codec; 70 71 protected DynAnyFactory dynAnyFactoryS_; 73 74 protected final int TX_CTX_ID = org.omg.IOP.TransactionService.value; 77 protected static ORB orb = null ; 79 80 83 84 public OTSInterceptor(ORBInitInfo info) { 85 86 org.omg.IOP.CodecFactory factory = info.codec_factory(); 88 if(factory == null) throw new RuntimeException (); 89 90 org.omg.IOP.Encoding how = new org.omg.IOP.Encoding (); 92 how.major_version = 1; 93 how.minor_version = 0; 94 how.format = org.omg.IOP.ENCODING_CDR_ENCAPS.value; 95 96 try { 97 codec = factory.create_codec(how); 98 } catch(org.omg.IOP.CodecFactoryPackage.UnknownEncoding ex) { 99 throw new RuntimeException (); 100 } 101 102 if(codec == null) throw new RuntimeException (); 103 104 DynAnyFactory dynAnyFactory = null; 108 try { 109 org.omg.CORBA.Object obj = info.resolve_initial_references("DynAnyFactory"); 110 dynAnyFactory = DynAnyFactoryHelper.narrow(obj); 111 } catch(InvalidName ex) { 112 throw new RuntimeException (); 113 } catch(org.omg.CORBA.BAD_PARAM ex) { 114 throw new RuntimeException (); 115 } 116 117 dynAnyFactoryS_ = dynAnyFactory; 118 119 } 121 protected Any create_any() throws org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode { 122 org.omg.DynamicAny.DynAny dynAny = dynAnyFactoryS_.create_dyn_any_from_type_code(PropagationContextHelper.type()); 123 return dynAny.to_any(); 124 } 125 126 129 protected ServiceContext buildCorbaPropagationContext(TransactionContext txCtx) throws ForwardRequest { 130 try { 131 132 138 Xid xid = txCtx.getXid(); 140 int timeout = txCtx.getTimeout(); 141 Coordinator coord = txCtx.getCoordinator(); 142 143 byte[] gtrid = xid.getGlobalTransactionId(); 145 byte[] bqual = xid.getBranchQualifier(); 146 byte[] tid = new byte[gtrid.length + bqual.length]; 147 System.arraycopy(bqual, 0, tid, 0, bqual.length); 148 System.arraycopy(gtrid, 0, tid, bqual.length, gtrid.length); 149 otid_t otid = new otid_t(xid.getFormatId(), bqual.length, tid); 150 151 TransIdentity curr = new TransIdentity(null, null, otid); 153 154 PropagationContext pctx = 156 new PropagationContext(timeout, curr, new TransIdentity[0], null); 157 158 if (orb==null){ 163 orb=ORB.init(new String []{}, null) ; 165 } 166 167 Any specific = orb.create_any(); 168 if (coord != null) { 169 specific.insert_Object((javax.rmi.CORBA.Stub ) PortableRemoteObject.toStub(coord)); 171 pctx.implementation_specific_data = specific; 172 } else { 173 specific.insert_string("JOnAS"); 177 pctx.implementation_specific_data = specific; 178 } 179 180 Any pAny = create_any(); 181 PropagationContextHelper.insert(pAny, pctx); 182 183 byte[] propagationContextData = codec.encode_value(pAny); 185 return new ServiceContext (TX_CTX_ID, propagationContextData); 186 187 } catch (Exception e) { 188 throw new ForwardRequest (); 189 } 190 } 191 192 196 protected TransactionContext decodeCorbaPropagationContext(ServiceContext sCtx){ 197 198 if (sCtx == null) { 200 return null ; 201 } 202 Xid xid = null; 203 int timeout; 204 Any specific = null ; 205 206 try { 208 Any pctxAny = codec.decode_value(sCtx.context_data, PropagationContextHelper.type()); 210 PropagationContext pctx = PropagationContextHelper.extract(pctxAny); 211 212 specific = pctx.implementation_specific_data; 214 otid_t otid = pctx.current.otid; 215 timeout = pctx.timeout; 216 xid = new XidImpl(otid.formatID, otid.bqual_length, otid.tid); 217 218 } catch (Exception e) { 220 return null; 221 } 222 223 boolean isJotmCtx=false; 226 Coordinator coord = null; 227 Terminator term = null; 228 229 try { 230 if ((specific == null) || (specific.type().kind() == TCKind.tk_null) || 231 (specific.type().kind() == TCKind.tk_octet)) { 232 } else if (specific.type().kind() == TCKind.tk_string) { 235 String pattern = specific.extract_string() ; 236 if (pattern.compareTo("JOnAS")==0) { 239 isJotmCtx=true ; 240 } 241 242 } else { 243 coord = (Coordinator) PortableRemoteObject.narrow((java.rmi.Remote ) specific.extract_Object(), 245 Coordinator.class); 246 term = (Terminator ) PortableRemoteObject.narrow((java.rmi.Remote ) specific.extract_Object(), 247 Terminator .class); 248 isJotmCtx=true ; 249 250 251 } 252 } catch (Exception e) { 253 } 256 257 TransactionContext tCtx = new InternalTransactionContext(timeout, coord, term, xid); 258 if ( isJotmCtx == false ) { 259 tCtx.setNotJotmCtx(); 262 } 263 return tCtx; 264 } 265 } 266 267 | Popular Tags |