1 23 24 28 29 package com.sun.jts.pi; 30 31 import java.lang.Object ; 32 import com.sun.enterprise.util.collection.ArrayListStack; 35 37 import org.omg.IOP.Codec ; 38 import org.omg.IOP.ServiceContext ; 39 import org.omg.IOP.TaggedComponent ; 40 import org.omg.IOP.CodecPackage.*; 41 42 import com.sun.corba.ee.spi.presentation.rmi.StubAdapter; 43 import org.omg.CORBA.portable.InputStream ; 44 import org.omg.PortableInterceptor.Current ; 45 46 import org.omg.CORBA.*; 47 import org.omg.CosTransactions.*; 48 import org.omg.PortableInterceptor.*; 49 50 import org.omg.CosTSPortability.Sender; 51 import org.omg.CosTSPortability.Receiver; 52 53 import org.omg.CosTSInteroperation.TAG_OTS_POLICY; 54 55 import com.sun.jts.CosTransactions.CurrentTransaction; 56 import com.sun.corba.ee.impl.corba.EnvironmentImpl; 57 import com.sun.corba.ee.impl.txpoa.TSIdentificationImpl; 58 59 import java.util.logging.Logger ; 60 import java.util.logging.Level ; 61 import com.sun.logging.LogDomains; 62 63 70 public class InterceptorImpl extends org.omg.CORBA.LocalObject 71 implements ClientRequestInterceptor, ServerRequestInterceptor { 72 73 75 private static final String name = "com.sun.jts.pi.Interceptor"; 76 private static final int TransactionServiceId = 0; 77 private static final SystemException SYS_EXC = 78 new SystemException("", 0, CompletionStatus.COMPLETED_MAYBE) {}; 79 80 public static final int NO_REPLY_SLOT = 0; 81 public static final int NULL_CTX_SLOT = 1; 82 83 public static final Integer PROPER_CTX = new Integer (0); 84 public static final Integer NULL_CTX = new Integer (1); 85 86 public static final Integer REPLY = new Integer (0); 87 public static final Integer NO_REPLY = new Integer (1); 88 89 public static final String CLIENT_POLICY_CHECKING = 90 "com.sun.jts.pi.CLIENT_POLICY_CHECKING"; 91 public static final String INTEROP_MODE = "com.sun.jts.pi.INTEROP_MODE"; 92 93 96 static Logger _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER); 97 98 public static ThreadLocal otsThreadLocal = 99 new ThreadLocal () { 100 protected java.lang.Object initialValue() { 101 Object [] threadLocalState = new Object [2]; 102 threadLocalState[NO_REPLY_SLOT] = new ArrayListStack(); 106 threadLocalState[NULL_CTX_SLOT] = new ArrayListStack(); 107 return threadLocalState; 109 } 110 }; 111 112 private static PropagationContext nullContext, dummyContext = null; 113 114 116 static { 117 Any any = ORB.init().create_any(); 118 any.insert_boolean(false); 119 nullContext = new PropagationContext( 120 0, 121 new TransIdentity(null, null, new otid_t(0, 0, new byte[0])), 122 new TransIdentity[0], 123 any); 124 125 any.insert_boolean(true); 126 dummyContext = new PropagationContext( 127 -1, 128 new TransIdentity(null, null, new otid_t(-1, 0, new byte[0])), 129 new TransIdentity[0], 130 any); 131 } 132 133 public static ORB txOrb = null; 134 135 137 Current pic = null; 138 Codec codec = null; 139 int[] slotIds = null; 140 TSIdentification tsi = null; 141 TSIdentificationImpl tsiImpl = null; 142 Sender sender = null; 143 Receiver receiver = null; 144 private boolean checkPolicy = true; 145 private boolean interopMode = true; 146 147 149 public InterceptorImpl(Current pic, Codec codec, int[] slotIds, 150 TSIdentification tsi) { 151 this.pic = pic; 152 this.codec = codec; 153 this.slotIds = slotIds; 154 this.tsi = tsi; 155 this.tsiImpl = (TSIdentificationImpl) tsi; 156 if (this.tsiImpl != null) { 157 this.sender = this.tsiImpl.getSender(); 158 this.receiver = this.tsiImpl.getReceiver(); 159 } 160 161 String prop = System.getProperty(CLIENT_POLICY_CHECKING, "true"); 164 this.checkPolicy = prop.equals("true"); 165 166 prop = System.getProperty(INTEROP_MODE, "true"); 168 this.interopMode = prop.equals("true"); 169 170 if (_logger.isLoggable(Level.FINE)) 171 _logger.log(Level.FINE, "Transaction INTEROP Mode: " + this.interopMode); 172 173 } 174 175 178 public void setTSIdentification(TSIdentification tsi) { 179 180 if (tsi == null) { 181 return; 182 } 183 184 this.tsi = tsi; 185 this.tsiImpl = (TSIdentificationImpl) tsi; 186 this.sender = this.tsiImpl.getSender(); 187 this.receiver = this.tsiImpl.getReceiver(); 188 } 189 190 192 public String name() { 193 return InterceptorImpl.name; 194 } 195 196 public void destroy() {} 197 198 200 public void send_request(ClientRequestInfo ri) throws ForwardRequest { 201 202 204 TaggedComponent otsComp = null; 205 try { 206 otsComp = ri.get_effective_component(TAG_OTS_POLICY.value); 207 } catch (BAD_PARAM e) { 208 } 210 211 short otsPolicyValue = -1; 212 213 if (otsComp == null) { 214 otsPolicyValue = OTSPolicyImpl._ADAPTS.value(); 223 } else { 224 TypeCode typeCode = ORB.init().get_primitive_tc(TCKind.tk_short); 225 Any any = null; 226 try { 227 any = this.codec.decode_value(otsComp.component_data, typeCode); 228 } catch (TypeMismatch e) { 229 throw new INTERNAL(); 230 } catch (FormatMismatch e) { 231 throw new INTERNAL(); 232 } 233 otsPolicyValue = OTSPolicyValueHelper.extract(any); 234 } 235 236 238 if (this.tsiImpl == null || this.sender == null) { 239 if (otsPolicyValue == REQUIRES.value && this.checkPolicy) { 240 throw new TRANSACTION_UNAVAILABLE(); 241 } 242 return; 243 } 244 245 247 259 260 boolean isTxAssociated = CurrentTransaction.isTxAssociated(); 262 if (!isTxAssociated) { if (otsPolicyValue == REQUIRES.value && this.checkPolicy) { 264 throw new TRANSACTION_REQUIRED(); 265 } 266 return; 267 } 268 269 if (_logger.isLoggable(Level.FINE)) { 270 _logger.log(Level.FINE," sending_request["+ ri.request_id() + 271 "] : " + ri.operation() + ", ThreadName : " + 272 Thread.currentThread().toString()); 273 } 274 275 277 if (otsPolicyValue == FORBIDS.value && this.checkPolicy) { 278 throw new INVALID_TRANSACTION(); 279 } 280 281 PropagationContextHolder hctx = new PropagationContextHolder(); 282 283 Object target = ri.effective_target(); 293 if ( StubAdapter.isStub(target) && StubAdapter.isLocal(target) ) { 294 hctx.value = dummyContext; 297 } else if (this.interopMode == false) { hctx.value = nullContext; 300 } else { 301 sender.sending_request(ri.request_id(), hctx); 303 } 304 305 307 Any any = txOrb.create_any(); 309 PropagationContextHelper.insert(any, hctx.value); 310 byte[] ctxData = null; 311 try { 312 ctxData = this.codec.encode_value(any); 313 } catch (InvalidTypeForEncoding e) { 314 throw new INTERNAL(); 315 } 316 317 ServiceContext svc = new ServiceContext (TransactionServiceId, ctxData); 318 ri.add_request_service_context(svc, false); 319 } 320 321 public void send_poll(ClientRequestInfo ri) { 322 } 324 325 public void receive_reply(ClientRequestInfo ri) { 326 327 329 ServiceContext svc = null; 330 try { 331 svc = ri.get_reply_service_context(TransactionServiceId); 332 } catch (BAD_PARAM e) { 333 return; } 338 339 341 if (this.tsiImpl == null || this.sender == null) { 343 throw new TRANSACTION_ROLLEDBACK(0, CompletionStatus.COMPLETED_YES); 344 } 345 346 if (_logger.isLoggable(Level.FINE)) { 347 _logger.log(Level.FINE," received_reply[" + ri.request_id() + "] : " + 348 ri.operation() + ", ThreadName : " + 349 Thread.currentThread().toString()); 350 } 351 352 354 Any any = null; 355 try { 356 TypeCode typeCode = PropagationContextHelper.type(); 357 any = this.codec.decode_value(svc.context_data, typeCode); 358 } catch (TypeMismatch e) { 359 throw new INTERNAL(0, CompletionStatus.COMPLETED_YES); 360 } catch (FormatMismatch e) { 361 throw new INTERNAL(0, CompletionStatus.COMPLETED_YES); 362 } 363 PropagationContext ctx = PropagationContextHelper.extract(any); 364 365 368 Environment env = new EnvironmentImpl(); 369 env.exception(null); 370 371 373 try { 374 sender.received_reply(ri.request_id(), ctx, env); 375 } catch (org.omg.CORBA.WrongTransaction ex) { 376 throw new INVALID_TRANSACTION(0, CompletionStatus.COMPLETED_YES); 377 } 378 } 379 380 public void receive_exception(ClientRequestInfo ri) throws ForwardRequest { 381 382 384 ServiceContext svc = null; 385 try { 386 svc = ri.get_reply_service_context(TransactionServiceId); 387 } catch (BAD_PARAM e) { 388 return; } 393 catch(Exception e){ 394 return; 395 } 396 397 399 Environment env = new com.sun.corba.ee.impl.corba.EnvironmentImpl(); 402 SystemException exception = null; 403 Any any = ri.received_exception(); 404 InputStream strm = any.create_input_stream(); 405 String repId = ri.received_exception_id(); 406 strm.read_string(); int minorCode = strm.read_long(); CompletionStatus completionStatus = CompletionStatus.from_int(strm.read_long()); 410 if (repId.indexOf("UNKNOWN") != -1) { if (minorCode == 1) { } else { exception = SYS_EXC; 415 } 416 } else { exception = SYS_EXC; 418 } 419 env.exception(exception); 420 421 if (this.tsiImpl == null || this.sender == null) { 423 throw new TRANSACTION_ROLLEDBACK(0, completionStatus); 424 } 425 426 428 try { 429 TypeCode typeCode = PropagationContextHelper.type(); 430 any = this.codec.decode_value(svc.context_data, typeCode); 431 } catch (TypeMismatch e) { 432 throw new INTERNAL(0, completionStatus); 433 } catch (FormatMismatch e) { 434 throw new INTERNAL(0, completionStatus); 435 } 436 PropagationContext ctx = PropagationContextHelper.extract(any); 437 438 440 try { 441 sender.received_reply(ri.request_id(), ctx, env); 442 } catch (org.omg.CORBA.WrongTransaction ex) { 443 throw new INVALID_TRANSACTION(0, completionStatus); 444 } 445 } 446 447 public void receive_other(ClientRequestInfo ri) throws ForwardRequest { 448 449 451 ServiceContext svc = null; 452 try { 453 svc = ri.get_reply_service_context(TransactionServiceId); 454 } catch (BAD_PARAM e) { 455 return; } 460 461 463 if (this.tsiImpl == null || this.sender == null) { 465 throw new TRANSACTION_ROLLEDBACK(0, CompletionStatus.COMPLETED_NO); 466 } 467 468 470 Any any = null; 471 try { 472 TypeCode typeCode = PropagationContextHelper.type(); 473 any = this.codec.decode_value(svc.context_data, typeCode); 474 } catch (TypeMismatch e) { 475 throw new INTERNAL(0, CompletionStatus.COMPLETED_NO); 476 } catch (FormatMismatch e) { 477 throw new INTERNAL(0, CompletionStatus.COMPLETED_NO); 478 } 479 PropagationContext ctx = PropagationContextHelper.extract(any); 480 481 484 Environment env = new EnvironmentImpl(); 485 env.exception(null); 486 487 489 try { 490 sender.received_reply(ri.request_id(), ctx, env); 491 } catch (org.omg.CORBA.WrongTransaction ex) { 492 throw new INVALID_TRANSACTION(0, CompletionStatus.COMPLETED_NO); 493 } 494 } 495 496 498 public void receive_request_service_contexts(ServerRequestInfo ri) 499 throws ForwardRequest { 500 501 507 OTSPolicy otsPolicy = null; 508 try { 509 otsPolicy = (OTSPolicy) ri.get_server_policy(OTS_POLICY_TYPE.value); 510 } catch (INV_POLICY e) { 511 } 513 514 short otsPolicyValue = -1; 515 516 if (otsPolicy == null) { 517 otsPolicyValue = OTSPolicyImpl._ADAPTS.value(); 521 } else { 522 otsPolicyValue = otsPolicy.value(); 523 } 524 525 527 ServiceContext svc = null; 528 try { 529 svc = ri.get_request_service_context(TransactionServiceId); 530 } catch (BAD_PARAM e) { 531 } 533 534 537 if (svc == null) { 538 setThreadLocalData(NO_REPLY_SLOT, NO_REPLY); 539 } else { 540 setThreadLocalData(NO_REPLY_SLOT, REPLY); 541 } 542 543 setThreadLocalData(NULL_CTX_SLOT, PROPER_CTX); 546 547 try { 548 549 551 if (this.tsiImpl == null || this.receiver == null) { 552 if (svc != null || otsPolicyValue == REQUIRES.value) { 553 throw new TRANSACTION_UNAVAILABLE(); 554 } 555 return; 556 } 557 558 560 if (svc == null) { 562 if (otsPolicyValue == REQUIRES.value) { 563 throw new TRANSACTION_REQUIRED(); 564 } 565 return; 566 } 567 568 570 if (otsPolicyValue == FORBIDS.value) { 572 throw new INVALID_TRANSACTION(); 573 } 574 575 if (_logger.isLoggable(Level.FINE)) { 576 _logger.log(Level.FINE," received_request[" + 577 ri.request_id() + "] : " + ri.operation() + 578 ", ThreadName : " + Thread.currentThread().toString()); 579 } 580 581 583 if (svc.context_id != TransactionServiceId) { 585 throw new INVALID_TRANSACTION(); 586 } 587 588 Any any = null; 589 try { 590 TypeCode typeCode = PropagationContextHelper.type(); 591 any = this.codec.decode_value(svc.context_data, typeCode); 592 } catch (TypeMismatch e) { 593 throw new INTERNAL(); 594 } catch (FormatMismatch e) { 595 throw new INTERNAL(); 596 } 597 PropagationContext ctx = PropagationContextHelper.extract(any); 598 599 602 if (isDummyContext(ctx)) { 603 609 getThreadLocalData(NO_REPLY_SLOT); setThreadLocalData(NO_REPLY_SLOT, NO_REPLY); return; 613 } 614 615 618 if (isNullContext(ctx)) { 619 getThreadLocalData(NULL_CTX_SLOT); setThreadLocalData(NULL_CTX_SLOT, NULL_CTX); getThreadLocalData(NO_REPLY_SLOT); setThreadLocalData(NO_REPLY_SLOT, NO_REPLY); return; 626 } else if (this.interopMode == false) { 627 getThreadLocalData(NULL_CTX_SLOT); setThreadLocalData(NULL_CTX_SLOT, NULL_CTX); } 630 631 receiver.received_request(ri.request_id(), ctx); 633 634 } catch (RuntimeException r) { 635 getThreadLocalData(NO_REPLY_SLOT); 639 getThreadLocalData(NULL_CTX_SLOT); 640 throw r; 641 } 642 } 643 644 public void receive_request(ServerRequestInfo ri) 645 throws ForwardRequest { 646 } 648 649 private void processServerSendPoint( 650 ServerRequestInfo ri, CompletionStatus completionStatus) { 651 652 getThreadLocalData(NULL_CTX_SLOT); 654 655 657 Integer no_reply = getThreadLocalData(NO_REPLY_SLOT); 658 659 if (no_reply == NO_REPLY) { 660 return; 661 } 662 663 665 if (this.tsiImpl == null || this.receiver == null) { 666 if (no_reply == REPLY) { 667 throw new TRANSACTION_ROLLEDBACK(0, completionStatus); 670 } 671 return; 672 } 673 674 if (_logger.isLoggable(Level.FINE)) { 675 _logger.log(Level.FINE," sending_reply["+ ri.request_id() + "] : " + 676 ri.operation() + ", ThreadName : " + 677 Thread.currentThread().toString()); 678 } 679 680 682 PropagationContextHolder ctxh = new PropagationContextHolder(); 683 receiver.sending_reply(ri.request_id(), ctxh); 684 685 if (ctxh.value == null) { 686 throw new TRANSACTION_ROLLEDBACK(0, completionStatus); 689 } 690 691 693 Any any = txOrb.create_any(); 695 PropagationContextHelper.insert(any, ctxh.value); 696 byte[] ctxData = null; 697 try { 698 ctxData = this.codec.encode_value(any); 699 } catch (InvalidTypeForEncoding e) { 700 throw new INTERNAL(0, completionStatus); 701 } 702 ServiceContext svc = new ServiceContext (TransactionServiceId, ctxData); 703 ri.add_reply_service_context(svc, false); 704 } 705 706 public void send_reply(ServerRequestInfo ri) { 707 processServerSendPoint(ri, CompletionStatus.COMPLETED_YES); 708 } 709 710 public void send_exception(ServerRequestInfo ri) throws ForwardRequest { 711 Any any = ri.sending_exception(); 712 InputStream strm = any.create_input_stream(); 713 strm.read_string(); strm.read_long(); CompletionStatus completionStatus = 716 CompletionStatus.from_int(strm.read_long()); 717 718 processServerSendPoint(ri, completionStatus); 719 } 720 721 public void send_other(ServerRequestInfo ri) throws ForwardRequest { 722 processServerSendPoint(ri, CompletionStatus.COMPLETED_NO); 723 } 724 725 727 public static boolean isTxCtxtNull() { 728 Object [] threadLocalState = (Object []) otsThreadLocal.get(); 729 736 ArrayListStack stack = (ArrayListStack) threadLocalState[NULL_CTX_SLOT]; 738 return ((Integer ) stack.peek() == NULL_CTX); 739 } 741 742 public static boolean isNullContext(PropagationContext ctx) { 743 return (ctx.current.coord == null && ctx.current.term == null); 744 } 745 746 public static boolean isDummyContext(PropagationContext ctx) { 747 boolean proceed = false; 748 try { 749 proceed = 750 (ctx.implementation_specific_data.extract_boolean() == true); 751 } catch (BAD_OPERATION e) { 752 return false; 753 } 754 return (proceed && isNullContext(ctx) && ctx.timeout == -1); 755 } 756 757 public static void setThreadLocalData(int slot, Integer data) { 758 Object [] threadLocalState = (Object []) otsThreadLocal.get(); 759 ((ArrayListStack) threadLocalState[slot]).push(data); 762 } 764 765 public static Integer getThreadLocalData(int slot) { 766 Object [] threadLocalState = (Object []) otsThreadLocal.get(); 767 return (Integer ) ((ArrayListStack) threadLocalState[slot]).pop(); 770 } 772 773 public static void setOrb(ORB orb) { 774 txOrb = orb; 775 } 776 } 777 | Popular Tags |