1. /*
  2. * @(#)PICurrent.java 1.14 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.impl.interceptors;
  8. import com.sun.corba.se.spi.orb.ORB;
  9. import org.omg.PortableInterceptor.Current;
  10. import org.omg.PortableInterceptor.InvalidSlot;
  11. import org.omg.CORBA.Any;
  12. import org.omg.CORBA.BAD_INV_ORDER;
  13. import org.omg.CORBA.CompletionStatus;
  14. import com.sun.corba.se.spi.logging.CORBALogDomains ;
  15. import com.sun.corba.se.impl.logging.OMGSystemException ;
  16. /**
  17. * PICurrent is the implementation of Current as specified in the Portable
  18. * Interceptors Spec orbos/99-12-02.
  19. * IMPORTANT: PICurrent is implemented with the assumption that get_slot()
  20. * or set_slot() will not be called in ORBInitializer.pre_init() and
  21. * post_init().
  22. */
  23. public class PICurrent extends org.omg.CORBA.LocalObject
  24. implements Current
  25. {
  26. // slotCounter is used to keep track of ORBInitInfo.allocate_slot_id()
  27. private int slotCounter;
  28. // The ORB associated with this PICurrent object.
  29. private ORB myORB;
  30. private OMGSystemException wrapper ;
  31. // True if the orb is still initialzing and get_slot and set_slot are not
  32. // to be called.
  33. private boolean orbInitializing;
  34. // ThreadLocal contains a stack of SlotTable which are used
  35. // for resolve_initial_references( "PICurrent" );
  36. private ThreadLocal threadLocalSlotTable
  37. = new ThreadLocal( ) {
  38. protected Object initialValue( ) {
  39. SlotTable table = new SlotTable( myORB, slotCounter );
  40. return new SlotTableStack( myORB, table );
  41. }
  42. };
  43. /**
  44. * PICurrent constructor which will be called for every ORB
  45. * initialization.
  46. */
  47. PICurrent( ORB myORB ) {
  48. this.myORB = myORB;
  49. wrapper = OMGSystemException.get( myORB,
  50. CORBALogDomains.RPC_PROTOCOL ) ;
  51. this.orbInitializing = true;
  52. slotCounter = 0;
  53. }
  54. /**
  55. * This method will be called from ORBInitInfo.allocate_slot_id( ).
  56. * simply returns a slot id by incrementing slotCounter.
  57. */
  58. int allocateSlotId( ) {
  59. int slotId = slotCounter;
  60. slotCounter = slotCounter + 1;
  61. return slotId;
  62. }
  63. /**
  64. * This method gets the SlotTable which is on the top of the
  65. * ThreadLocalStack.
  66. */
  67. SlotTable getSlotTable( ) {
  68. SlotTable table = (SlotTable)
  69. ((SlotTableStack)threadLocalSlotTable.get()).peekSlotTable();
  70. return table;
  71. }
  72. /**
  73. * This method pushes a SlotTable on the SlotTableStack. When there is
  74. * a resolve_initial_references("PICurrent") after this call. The new
  75. * PICurrent will be returned.
  76. */
  77. void pushSlotTable( ) {
  78. SlotTableStack st = (SlotTableStack)threadLocalSlotTable.get();
  79. st.pushSlotTable( );
  80. }
  81. /**
  82. * This method pops a SlotTable on the SlotTableStack.
  83. */
  84. void popSlotTable( ) {
  85. SlotTableStack st = (SlotTableStack)threadLocalSlotTable.get();
  86. st.popSlotTable( );
  87. }
  88. /**
  89. * This method sets the slot data at the given slot id (index) in the
  90. * Slot Table which is on the top of the SlotTableStack.
  91. */
  92. public void set_slot( int id, Any data ) throws InvalidSlot
  93. {
  94. if( orbInitializing ) {
  95. // As per ptc/00-08-06 if the ORB is still initializing, disallow
  96. // calls to get_slot and set_slot. If an attempt is made to call,
  97. // throw a BAD_INV_ORDER.
  98. throw wrapper.invalidPiCall3() ;
  99. }
  100. getSlotTable().set_slot( id, data );
  101. }
  102. /**
  103. * This method gets the slot data at the given slot id (index) from the
  104. * Slot Table which is on the top of the SlotTableStack.
  105. */
  106. public Any get_slot( int id ) throws InvalidSlot
  107. {
  108. if( orbInitializing ) {
  109. // As per ptc/00-08-06 if the ORB is still initializing, disallow
  110. // calls to get_slot and set_slot. If an attempt is made to call,
  111. // throw a BAD_INV_ORDER.
  112. throw wrapper.invalidPiCall4() ;
  113. }
  114. return getSlotTable().get_slot( id );
  115. }
  116. /**
  117. * This method resets all the slot data to null in the
  118. * Slot Table which is on the top of SlotTableStack.
  119. */
  120. void resetSlotTable( ) {
  121. getSlotTable().resetSlots();
  122. }
  123. /**
  124. * Called from ORB when the ORBInitializers are about to start
  125. * initializing.
  126. */
  127. void setORBInitializing( boolean init ) {
  128. this.orbInitializing = init;
  129. }
  130. }