1. /*
  2. * @(#)ActiveObjectMap.java 1.16 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.POA;
  8. import org.omg.PortableServer.Servant;
  9. import java.util.*;
  10. /** The ActiveObjectMap is essentially a hashtable which maps object-ids
  11. * (which are byte arrays) to servants (which are of type Servant).
  12. * Currently it just uses java.util.Hashtable internally, and
  13. * will be optimized later.
  14. */
  15. final class ActiveObjectMap
  16. {
  17. private Hashtable map;
  18. private Vector multipleIDs;
  19. public ActiveObjectMap(boolean trackMultipleIDS) {
  20. map = new Hashtable();
  21. if (trackMultipleIDS)
  22. multipleIDs = new Vector();
  23. }
  24. public synchronized final boolean contains(Servant value) {
  25. if (value == null)
  26. throw new NullPointerException();
  27. Enumeration contents = map.elements();
  28. while (contents.hasMoreElements()) {
  29. Servant s = (Servant) contents.nextElement();
  30. if (s == value)
  31. return true;
  32. }
  33. return false;
  34. }
  35. public synchronized final boolean containsKey(byte[] key) {
  36. return map.containsKey(new Key(key));
  37. }
  38. public synchronized final Servant get(byte[] key) {
  39. return (Servant)map.get(new Key(key));
  40. }
  41. public synchronized final byte[] getKey(Servant value)
  42. {
  43. Enumeration keys = map.keys();
  44. while ( keys.hasMoreElements() ) {
  45. Object key = keys.nextElement();
  46. Object element = map.get(key);
  47. if ( element.equals(value) )
  48. return ((Key)key).id;
  49. }
  50. return null;
  51. }
  52. public synchronized final Object put(byte[] key, Servant value) {
  53. if (this.contains(value) && multipleIDs != null)
  54. if (!multipleIDs.contains(value))
  55. multipleIDs.addElement(value);
  56. return map.put(new Key(key), value);
  57. }
  58. public synchronized final boolean hasMultipleIDs(Servant value) {
  59. if (multipleIDs == null)
  60. return false;
  61. if (multipleIDs.contains(value))
  62. return true;
  63. return false;
  64. }
  65. public synchronized final Object remove(byte[] key) {
  66. Servant s = (Servant) map.remove(new Key(key));
  67. if (s == null)
  68. return s;
  69. if (s != null && multipleIDs != null && !this.contains(s))
  70. multipleIDs.removeElement(s);
  71. return s;
  72. }
  73. public synchronized final void clear() {
  74. map.clear();
  75. if (multipleIDs != null) {
  76. multipleIDs.clear();
  77. multipleIDs = null;
  78. }
  79. }
  80. public synchronized Enumeration keys()
  81. {
  82. int size = map.size();
  83. Object[] objs = new Object[size];
  84. Enumeration ks = map.keys();
  85. for ( int i=0; i<size; i++ ) {
  86. Key key = (Key)ks.nextElement();
  87. objs[i] = key.id;
  88. }
  89. return new EnumerationImpl(objs);
  90. }
  91. }
  92. class EnumerationImpl implements Enumeration
  93. {
  94. int index;
  95. int size;
  96. Object[] objArray;
  97. EnumerationImpl(Object[] objs)
  98. {
  99. objArray = objs;
  100. size = objs.length;
  101. index = 0;
  102. }
  103. public boolean hasMoreElements()
  104. {
  105. if ( index < size )
  106. return true;
  107. else
  108. return false;
  109. }
  110. public Object nextElement()
  111. {
  112. Object obj = objArray[index];
  113. index++;
  114. return obj;
  115. }
  116. }
  117. class Key {
  118. byte[] id;
  119. Key(byte[] id) {
  120. this.id = id;
  121. }
  122. public String toString() {
  123. StringBuffer buffer = new StringBuffer();
  124. for(int i = 0; i < id.length; i++) {
  125. buffer.append(Integer.toString((int) id[i], 16));
  126. if (i != id.length-1)
  127. buffer.append(":");
  128. }
  129. return buffer.toString();
  130. }
  131. public boolean equals(java.lang.Object key) {
  132. if (!(key instanceof Key))
  133. return false;
  134. Key k = (Key) key;
  135. if (k.id.length != this.id.length)
  136. return false;
  137. for(int i = 0; i < this.id.length; i++)
  138. if (this.id[i] != k.id[i])
  139. return false;
  140. return true;
  141. }
  142. // Use the same hash function as what exists for Strings
  143. public int hashCode() {
  144. int h = 0;
  145. for (int i = 0; i < id.length; i++)
  146. h = 31*h + id[i];
  147. return h;
  148. }
  149. }