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