1. /*
  2. * @(#)TransientObjectManager.java 1.30 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. /*
  8. * Licensed Materials - Property of IBM
  9. * RMI-IIOP v1.0
  10. * Copyright IBM Corp. 1998 1999 All Rights Reserved
  11. *
  12. * US Government Users Restricted Rights - Use, duplication or
  13. * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  14. */
  15. package com.sun.corba.se.impl.oa.toa;
  16. import com.sun.corba.se.impl.orbutil.ORBUtility ;
  17. import com.sun.corba.se.spi.orb.ORB ;
  18. public final class TransientObjectManager {
  19. private ORB orb ;
  20. private int maxSize = 128;
  21. private Element[] elementArray;
  22. private Element freeList;
  23. void dprint( String msg ) {
  24. ORBUtility.dprint( this, msg ) ;
  25. }
  26. public TransientObjectManager( ORB orb )
  27. {
  28. this.orb = orb ;
  29. elementArray = new Element[maxSize];
  30. elementArray[maxSize-1] = new Element(maxSize-1,null);
  31. for ( int i=maxSize-2; i>=0; i-- )
  32. elementArray[i] = new Element(i,elementArray[i+1]);
  33. freeList = elementArray[0];
  34. }
  35. public synchronized byte[] storeServant(java.lang.Object servant, java.lang.Object servantData)
  36. {
  37. if ( freeList == null )
  38. doubleSize();
  39. Element elem = freeList;
  40. freeList = (Element)freeList.servant;
  41. byte[] result = elem.getKey(servant, servantData);
  42. if (orb.transientObjectManagerDebugFlag)
  43. dprint( "storeServant returns key for element " + elem ) ;
  44. return result ;
  45. }
  46. public synchronized java.lang.Object lookupServant(byte transientKey[])
  47. {
  48. int index = ORBUtility.bytesToInt(transientKey,0);
  49. int counter = ORBUtility.bytesToInt(transientKey,4);
  50. if (orb.transientObjectManagerDebugFlag)
  51. dprint( "lookupServant called with index=" + index + ", counter=" + counter ) ;
  52. if (elementArray[index].counter == counter &&
  53. elementArray[index].valid ) {
  54. if (orb.transientObjectManagerDebugFlag)
  55. dprint( "\tcounter is valid" ) ;
  56. return elementArray[index].servant;
  57. }
  58. // servant not found
  59. if (orb.transientObjectManagerDebugFlag)
  60. dprint( "\tcounter is invalid" ) ;
  61. return null;
  62. }
  63. public synchronized java.lang.Object lookupServantData(byte transientKey[])
  64. {
  65. int index = ORBUtility.bytesToInt(transientKey,0);
  66. int counter = ORBUtility.bytesToInt(transientKey,4);
  67. if (orb.transientObjectManagerDebugFlag)
  68. dprint( "lookupServantData called with index=" + index + ", counter=" + counter ) ;
  69. if (elementArray[index].counter == counter &&
  70. elementArray[index].valid ) {
  71. if (orb.transientObjectManagerDebugFlag)
  72. dprint( "\tcounter is valid" ) ;
  73. return elementArray[index].servantData;
  74. }
  75. // servant not found
  76. if (orb.transientObjectManagerDebugFlag)
  77. dprint( "\tcounter is invalid" ) ;
  78. return null;
  79. }
  80. public synchronized void deleteServant(byte transientKey[])
  81. {
  82. int index = ORBUtility.bytesToInt(transientKey,0);
  83. if (orb.transientObjectManagerDebugFlag)
  84. dprint( "deleting servant at index=" + index ) ;
  85. elementArray[index].delete(freeList);
  86. freeList = elementArray[index];
  87. }
  88. public synchronized byte[] getKey(java.lang.Object servant)
  89. {
  90. for ( int i=0; i<maxSize; i++ )
  91. if ( elementArray[i].valid &&
  92. elementArray[i].servant == servant )
  93. return elementArray[i].toBytes();
  94. // if we come here Object does not exist
  95. return null;
  96. }
  97. private void doubleSize()
  98. {
  99. // Assume caller is synchronized
  100. Element old[] = elementArray;
  101. int oldSize = maxSize;
  102. maxSize *= 2;
  103. elementArray = new Element[maxSize];
  104. for ( int i=0; i<oldSize; i++ )
  105. elementArray[i] = old[i];
  106. elementArray[maxSize-1] = new Element(maxSize-1,null);
  107. for ( int i=maxSize-2; i>=oldSize; i-- )
  108. elementArray[i] = new Element(i,elementArray[i+1]);
  109. freeList = elementArray[oldSize];
  110. }
  111. }
  112. final class Element {
  113. java.lang.Object servant=null; // also stores "next pointer" in free list
  114. java.lang.Object servantData=null;
  115. int index=-1;
  116. int counter=0;
  117. boolean valid=false; // valid=true if this Element contains
  118. // a valid servant
  119. Element(int i, java.lang.Object next)
  120. {
  121. servant = next;
  122. index = i;
  123. }
  124. byte[] getKey(java.lang.Object servant, java.lang.Object servantData)
  125. {
  126. this.servant = servant;
  127. this.servantData = servantData;
  128. this.valid = true;
  129. return toBytes();
  130. }
  131. byte[] toBytes()
  132. {
  133. // Convert the index+counter into an 8-byte (big-endian) key.
  134. byte key[] = new byte[8];
  135. ORBUtility.intToBytes(index, key, 0);
  136. ORBUtility.intToBytes(counter, key, 4);
  137. return key;
  138. }
  139. void delete(Element freeList)
  140. {
  141. if ( !valid ) // prevent double deletion
  142. return;
  143. counter++;
  144. servantData = null;
  145. valid = false;
  146. // add this to freeList
  147. servant = freeList;
  148. }
  149. public String toString()
  150. {
  151. return "Element[" + index + ", " + counter + "]" ;
  152. }
  153. }