1 /* GiopNamingEnumeration.java -- handles corbaname: urls 2 Copyright (C) 2006 Free Software Foundation, Inc. 3 4 This file is part of GNU Classpath. 5 6 GNU Classpath is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 11 GNU Classpath is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GNU Classpath; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 02110-1301 USA. 20 21 Linking this library statically or dynamically with other modules is 22 making a combined work based on this library. Thus, the terms and 23 conditions of the GNU General Public License cover the whole 24 combination. 25 26 As a special exception, the copyright holders of this library give you 27 permission to link this library with independent modules to produce an 28 executable, regardless of the license terms of these independent 29 modules, and to copy and distribute the resulting executable under 30 terms of your choice, provided that you also meet, for each linked 31 independent module, the terms and conditions of the license of that 32 module. An independent module is a module which is not derived from 33 or based on this library. If you modify this library, you may extend 34 this exception to your version of the library, but you are not 35 obligated to do so. If you do not wish to do so, delete this 36 exception statement from your version. */ 37 38 39 package gnu.javax.naming.giop; 40 41 import java.util.NoSuchElementException; 42 43 import javax.naming.NamingEnumeration; 44 import javax.naming.NamingException; 45 46 import org.omg.CosNaming.Binding; 47 import org.omg.CosNaming.BindingIterator; 48 import org.omg.CosNaming.BindingIteratorHolder; 49 import org.omg.CosNaming.BindingListHolder; 50 51 /** 52 * Iterates over name class pairs, obtaining values first from the binding list 53 * and then from the binding iterator. 54 * 55 * @author Audrius Meskauskas 56 */ 57 public abstract class GiopNamingEnumeration implements NamingEnumeration 58 { 59 /** 60 * The array of bindings, returned at once. 61 */ 62 Binding[] list; 63 64 /** 65 * The binding iterator to obtain the subsequent bindings. May be null, 66 * if all values are stored in the <code>list</code>. 67 */ 68 BindingIterator iterator; 69 70 /** 71 * The batch size. 72 */ 73 int batch; 74 75 /** 76 * The position of the element in the binding list, that must be returned 77 * during the subsequent call of the next(). If this field is grater or equal 78 * to the lenght of the list, the subsequent values must be requested from the 79 * iterator. 80 */ 81 int p; 82 GiopNamingEnumeration(BindingListHolder bh, BindingIteratorHolder bih, int batchSize)83 GiopNamingEnumeration(BindingListHolder bh, BindingIteratorHolder bih, int batchSize) 84 { 85 list = bh.value; 86 iterator = bih.value; 87 batch = batchSize; 88 } 89 90 /** 91 * Convert from the CORBA binding into that this enumeration should return. 92 * 93 * @param binding 94 * the binding to convert 95 * @return the value, that must be returned by the {@link #next()}. 96 */ convert(Binding binding)97 public abstract Object convert(Binding binding); 98 close()99 public void close() throws NamingException 100 { 101 if (iterator != null) 102 { 103 iterator.destroy(); 104 iterator = null; 105 } 106 } 107 108 /** 109 * Checks if there are more elements to return. 110 * 111 * @throws NamingException 112 * never 113 */ hasMore()114 public boolean hasMore() throws NamingException 115 { 116 return hasMoreElements(); 117 } 118 119 /** 120 * Returns the next element. 121 * 122 * @throws NamingException 123 * never 124 */ next()125 public Object next() throws NamingException 126 { 127 return nextElement(); 128 } 129 130 /** 131 * Checks if there are more elements to return. 132 */ hasMoreElements()133 public boolean hasMoreElements() 134 { 135 if (p < 0) 136 return false; 137 else if (p < list.length) 138 return true; 139 else 140 return getMore(); 141 } 142 143 /** 144 * Returns the next element. 145 */ nextElement()146 public Object nextElement() 147 { 148 if (p < 0) 149 throw new NoSuchElementException(); 150 else if (p < list.length) 151 return convert(list[p++]); 152 else if (getMore()) 153 // getMore updates p 154 return convert(list[p++]); 155 else 156 throw new NoSuchElementException(); 157 } 158 159 /** 160 * Tries to obtain more elements, return true on success. Updates the fields 161 * accordingly. 162 */ getMore()163 boolean getMore() 164 { 165 if (iterator != null) 166 { 167 BindingListHolder holder = new BindingListHolder(); 168 boolean rt = iterator.next_n(batch, holder); 169 if (rt) 170 { 171 // The new pack of the bindings arrived. 172 p = 0; 173 list = holder.value; 174 return true; 175 } 176 else 177 { 178 iterator.destroy(); 179 iterator = null; 180 p = -1; 181 return false; 182 } 183 } 184 else 185 return false; 186 } 187 } 188