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