1. /*
  2. * @(#)ThreadCurrentStack.java 1.8 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 com.sun.corba.se.internal.util.MinorCodes;
  12. /**
  13. * ThreadCurrentStack is the container of PICurrent instances for each thread
  14. */
  15. public class ThreadCurrentStack
  16. {
  17. // PICurrentPool is the container for reusable PICurrents
  18. private class PICurrentPool {
  19. // Contains a list of reusable PICurrents
  20. private java.util.ArrayList pool;
  21. // High water mark for the pool
  22. // If the pool size reaches this limit then putPICurrent will
  23. // not put PICurrent to the pool.
  24. private static final int HIGH_WATER_MARK = 5;
  25. // currentIndex points to the last PICurrent in the list
  26. private int currentIndex;
  27. PICurrentPool( ) {
  28. pool = new java.util.ArrayList( HIGH_WATER_MARK );
  29. currentIndex = 0;
  30. }
  31. /**
  32. * Puts PICurrent to the re-usable pool.
  33. */
  34. void putPICurrent( PICurrent current ) {
  35. // If there are enough PICurrents in the pool, then don't add
  36. // this current to the pool.
  37. if( currentIndex >= HIGH_WATER_MARK ) {
  38. return;
  39. }
  40. pool.add(currentIndex , current);
  41. currentIndex++;
  42. }
  43. /**
  44. * Gets PICurrent from the re-usable pool.
  45. */
  46. PICurrent getPICurrent( ) {
  47. // If there are no entries in the pool then return null
  48. if( currentIndex == 0 ) {
  49. return null;
  50. }
  51. // Works like a stack, Gets the last one added first
  52. currentIndex--;
  53. return (PICurrent) pool.get(currentIndex);
  54. }
  55. }
  56. // Contains all the active PICurrents for each thread.
  57. // The ArrayList is made to behave like a stack.
  58. private java.util.ArrayList currentContainer;
  59. // Keeps track of number of PICurrents in the stack.
  60. private int currentIndex;
  61. // For Every Thread there will be a pool of re-usable ThreadCurrent's
  62. // stored in PICurrentPool
  63. private PICurrentPool currentPool;
  64. // The orb associated with this ThreadCurrentStack
  65. private PIORB piOrb;
  66. /**
  67. * Constructs the stack and and PICurrentPool
  68. */
  69. ThreadCurrentStack( PIORB piOrb, PICurrent current ) {
  70. this.piOrb = piOrb;
  71. currentIndex = 0;
  72. currentContainer = new java.util.ArrayList( );
  73. currentPool = new PICurrentPool( );
  74. currentContainer.add( currentIndex, current );
  75. currentIndex++;
  76. }
  77. /**
  78. * pushPICurrent goes through the following steps
  79. * 1: Checks to see if there is any PICurrent in PICurrentPool
  80. * If present then use that instance to push into the ThreadCurrentStack
  81. *
  82. * 2:If there is no PICurrent in the pool, then creates a new one and pushes
  83. * that into the ThreadCurrentStack
  84. */
  85. void pushPICurrent( ) {
  86. PICurrent current = currentPool.getPICurrent( );
  87. if( current == null ) {
  88. // get an existing PICurrent to get the slotSize
  89. PICurrent currentTemp = peekPICurrent();
  90. current = new PICurrent( piOrb, currentTemp.getSlotSize( ));
  91. }
  92. currentContainer.add( currentIndex, current );
  93. currentIndex++;
  94. }
  95. /**
  96. * popPICurrent does the following
  97. * 1: pops the top PICurrent in the ThreadCurrentStack
  98. *
  99. * 2: resets the slots in the PICurrent which resets the slotvalues to
  100. * null if there are any previous sets.
  101. *
  102. * 3: pushes the reset PICurrent into the PICurrentPool to reuse
  103. */
  104. void popPICurrent( ) {
  105. // Do not pop the PICurrent, If there is only one.
  106. // This should not happen, But an extra check for safety.
  107. if( currentIndex <= 1 ) {
  108. throw new org.omg.CORBA.INTERNAL(
  109. "Cannot pop the only PICurrent in the stack",
  110. MinorCodes.CANT_POP_ONLY_CURRENT_2,
  111. CompletionStatus.COMPLETED_NO );
  112. }
  113. currentIndex--;
  114. PICurrent current = (PICurrent)currentContainer.get( currentIndex );
  115. current.resetSlots( );
  116. currentPool.putPICurrent( current );
  117. }
  118. /**
  119. * peekPICurrent gets the top PICurrent in the ThreadCurrentStack without
  120. * popping.
  121. */
  122. PICurrent peekPICurrent( ) {
  123. return (PICurrent) currentContainer.get( currentIndex - 1);
  124. }
  125. }