1 /*
   2  * Copyright (c) 1996, 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.corba.se.impl.naming.cosnaming;
  27 
  28 // Import general CORBA classes
  29 import org.omg.CORBA.SystemException;
  30 import org.omg.CORBA.ORB;
  31 import org.omg.PortableServer.POA;
  32 
  33 // Get org.omg.CosNaming Types
  34 import org.omg.CosNaming.Binding;
  35 import org.omg.CosNaming.BindingType;
  36 import org.omg.CosNaming.BindingTypeHolder;
  37 import org.omg.CosNaming.NameComponent;
  38 
  39 // Get base implementation
  40 import com.sun.corba.se.impl.naming.cosnaming.NamingContextImpl;
  41 import com.sun.corba.se.impl.naming.cosnaming.InternalBindingValue;
  42 
  43 // Get a hash table
  44 import java.util.Hashtable;
  45 import java.util.Enumeration;
  46 
  47 /**
  48  * Class TransientBindingIterator implements the abstract methods
  49  * defined by BindingIteratorImpl, to use with the TransientNamingContext
  50  * implementation of the NamingContextImpl. The TransientBindingIterator
  51  * implementation receives a hash table of InternalBindingValues, and uses
  52  * an Enumeration to iterate over the contents of the hash table.
  53  * @see BindingIteratorImpl
  54  * @see TransientNamingContext
  55  */
  56 public class TransientBindingIterator extends BindingIteratorImpl
  57 {
  58     // There is only one POA used for both TransientNamingContext and
  59     // TransientBindingIteraor servants.
  60     private POA nsPOA;
  61     /**
  62      * Constructs a new TransientBindingIterator object.
  63      * @param orb a org.omg.CORBA.ORB object.
  64      * @param aTable A hashtable containing InternalBindingValues which is
  65      * the content of the TransientNamingContext.
  66      * @exception Exception a Java exception thrown of the base class cannot
  67      * initialize.
  68    */
  69     public TransientBindingIterator(ORB orb, Hashtable aTable,
  70         POA thePOA )
  71         throws java.lang.Exception
  72     {
  73         super(orb);
  74         theHashtable = aTable;
  75         theEnumeration = this.theHashtable.elements();
  76         currentSize = this.theHashtable.size();
  77         this.nsPOA = thePOA;
  78     }
  79 
  80     /**
  81    * Returns the next binding in the NamingContext. Uses the enumeration
  82    * object to determine if there are more bindings and if so, returns
  83    * the next binding from the InternalBindingValue.
  84    * @param b The Binding as an out parameter.
  85    * @return true if there were more bindings.
  86    */
  87     final public boolean NextOne(org.omg.CosNaming.BindingHolder b)
  88     {
  89         // If there are more elements get the next element
  90         boolean hasMore = theEnumeration.hasMoreElements();
  91         if (hasMore) {
  92             b.value =
  93                 ((InternalBindingValue)theEnumeration.nextElement()).theBinding;
  94             currentSize--;
  95         } else {
  96             // Return empty but marshalable binding
  97             b.value = new Binding(new NameComponent[0],BindingType.nobject);
  98         }
  99         return hasMore;
 100     }
 101 
 102     /**
 103      * Destroys this BindingIterator by disconnecting from the ORB
 104      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
 105      * system exceptions.
 106      */
 107     final public void Destroy()
 108     {
 109         // Remove the object from the Active Object Map.
 110         try {
 111             byte[] objectId = nsPOA.servant_to_id( this );
 112             if( objectId != null ) {
 113                 nsPOA.deactivate_object( objectId );
 114             }
 115         }
 116         catch( Exception e ) {
 117             NamingUtils.errprint("BindingIterator.Destroy():caught exception:");
 118             NamingUtils.printException(e);
 119         }
 120     }
 121 
 122     /**
 123      * Returns the remaining number of elements in the iterator.
 124      * @return the remaining number of elements in the iterator.
 125      */
 126     public final int RemainingElements() {
 127         return currentSize;
 128     }
 129 
 130     private int currentSize;
 131     private Hashtable theHashtable;
 132     private Enumeration theEnumeration;
 133 }