1. /*
  2. * @(#)BindingIteratorImpl.java 1.41 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. package com.sun.corba.se.impl.naming.cosnaming;
  8. // Import general CORBA classes
  9. import org.omg.CORBA.ORB;
  10. import org.omg.CORBA.Object;
  11. // Import org.omg.CosNaming classes
  12. import org.omg.CosNaming.Binding;
  13. import org.omg.CosNaming.BindingType;
  14. import org.omg.CosNaming.BindingHolder;
  15. import org.omg.CosNaming.BindingListHolder;
  16. import org.omg.CosNaming.BindingIteratorHolder;
  17. import org.omg.CosNaming.BindingIteratorPOA;
  18. import org.omg.CORBA.BAD_PARAM;
  19. /**
  20. * Class BindingIteratorImpl implements the org.omg.CosNaming::BindingIterator
  21. * interface, but does not implement the method to retrieve the next
  22. * binding in the NamingContext for which it was created. This is left
  23. * to a subclass, which is why this class is abstract; BindingIteratorImpl
  24. * provides an implementation of the interface operations on top of two
  25. * subclass methods, allowing multiple implementations of iterators that
  26. * differ in storage and access to the contents of a NamingContext
  27. * implementation.
  28. * <p>
  29. * The operation next_one() is implemented by the subclass, whereas
  30. * next_n() is implemented on top of the next_one() implementation.
  31. * Destroy must also be implemented by the subclass.
  32. * <p>
  33. * A subclass must implement NextOne() and Destroy(); these
  34. * methods are invoked from synchronized methods and need therefore
  35. * not be synchronized themselves.
  36. */
  37. public abstract class BindingIteratorImpl extends BindingIteratorPOA
  38. {
  39. protected ORB orb ;
  40. /**
  41. * Create a binding iterator servant.
  42. * runs the super constructor.
  43. * @param orb an ORB object.
  44. * @exception java.lang.Exception a Java exception.
  45. */
  46. public BindingIteratorImpl(ORB orb)
  47. throws java.lang.Exception
  48. {
  49. super();
  50. this.orb = orb ;
  51. }
  52. /**
  53. * Return the next binding. It also returns true or false, indicating
  54. * whether there were more bindings.
  55. * @param b The Binding as an out parameter.
  56. * @return true if there were more bindings.
  57. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  58. * system exceptions.
  59. * @see NextOne
  60. */
  61. public synchronized boolean next_one(org.omg.CosNaming.BindingHolder b)
  62. {
  63. // NextOne actually returns the next one
  64. return NextOne(b);
  65. }
  66. /**
  67. * Return the next n bindings. It also returns true or false, indicating
  68. * whether there were more bindings.
  69. * @param how_many The number of requested bindings in the BindingList.
  70. * @param bl The BindingList as an out parameter.
  71. * @return true if there were more bindings.
  72. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  73. * system exceptions.
  74. * @see NextOne
  75. */
  76. public synchronized boolean next_n(int how_many,
  77. org.omg.CosNaming.BindingListHolder blh)
  78. {
  79. if( how_many == 0 ) {
  80. throw new BAD_PARAM( " 'how_many' parameter is set to 0 which is" +
  81. " invalid" );
  82. }
  83. return list( how_many, blh );
  84. }
  85. /**
  86. * lists next n bindings. It returns true or false, indicating
  87. * whether there were more bindings. This method has the package private
  88. * scope, It will be called from NamingContext.list() operation or
  89. * this.next_n().
  90. * @param how_many The number of requested bindings in the BindingList.
  91. * @param bl The BindingList as an out parameter.
  92. * @return true if there were more bindings.
  93. */
  94. public boolean list( int how_many, org.omg.CosNaming.BindingListHolder blh)
  95. {
  96. // Take the smallest of what's left and what's being asked for
  97. int numberToGet = Math.min(RemainingElements(),how_many);
  98. // Create a resulting BindingList
  99. Binding[] bl = new Binding[numberToGet];
  100. BindingHolder bh = new BindingHolder();
  101. int i = 0;
  102. // Keep iterating as long as there are entries
  103. while (i < numberToGet && this.NextOne(bh) == true) {
  104. bl[i] = bh.value;
  105. i++;
  106. }
  107. // Found any at all?
  108. if (i == 0) {
  109. // No
  110. blh.value = new Binding[0];
  111. return false;
  112. }
  113. // Set into holder
  114. blh.value = bl;
  115. return true;
  116. }
  117. /**
  118. * Destroy this BindingIterator object. The object corresponding to this
  119. * object reference is destroyed.
  120. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  121. * system exceptions.
  122. * @see Destroy
  123. */
  124. public synchronized void destroy()
  125. {
  126. // Destroy actually destroys
  127. this.Destroy();
  128. }
  129. /**
  130. * Abstract method for returning the next binding in the NamingContext
  131. * for which this BindingIterator was created.
  132. * @param b The Binding as an out parameter.
  133. * @return true if there were more bindings.
  134. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  135. * system exceptions.
  136. */
  137. protected abstract boolean NextOne(org.omg.CosNaming.BindingHolder b);
  138. /**
  139. * Abstract method for destroying this BindingIterator.
  140. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  141. * system exceptions.
  142. */
  143. protected abstract void Destroy();
  144. /**
  145. * Abstract method for returning the remaining number of elements.
  146. * @return the remaining number of elements in the iterator.
  147. */
  148. protected abstract int RemainingElements();
  149. }