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.TransactionManager ; 28 import org.jboss.iiop.CorbaORB; 29 import org.jboss.iiop.CorbaORBService; 30 import org.jboss.logging.Logger; 31 import org.jboss.tm.TransactionImpl; 32 import org.jboss.tm.TxUtils; 33 import org.jboss.util.NestedRuntimeException; 34 import org.omg.CORBA.Any ; 35 import org.omg.CORBA.LocalObject ; 36 import org.omg.CORBA_2_3.ORB ; 37 import org.omg.CosTransactions.PropagationContext; 38 import org.omg.CosTransactions.PropagationContextHelper; 39 import org.omg.CosTransactions.TransIdentity; 40 import org.omg.CosTransactions.otid_t; 41 import org.omg.IOP.Codec ; 42 import org.omg.IOP.ServiceContext ; 43 import org.omg.IOP.TransactionService ; 44 import org.omg.IOP.CodecPackage.InvalidTypeForEncoding ; 45 import org.omg.PortableInterceptor.ClientRequestInfo ; 46 import org.omg.PortableInterceptor.ClientRequestInterceptor ; 47 48 57 public class TxServerClientInterceptor extends LocalObject implements ClientRequestInterceptor 58 { 59 60 static final long serialVersionUID = 4716203472714459196L; 61 62 64 private static final Logger log = 65 Logger.getLogger(TxServerClientInterceptor.class); 66 private static final boolean traceEnabled = log.isTraceEnabled(); 67 68 private static final int txContextId = TransactionService.value; 69 private static Codec codec; 70 private static TransactionManager tm; 71 private static PropagationContext emptyPC; 72 73 75 static void init(Codec codec) 76 { 77 TxServerClientInterceptor.codec = codec; 78 } 79 80 static TransactionManager getTransactionManager() 81 { 82 if (tm == null) 83 { 84 try 85 { 86 Context ctx = new InitialContext (); 87 tm = (TransactionManager )ctx.lookup("java:/TransactionManager"); 88 } 89 catch (NamingException e) 90 { 91 throw new NestedRuntimeException("java:/TransactionManager lookup failed", e); 92 } 93 } 94 return tm; 95 } 96 97 static PropagationContext getEmptyPropagationContext() 98 { 99 if (emptyPC == null) 100 { 101 emptyPC = new PropagationContext(); 104 emptyPC.parents = new TransIdentity[0]; 105 emptyPC.current = new TransIdentity(); 106 emptyPC.current.otid = new otid_t(); 107 emptyPC.current.otid.formatID = 666; 108 emptyPC.current.otid.bqual_length = 1; 109 emptyPC.current.otid.tid = new byte[] { (byte) 1 }; 110 emptyPC.implementation_specific_data = ORB.init().create_any(); 111 emptyPC.implementation_specific_data.insert_boolean(false); 112 } 113 return emptyPC; 114 } 115 116 118 public TxServerClientInterceptor() 119 { 120 } 122 123 125 public String name() 126 { 127 return "TxServerClientInterceptor"; 128 } 129 130 public void destroy() 131 { 132 } 134 135 137 public void send_request(ClientRequestInfo ri) 138 { 139 if (traceEnabled) 140 log.trace("Intercepting send_request, operation: " + ri.operation()); 141 try 142 { 143 Any any = getTransactionPropagationContextAny(); 144 if (any != null) 145 { 146 ServiceContext sc = new ServiceContext (txContextId, codec.encode_value(any)); 147 ri.add_request_service_context(sc, true ); 148 } 149 } 150 catch (InvalidTypeForEncoding e) 151 { 152 throw new NestedRuntimeException(e); 153 } 154 } 155 156 public void send_poll(ClientRequestInfo ri) 157 { 158 } 160 161 public void receive_reply(ClientRequestInfo ri) 162 { 163 } 165 166 public void receive_exception(ClientRequestInfo ri) 167 { 168 } 170 171 public void receive_other(ClientRequestInfo ri) 172 { 173 } 175 176 protected Any getTransactionPropagationContextAny() 177 { 178 try 179 { 180 PropagationContext pc = null; 181 TransactionManager tm = getTransactionManager(); 182 TransactionImpl tx = (TransactionImpl) tm.getTransaction(); 183 if (!TxUtils.isUncommitted(tx)) 184 { 185 if (traceEnabled) 186 log.trace("No transaction context"); 187 return null; 188 } 189 190 if (CorbaORBService.getOTSContextPropagationEnabledFlag() == true) 191 { 192 if (traceEnabled) 193 log.trace("Propagating actual OTS context"); 194 pc = (PropagationContext) tx.getOTSPropagationContext(); 195 } 196 else 197 { 198 if (traceEnabled) 199 log.trace("Propagating empty OTS context"); 200 pc = getEmptyPropagationContext(); 201 } 202 203 Any any = CorbaORB.getInstance().create_any(); 204 PropagationContextHelper.insert(any, pc); 205 return any; 206 } 207 catch (Exception e) 208 { 209 throw new NestedRuntimeException("Error getting tpc", e); 210 } 211 } 212 } 213 | Popular Tags |