001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Vasily Zakharov
021: * @version $Revision: 1.1.2.2 $
022: */package org.apache.harmony.jndi.provider.rmi.registry;
023:
024: import java.rmi.registry.Registry;
025:
026: import java.util.NoSuchElementException;
027:
028: import javax.naming.Binding;
029: import javax.naming.Name;
030: import javax.naming.NamingEnumeration;
031: import javax.naming.NamingException;
032:
033: /**
034: * Enumeration of {@link Binding} objects, used by
035: * {@link RegistryContext#listBindings(Name)} method.
036: */
037: class BindingEnumeration implements NamingEnumeration<Binding> {
038:
039: /**
040: * Binding names returned from {@link Registry#list()} method.
041: */
042: protected final String[] names;
043:
044: /**
045: * Index of the next name to return.
046: */
047: protected int index = 0;
048:
049: /**
050: * Registry context.
051: */
052: protected RegistryContext context;
053:
054: /**
055: * Creates this enumeration.
056: *
057: * @param names
058: * Binding names returned from {@link Registry#list()} method.
059: *
060: * @param context
061: * RegistryContext to extract bindings from.
062: */
063: public BindingEnumeration(String[] names, RegistryContext context) {
064: super ();
065: this .names = names;
066: this .context = context.cloneContext();
067: }
068:
069: public boolean hasMore() {
070: if (index < names.length) {
071: return true;
072: }
073: close();
074: return false;
075: }
076:
077: public boolean hasMoreElements() {
078: return hasMore();
079: }
080:
081: public Binding next() throws NoSuchElementException,
082: NamingException {
083: if (!hasMore()) {
084: throw new NoSuchElementException();
085: }
086:
087: String name = names[index++];
088: Binding binding = new Binding(name, context.lookup(name));
089: binding.setNameInNamespace(name);
090: return binding;
091: }
092:
093: public Binding nextElement() {
094: try {
095: return next();
096: } catch (NamingException e) {
097: throw (NoSuchElementException) new NoSuchElementException()
098: .initCause(e);
099: }
100: }
101:
102: public void close() {
103: index = names.length;
104: context.close();
105: }
106:
107: }
|