Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jndi/cosnaming/CNBindingEnumeration.java
38923 views
/*1* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.jndi.cosnaming;2627import javax.naming.*;28import javax.naming.spi.NamingManager;2930import java.util.NoSuchElementException;31import java.util.Hashtable;3233import org.omg.CosNaming.*;3435import com.sun.jndi.toolkit.corba.CorbaUtils;3637/**38* Implements the JNDI NamingEnumeration interface for COS39* Naming. Gets hold of a list of bindings from the COS Naming Server40* and allows the client to iterate through them.41*42* @author Raj Krishnamurthy43* @author Rosanna Lee44*/4546final class CNBindingEnumeration47implements NamingEnumeration<javax.naming.Binding> {4849private static final int DEFAULT_BATCHSIZE = 100;50private BindingListHolder _bindingList; // list of bindings51private BindingIterator _bindingIter; // iterator for getting list of bindings52private int counter; // pointer in _bindingList53private int batchsize = DEFAULT_BATCHSIZE; // how many to ask for each time54private CNCtx _ctx; // ctx to list55private Hashtable<?,?> _env; // environment for getObjectInstance56private boolean more = false; // iterator done?57private boolean isLookedUpCtx = false; // iterating on a context beneath this context ?5859/**60* Creates a CNBindingEnumeration object.61* @param ctx Context to enumerate62*/63CNBindingEnumeration(CNCtx ctx, boolean isLookedUpCtx, Hashtable<?,?> env) {64// Get batch size to use65String batch = (env != null ?66(String)env.get(javax.naming.Context.BATCHSIZE) : null);67if (batch != null) {68try {69batchsize = Integer.parseInt(batch);70} catch (NumberFormatException e) {71throw new IllegalArgumentException("Batch size not numeric: " + batch);72}73}74_ctx = ctx;75_ctx.incEnumCount();76this.isLookedUpCtx = isLookedUpCtx;77_env = env;78_bindingList = new BindingListHolder();79BindingIteratorHolder _bindingIterH = new BindingIteratorHolder();8081// Perform listing and request that bindings be returned in _bindingIter82// Upon return,_bindingList returns a zero length list83_ctx._nc.list(0, _bindingList, _bindingIterH);8485_bindingIter = _bindingIterH.value;8687// Get first batch using _bindingIter88if (_bindingIter != null) {89more = _bindingIter.next_n(batchsize, _bindingList);90} else {91more = false;92}93counter = 0;94}9596/**97* Returns the next binding in the list.98* @exception NamingException any naming exception.99*/100101public javax.naming.Binding next() throws NamingException {102if (more && counter >= _bindingList.value.length) {103getMore();104}105if (more && counter < _bindingList.value.length) {106org.omg.CosNaming.Binding bndg = _bindingList.value[counter];107counter++;108return mapBinding(bndg);109} else {110throw new NoSuchElementException();111}112}113114115/**116* Returns true or false depending on whether there are more bindings.117* @return boolean value118*/119120public boolean hasMore() throws NamingException {121// If there's more, check whether current bindingList has been exhausted,122// and if so, try to get more.123// If no more, just say so.124return more ? (counter < _bindingList.value.length || getMore()) : false;125}126127/**128* Returns true or false depending on whether there are more bindings.129* Need to define this to satisfy the Enumeration api requirement.130* @return boolean value131*/132133public boolean hasMoreElements() {134try {135return hasMore();136} catch (NamingException e) {137return false;138}139}140141/**142* Returns the next binding in the list.143* @exception NoSuchElementException Thrown when the end of the144* list is reached.145*/146147public javax.naming.Binding nextElement() {148try {149return next();150} catch (NamingException ne) {151throw new NoSuchElementException();152}153}154155public void close() throws NamingException {156more = false;157if (_bindingIter != null) {158_bindingIter.destroy();159_bindingIter = null;160}161if (_ctx != null) {162_ctx.decEnumCount();163164/**165* context was obtained by CNCtx, the user doesn't have a handle to166* it, close it as we are done enumerating through the context167*/168if (isLookedUpCtx) {169_ctx.close();170}171_ctx = null;172}173}174175protected void finalize() {176try {177close();178} catch (NamingException e) {179// ignore failures180}181}182183/**184* Get the next batch using _bindingIter. Update the 'more' field.185*/186private boolean getMore() throws NamingException {187try {188more = _bindingIter.next_n(batchsize, _bindingList);189counter = 0; // reset190} catch (Exception e) {191more = false;192NamingException ne = new NamingException(193"Problem getting binding list");194ne.setRootCause(e);195throw ne;196}197return more;198}199200/**201* Constructs a JNDI Binding object from the COS Naming binding202* object.203* @exception NameNotFound No objects under the name.204* @exception CannotProceed Unable to obtain a continuation context205* @exception InvalidName Name not understood.206* @exception NamingException One of the above.207*/208209private javax.naming.Binding mapBinding(org.omg.CosNaming.Binding bndg)210throws NamingException {211java.lang.Object obj = _ctx.callResolve(bndg.binding_name);212213Name cname = CNNameParser.cosNameToName(bndg.binding_name);214215try {216// Check whether object factory codebase is trusted217if (CorbaUtils.isObjectFactoryTrusted(obj)) {218obj = NamingManager.getObjectInstance(obj, cname, _ctx, _env);219}220} catch (NamingException e) {221throw e;222} catch (Exception e) {223NamingException ne = new NamingException(224"problem generating object using object factory");225ne.setRootCause(e);226throw ne;227}228229// Use cname.toString() instead of bindingName because the name230// in the binding should be a composite name231String cnameStr = cname.toString();232javax.naming.Binding jbndg = new javax.naming.Binding(cnameStr, obj);233234NameComponent[] comps = _ctx.makeFullName(bndg.binding_name);235String fullName = CNNameParser.cosNameToInsString(comps);236jbndg.setNameInNamespace(fullName);237return jbndg;238}239}240241242