PersistentBindingIterator.java revision 673:6b017d166ac2
1/*
2 * Copyright (c) 1999, 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.pcosnaming;
27
28// Import general CORBA classes
29import org.omg.CORBA.SystemException;
30import org.omg.CORBA.ORB;
31import org.omg.CORBA.INTERNAL;
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;
38import org.omg.PortableServer.POA;
39
40// Get base implementation
41import com.sun.corba.se.impl.naming.pcosnaming.NamingContextImpl;
42import com.sun.corba.se.impl.naming.pcosnaming.InternalBindingValue;
43
44import com.sun.corba.se.impl.naming.cosnaming.BindingIteratorImpl;
45
46// Get a hash table
47import java.util.Hashtable;
48import java.util.Enumeration;
49
50/**
51 * Class TransientBindingIterator implements the abstract methods
52 * defined by BindingIteratorImpl, to use with the TransientNamingContext
53 * implementation of the NamingContextImpl. The TransientBindingIterator
54 * implementation receives a hash table of InternalBindingValues, and uses
55 * an Enumeration to iterate over the contents of the hash table.
56 * @see BindingIteratorImpl
57 * @see TransientNamingContext
58 */
59public class PersistentBindingIterator extends BindingIteratorImpl
60{
61    private POA biPOA;
62    /**
63     * Constructs a new PersistentBindingIterator object.
64     * @param orb a org.omg.CORBA.ORB object.
65     * @param aTable A hashtable containing InternalBindingValues which is
66     * the content of the PersistentNamingContext.
67     * @exception Exception a Java exception thrown of the base class cannot
68     * initialize.
69   */
70    public PersistentBindingIterator(org.omg.CORBA.ORB orb, Hashtable aTable,
71        POA thePOA ) throws java.lang.Exception
72    {
73        super(orb);
74        this.orb = orb;
75        theHashtable = aTable;
76        theEnumeration = this.theHashtable.keys();
77        currentSize = this.theHashtable.size();
78        biPOA = 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            InternalBindingKey theBindingKey =
94                 ((InternalBindingKey)theEnumeration.nextElement());
95            InternalBindingValue theElement =
96                (InternalBindingValue)theHashtable.get( theBindingKey );
97            NameComponent n = new NameComponent( theBindingKey.id, theBindingKey.kind );
98            NameComponent[] nlist = new NameComponent[1];
99            nlist[0] = n;
100            BindingType theType = theElement.theBindingType;
101
102            b.value =
103                new Binding( nlist, theType );
104        } else {
105            // Return empty but marshalable binding
106            b.value = new Binding(new NameComponent[0],BindingType.nobject);
107        }
108        return hasMore;
109    }
110
111    /**
112   * Destroys this BindingIterator by disconnecting from the ORB
113   * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA system exceptions.
114   */
115    final public void Destroy()
116    {
117        // Remove the object from the Active Object Map.
118        try {
119            byte[] objectId = biPOA.servant_to_id( this );
120            if( objectId != null ) {
121                biPOA.deactivate_object( objectId );
122            }
123        }
124        catch( Exception e ) {
125            throw new INTERNAL( "Exception in BindingIterator.Destroy " + e );
126        }
127    }
128
129    /**
130   * Returns the remaining number of elements in the iterator.
131   * @return the remaining number of elements in the iterator.
132   */
133    public final int RemainingElements() {
134        return currentSize;
135    }
136
137    private int currentSize;
138    private Hashtable theHashtable;
139    private Enumeration theEnumeration;
140    private org.omg.CORBA.ORB orb;
141}
142