TransientBindingIterator.java revision 608:7e06bf1dcb09
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
26package com.sun.corba.se.impl.naming.cosnaming;
27
28// Import general CORBA classes
29import org.omg.CORBA.SystemException;
30import org.omg.CORBA.ORB;
31import org.omg.PortableServer.POA;
32
33// Get org.omg.CosNaming Types
34import org.omg.CosNaming.Binding;
35import org.omg.CosNaming.BindingType;
36import org.omg.CosNaming.BindingTypeHolder;
37import org.omg.CosNaming.NameComponent;
38
39// Get base implementation
40import com.sun.corba.se.impl.naming.cosnaming.NamingContextImpl;
41import com.sun.corba.se.impl.naming.cosnaming.InternalBindingValue;
42
43// Get a hash table
44import java.util.Hashtable;
45import 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 */
56public 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     * @param java.lang.Exception a Java exception.
67     * @exception Exception a Java exception thrown of the base class cannot
68     * initialize.
69   */
70    public TransientBindingIterator(ORB orb, Hashtable aTable,
71        POA thePOA )
72        throws java.lang.Exception
73    {
74        super(orb);
75        theHashtable = aTable;
76        theEnumeration = this.theHashtable.elements();
77        currentSize = this.theHashtable.size();
78        this.nsPOA = thePOA;
79    }
80
81    /**
82   * Returns the next binding in the NamingContext. Uses the enumeration
83   * object to determine if there are more bindings and if so, returns
84   * the next binding from the InternalBindingValue.
85   * @param b The Binding as an out parameter.
86   * @return true if there were more bindings.
87   */
88    final public boolean NextOne(org.omg.CosNaming.BindingHolder b)
89    {
90        // If there are more elements get the next element
91        boolean hasMore = theEnumeration.hasMoreElements();
92        if (hasMore) {
93            b.value =
94                ((InternalBindingValue)theEnumeration.nextElement()).theBinding;
95            currentSize--;
96        } else {
97            // Return empty but marshalable binding
98            b.value = new Binding(new NameComponent[0],BindingType.nobject);
99        }
100        return hasMore;
101    }
102
103    /**
104     * Destroys this BindingIterator by disconnecting from the ORB
105     * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
106     * system exceptions.
107     */
108    final public void Destroy()
109    {
110        // Remove the object from the Active Object Map.
111        try {
112            byte[] objectId = nsPOA.servant_to_id( this );
113            if( objectId != null ) {
114                nsPOA.deactivate_object( objectId );
115            }
116        }
117        catch( Exception e ) {
118            NamingUtils.errprint("BindingIterator.Destroy():caught exception:");
119            NamingUtils.printException(e);
120        }
121    }
122
123    /**
124     * Returns the remaining number of elements in the iterator.
125     * @return the remaining number of elements in the iterator.
126     */
127    public final int RemainingElements() {
128        return currentSize;
129    }
130
131    private int currentSize;
132    private Hashtable theHashtable;
133    private Enumeration theEnumeration;
134}
135