Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jndi/cosnaming/CNCtx.java
38924 views
/*1* Copyright (c) 1999, 2012, 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;29import javax.naming.spi.ResolveResult;3031import java.util.Hashtable;32import java.net.MalformedURLException;33import java.net.URL;34import java.io.InputStream;35import java.io.InputStreamReader;36import java.io.BufferedReader;37import java.io.IOException;38import java.security.AccessController;39import java.security.PrivilegedAction;4041import org.omg.CosNaming.*;42import org.omg.CosNaming.NamingContextPackage.*;43import org.omg.CORBA.*;4445import com.sun.jndi.toolkit.corba.CorbaUtils;4647// Needed for creating default ORB48import java.applet.Applet;4950/**51* Provides a bridge to the CosNaming server provided by52* JavaIDL. This class provides the InitialContext from CosNaming.53*54* @author Raj Krishnamurthy55* @author Rosanna Lee56*/5758public class CNCtx implements javax.naming.Context {5960private final static boolean debug = false;6162/*63* Implement one shared ORB among all CNCtx. However, there is a public constructor64* accepting an ORB, so we need the option of using a given ORB.65*/66private static ORB _defaultOrb;67ORB _orb; // used by ExceptionMapper and RMI/IIOP factory68public NamingContext _nc; // public for accessing underlying NamingContext6970private synchronized static ORB getDefaultOrb() {71if (_defaultOrb == null) {72_defaultOrb = CorbaUtils.getOrb(null, -1,73new Hashtable<String, java.lang.Object>());74}75return _defaultOrb;76}7778private NameComponent[] _name = null;7980Hashtable<String, java.lang.Object> _env; // used by ExceptionMapper81static final CNNameParser parser = new CNNameParser();8283private static final String FED_PROP = "com.sun.jndi.cosnaming.federation";84boolean federation = false;8586/**87* Determines whether classes may be loaded from an arbitrary URL code base.88*/89public static final boolean trustURLCodebase;90static {91// System property to control whether classes may be loaded from an92// arbitrary URL code base93PrivilegedAction<String> act = () -> System.getProperty(94"com.sun.jndi.cosnaming.object.trustURLCodebase", "false");95String trust = AccessController.doPrivileged(act);96trustURLCodebase = "true".equalsIgnoreCase(trust);97}9899// Reference counter for tracking _orb references100OrbReuseTracker orbTracker = null;101int enumCount;102boolean isCloseCalled = false;103104/**105* Create a CNCtx object. Gets the initial naming106* reference for the COS Naming Service from the ORB.107* The ORB can be passed in via the java.naming.corba.orb property108* or be created using properties in the environment properties.109* @param env Environment properties for initializing name service.110* @exception NamingException Cannot initialize ORB or naming context.111*/112@SuppressWarnings("unchecked")113CNCtx(Hashtable<?,?> env) throws NamingException {114if (env != null) {115env = (Hashtable<?,?>)env.clone();116}117_env = (Hashtable<String, java.lang.Object>)env;118federation = "true".equals(env != null ? env.get(FED_PROP) : null);119initOrbAndRootContext(env);120}121122private CNCtx() {123}124125/**126* This method is used by the iiop and iiopname URL Context factories.127*/128@SuppressWarnings("unchecked")129public static ResolveResult createUsingURL(String url, Hashtable<?,?> env)130throws NamingException {131CNCtx ctx = new CNCtx();132if (env != null) {133env = (Hashtable<?,?>) env.clone();134}135ctx._env = (Hashtable<String, java.lang.Object>)env;136String rest = ctx.initUsingUrl(137env != null ?138(org.omg.CORBA.ORB) env.get("java.naming.corba.orb")139: null,140url, env);141142// rest is the INS name143// Return the parsed form to prevent subsequent lookup144// from parsing the string as a composite name145// The caller should be aware that a toString() of the name,146// which came from the environment will yield its INS syntax,147// rather than a composite syntax148return new ResolveResult(ctx, parser.parse(rest));149}150151/**152* Creates a CNCtx object which supports the javax.naming153* apis given a COS Naming Context object.154* @param orb The ORB used by this context155* @param tracker The ORB reuse tracker for tracking references to the156* orb object157* @param nctx The COS NamingContext object associated with this context158* @param name The name of this context relative to the root159*/160161CNCtx(ORB orb, OrbReuseTracker tracker, NamingContext nctx,162Hashtable<String, java.lang.Object> env, NameComponent[]name)163throws NamingException {164if (orb == null || nctx == null)165throw new ConfigurationException(166"Must supply ORB or NamingContext");167if (orb != null) {168_orb = orb;169} else {170_orb = getDefaultOrb();171}172_nc = nctx;173_env = env;174_name = name;175federation = "true".equals(env != null ? env.get(FED_PROP) : null);176}177178NameComponent[] makeFullName(NameComponent[] child) {179if (_name == null || _name.length == 0) {180return child;181}182NameComponent[] answer = new NameComponent[_name.length+child.length];183184// parent185System.arraycopy(_name, 0, answer, 0, _name.length);186187// child188System.arraycopy(child, 0, answer, _name.length, child.length);189return answer;190}191192193public String getNameInNamespace() throws NamingException {194if (_name == null || _name.length == 0) {195return "";196}197return CNNameParser.cosNameToInsString(_name);198}199200/**201* These are the URL schemes that need to be processed.202* IOR and corbaloc URLs can be passed directly to ORB.string_to_object()203*/204private static boolean isCorbaUrl(String url) {205return url.startsWith("iiop://")206|| url.startsWith("iiopname://")207|| url.startsWith("corbaname:")208;209}210211/**212* Initializes the COS Naming Service.213* This method initializes the three instance fields:214* _nc : The root naming context.215* _orb: The ORB to use for connecting RMI/IIOP stubs and for216* getting the naming context (_nc) if one was not specified217* explicitly via PROVIDER_URL.218* _name: The name of the root naming context.219*<p>220* _orb is obtained from java.naming.corba.orb if it has been set.221* Otherwise, _orb is created using the host/port from PROVIDER_URL222* (if it contains an "iiop" or "iiopname" URL), or from initialization223* properties specified in env.224*<p>225* _nc is obtained from the IOR stored in PROVIDER_URL if it has been226* set and does not contain an "iiop" or "iiopname" URL. It can be227* a stringified IOR, "corbaloc" URL, "corbaname" URL,228* or a URL (such as file/http/ftp) to a location229* containing a stringified IOR. If PROVIDER_URL has not been230* set in this way, it is obtained from the result of231* ORB.resolve_initial_reference("NameService");232*<p>233* _name is obtained from the "iiop", "iiopname", or "corbaname" URL.234* It is the empty name by default.235*236* @param env Environment The possibly null environment.237* @exception NamingException When an error occurs while initializing the238* ORB or the naming context.239*/240private void initOrbAndRootContext(Hashtable<?,?> env) throws NamingException {241org.omg.CORBA.ORB inOrb = null;242String ncIor = null;243244if (inOrb == null && env != null) {245inOrb = (org.omg.CORBA.ORB) env.get("java.naming.corba.orb");246}247248if (inOrb == null)249inOrb = getDefaultOrb(); // will create a default ORB if none exists250251// Extract PROVIDER_URL from environment252String provUrl = null;253if (env != null) {254provUrl = (String)env.get(javax.naming.Context.PROVIDER_URL);255}256257if (provUrl != null && !isCorbaUrl(provUrl)) {258// Initialize the root naming context by using the IOR supplied259// in the PROVIDER_URL260ncIor = getStringifiedIor(provUrl);261setOrbAndRootContext(inOrb, ncIor);262} else if (provUrl != null) {263// Initialize the root naming context by using the URL supplied264// in the PROVIDER_URL265String insName = initUsingUrl(inOrb, provUrl, env);266267// If name supplied in URL, resolve it to a NamingContext268if (insName.length() > 0) {269_name = CNNameParser.nameToCosName(parser.parse(insName));270try {271org.omg.CORBA.Object obj = _nc.resolve(_name);272_nc = NamingContextHelper.narrow(obj);273if (_nc == null) {274throw new ConfigurationException(insName +275" does not name a NamingContext");276}277} catch (org.omg.CORBA.BAD_PARAM e) {278throw new ConfigurationException(insName +279" does not name a NamingContext");280} catch (Exception e) {281throw ExceptionMapper.mapException(e, this, _name);282}283}284} else {285// No PROVIDER_URL supplied; initialize using defaults286if (debug) {287System.err.println("Getting default ORB: " + inOrb + env);288}289setOrbAndRootContext(inOrb, (String)null);290}291}292293294private String initUsingUrl(ORB orb, String url, Hashtable<?,?> env)295throws NamingException {296if (url.startsWith("iiop://") || url.startsWith("iiopname://")) {297return initUsingIiopUrl(orb, url, env);298} else {299return initUsingCorbanameUrl(orb, url, env);300}301}302303/**304* Handles "iiop" and "iiopname" URLs (INS 98-10-11)305*/306private String initUsingIiopUrl(ORB defOrb, String url, Hashtable<?,?> env)307throws NamingException {308309if (defOrb == null)310defOrb = getDefaultOrb();311312try {313IiopUrl parsedUrl = new IiopUrl(url);314315NamingException savedException = null;316317for (IiopUrl.Address addr : parsedUrl.getAddresses()) {318319try {320try {321String tmpUrl = "corbaloc:iiop:" + addr.host322+ ":" + addr.port + "/NameService";323if (debug) {324System.err.println("Using url: " + tmpUrl);325}326org.omg.CORBA.Object rootCtx =327defOrb.string_to_object(tmpUrl);328setOrbAndRootContext(defOrb, rootCtx);329return parsedUrl.getStringName();330} catch (Exception e) {} // keep going331332// Get ORB333if (debug) {334System.err.println("Getting ORB for " + addr.host335+ " and port " + addr.port);336}337338// Assign to fields339setOrbAndRootContext(defOrb, (String)null);340return parsedUrl.getStringName();341342} catch (NamingException ne) {343savedException = ne;344}345}346if (savedException != null) {347throw savedException;348} else {349throw new ConfigurationException("Problem with URL: " + url);350}351} catch (MalformedURLException e) {352throw new ConfigurationException(e.getMessage());353}354}355356/**357* Initializes using "corbaname" URL (INS 99-12-03)358*/359private String initUsingCorbanameUrl(ORB orb, String url, Hashtable<?,?> env)360throws NamingException {361362if (orb == null)363orb = getDefaultOrb();364365try {366CorbanameUrl parsedUrl = new CorbanameUrl(url);367368String corbaloc = parsedUrl.getLocation();369String cosName = parsedUrl.getStringName();370371setOrbAndRootContext(orb, corbaloc);372373return parsedUrl.getStringName();374} catch (MalformedURLException e) {375throw new ConfigurationException(e.getMessage());376}377}378379private void setOrbAndRootContext(ORB orb, String ncIor)380throws NamingException {381_orb = orb;382try {383org.omg.CORBA.Object ncRef;384if (ncIor != null) {385if (debug) {386System.err.println("Passing to string_to_object: " + ncIor);387}388ncRef = _orb.string_to_object(ncIor);389} else {390ncRef = _orb.resolve_initial_references("NameService");391}392if (debug) {393System.err.println("Naming Context Ref: " + ncRef);394}395_nc = NamingContextHelper.narrow(ncRef);396if (_nc == null) {397if (ncIor != null) {398throw new ConfigurationException(399"Cannot convert IOR to a NamingContext: " + ncIor);400} else {401throw new ConfigurationException(402"ORB.resolve_initial_references(\"NameService\") does not return a NamingContext");403}404}405} catch (org.omg.CORBA.ORBPackage.InvalidName in) {406NamingException ne =407new ConfigurationException(408"COS Name Service not registered with ORB under the name 'NameService'");409ne.setRootCause(in);410throw ne;411} catch (org.omg.CORBA.COMM_FAILURE e) {412NamingException ne =413new CommunicationException("Cannot connect to ORB");414ne.setRootCause(e);415throw ne;416} catch (org.omg.CORBA.BAD_PARAM e) {417NamingException ne = new ConfigurationException(418"Invalid URL or IOR: " + ncIor);419ne.setRootCause(e);420throw ne;421} catch (org.omg.CORBA.INV_OBJREF e) {422NamingException ne = new ConfigurationException(423"Invalid object reference: " + ncIor);424ne.setRootCause(e);425throw ne;426}427}428429private void setOrbAndRootContext(ORB orb, org.omg.CORBA.Object ncRef)430throws NamingException {431_orb = orb;432try {433_nc = NamingContextHelper.narrow(ncRef);434if (_nc == null) {435throw new ConfigurationException(436"Cannot convert object reference to NamingContext: " + ncRef);437}438} catch (org.omg.CORBA.COMM_FAILURE e) {439NamingException ne =440new CommunicationException("Cannot connect to ORB");441ne.setRootCause(e);442throw ne;443}444}445446private String getStringifiedIor(String url) throws NamingException {447if (url.startsWith("IOR:") || url.startsWith("corbaloc:")) {448return url;449} else {450InputStream in = null;451try {452URL u = new URL(url);453in = u.openStream();454if (in != null) {455BufferedReader bufin =456new BufferedReader(new InputStreamReader(in, "8859_1"));457String str;458while ((str = bufin.readLine()) != null) {459if (str.startsWith("IOR:")) {460return str;461}462}463}464} catch (IOException e) {465NamingException ne =466new ConfigurationException("Invalid URL: " + url);467ne.setRootCause(e);468throw ne;469} finally {470try {471if (in != null) {472in.close();473}474} catch (IOException e) {475NamingException ne =476new ConfigurationException("Invalid URL: " + url);477ne.setRootCause(e);478throw ne;479}480}481throw new ConfigurationException(url + " does not contain an IOR");482}483}484485486/**487* Does the job of calling the COS Naming API,488* resolve, and performs the exception mapping. If the resolved489* object is a COS Naming Context (sub-context), then this function490* returns a new JNDI naming context object.491* @param path the NameComponent[] object.492* @exception NotFound No objects under the name.493* @exception CannotProceed Unable to obtain a continuation context494* @exception InvalidName Name not understood.495* @return Resolved object returned by the COS Name Server.496*/497java.lang.Object callResolve(NameComponent[] path)498throws NamingException {499try {500org.omg.CORBA.Object obj = _nc.resolve(path);501try {502NamingContext nc =503NamingContextHelper.narrow(obj);504if (nc != null) {505return new CNCtx(_orb, orbTracker, nc, _env,506makeFullName(path));507} else {508return obj;509}510} catch (org.omg.CORBA.SystemException e) {511return obj;512}513} catch (Exception e) {514throw ExceptionMapper.mapException(e, this, path);515}516}517518/**519* Converts the "String" name into a CompositeName520* returns the object resolved by the COS Naming api,521* resolve. Returns the current context if the name is empty.522* Returns either an org.omg.CORBA.Object or javax.naming.Context object.523* @param name string used to resolve the object.524* @exception NamingException See callResolve.525* @return the resolved object526*/527public java.lang.Object lookup(String name) throws NamingException {528if (debug) {529System.out.println("Looking up: " + name);530}531return lookup(new CompositeName(name));532}533534/**535* Converts the "Name" name into a NameComponent[] object and536* returns the object resolved by the COS Naming api,537* resolve. Returns the current context if the name is empty.538* Returns either an org.omg.CORBA.Object or javax.naming.Context object.539* @param name JNDI Name used to resolve the object.540* @exception NamingException See callResolve.541* @return the resolved object542*/543public java.lang.Object lookup(Name name)544throws NamingException {545if (_nc == null)546throw new ConfigurationException(547"Context does not have a corresponding NamingContext");548if (name.size() == 0 )549return this; // %%% should clone() so that env can be changed550NameComponent[] path = CNNameParser.nameToCosName(name);551java.lang.Object answer = null;552553try {554answer = callResolve(path);555try {556// Check whether object factory codebase is trusted557if (CorbaUtils.isObjectFactoryTrusted(answer)) {558answer = NamingManager.getObjectInstance(559answer, name, this, _env);560}561} catch (NamingException e) {562throw e;563} catch (Exception e) {564NamingException ne = new NamingException(565"problem generating object using object factory");566ne.setRootCause(e);567throw ne;568}569} catch (CannotProceedException cpe) {570javax.naming.Context cctx = getContinuationContext(cpe);571return cctx.lookup(cpe.getRemainingName());572}573return answer;574}575576/**577* Performs bind or rebind in the context depending on whether the578* flag rebind is set. The only objects allowed to be bound are of579* types org.omg.CORBA.Object, org.omg.CosNaming.NamingContext.580* You can use a state factory to turn other objects (such as581* Remote) into these acceptable forms.582*583* Uses the COS Naming apis bind/rebind or584* bind_context/rebind_context.585* @param pth NameComponent[] object586* @param obj Object to be bound.587* @param rebind perform rebind ? if true performs a rebind.588* @exception NotFound No objects under the name.589* @exception CannotProceed Unable to obtain a continuation context590* @exception AlreadyBound An object is already bound to this name.591*/592private void callBindOrRebind(NameComponent[] pth, Name name,593java.lang.Object obj, boolean rebind) throws NamingException {594if (_nc == null)595throw new ConfigurationException(596"Context does not have a corresponding NamingContext");597try {598// Call state factories to convert599obj = NamingManager.getStateToBind(obj, name, this, _env);600601if (obj instanceof CNCtx) {602// Use naming context object reference603obj = ((CNCtx)obj)._nc;604}605606if ( obj instanceof org.omg.CosNaming.NamingContext) {607NamingContext nobj =608NamingContextHelper.narrow((org.omg.CORBA.Object)obj);609if (rebind)610_nc.rebind_context(pth,nobj);611else612_nc.bind_context(pth,nobj);613614} else if (obj instanceof org.omg.CORBA.Object) {615if (rebind)616_nc.rebind(pth,(org.omg.CORBA.Object)obj);617else618_nc.bind(pth,(org.omg.CORBA.Object)obj);619}620else621throw new IllegalArgumentException(622"Only instances of org.omg.CORBA.Object can be bound");623} catch (BAD_PARAM e) {624// probably narrow() failed?625NamingException ne = new NotContextException(name.toString());626ne.setRootCause(e);627throw ne;628} catch (Exception e) {629throw ExceptionMapper.mapException(e, this, pth);630}631}632633/**634* Converts the "Name" name into a NameComponent[] object and635* performs the bind operation. Uses callBindOrRebind. Throws an636* invalid name exception if the name is empty. We need a name to637* bind the object even when we work within the current context.638* @param name JNDI Name object639* @param obj Object to be bound.640* @exception NamingException See callBindOrRebind641*/642public void bind(Name name, java.lang.Object obj)643throws NamingException {644if (name.size() == 0 ) {645throw new InvalidNameException("Name is empty");646}647648if (debug) {649System.out.println("Bind: " + name);650}651NameComponent[] path = CNNameParser.nameToCosName(name);652653try {654callBindOrRebind(path, name, obj, false);655} catch (CannotProceedException e) {656javax.naming.Context cctx = getContinuationContext(e);657cctx.bind(e.getRemainingName(), obj);658}659}660661static private javax.naming.Context662getContinuationContext(CannotProceedException cpe)663throws NamingException {664try {665return NamingManager.getContinuationContext(cpe);666} catch (CannotProceedException e) {667java.lang.Object resObj = e.getResolvedObj();668if (resObj instanceof Reference) {669Reference ref = (Reference)resObj;670RefAddr addr = ref.get("nns");671if (addr.getContent() instanceof javax.naming.Context) {672NamingException ne = new NameNotFoundException(673"No object reference bound for specified name");674ne.setRootCause(cpe.getRootCause());675ne.setRemainingName(cpe.getRemainingName());676throw ne;677}678}679throw e;680}681}682683/**684* Converts the "String" name into a CompositeName object and685* performs the bind operation. Uses callBindOrRebind. Throws an686* invalid name exception if the name is empty.687* @param name string688* @param obj Object to be bound.689* @exception NamingException See callBindOrRebind690*/691public void bind(String name, java.lang.Object obj) throws NamingException {692bind(new CompositeName(name), obj);693}694695/**696* Converts the "Name" name into a NameComponent[] object and697* performs the rebind operation. Uses callBindOrRebind. Throws an698* invalid name exception if the name is empty. We must have a name699* to rebind the object to even if we are working within the current700* context.701* @param name string702* @param obj Object to be bound.703* @exception NamingException See callBindOrRebind704*/705public void rebind(Name name, java.lang.Object obj)706throws NamingException {707if (name.size() == 0 ) {708throw new InvalidNameException("Name is empty");709}710NameComponent[] path = CNNameParser.nameToCosName(name);711try {712callBindOrRebind(path, name, obj, true);713} catch (CannotProceedException e) {714javax.naming.Context cctx = getContinuationContext(e);715cctx.rebind(e.getRemainingName(), obj);716}717}718719/**720* Converts the "String" name into a CompositeName object and721* performs the rebind operation. Uses callBindOrRebind. Throws an722* invalid name exception if the name is an empty string.723* @param name string724* @param obj Object to be bound.725* @exception NamingException See callBindOrRebind726*/727public void rebind(String name, java.lang.Object obj)728throws NamingException {729rebind(new CompositeName(name), obj);730}731732/**733* Calls the unbind api of COS Naming and uses the exception mapper734* class to map the exceptions735* @param path NameComponent[] object736* @exception NotFound No objects under the name. If leaf737* is not found, that's OK according to the JNDI spec738* @exception CannotProceed Unable to obtain a continuation context739* @exception InvalidName Name not understood.740*/741private void callUnbind(NameComponent[] path) throws NamingException {742if (_nc == null)743throw new ConfigurationException(744"Context does not have a corresponding NamingContext");745try {746_nc.unbind(path);747} catch (NotFound e) {748// If leaf is the one missing, return success749// as per JNDI spec750751if (leafNotFound(e, path[path.length-1])) {752// do nothing753} else {754throw ExceptionMapper.mapException(e, this, path);755}756} catch (Exception e) {757throw ExceptionMapper.mapException(e, this, path);758}759}760761private boolean leafNotFound(NotFound e, NameComponent leaf) {762763// This test is not foolproof because some name servers764// always just return one component in rest_of_name765// so you might not be able to tell whether that is766// the leaf (e.g. aa/aa/aa, which one is missing?)767768NameComponent rest;769return e.why.value() == NotFoundReason._missing_node &&770e.rest_of_name.length == 1 &&771(rest=e.rest_of_name[0]).id.equals(leaf.id) &&772(rest.kind == leaf.kind ||773(rest.kind != null && rest.kind.equals(leaf.kind)));774}775776/**777* Converts the "String" name into a CompositeName object and778* performs the unbind operation. Uses callUnbind. If the name is779* empty, throws an invalid name exception. Do we unbind the780* current context (JNDI spec says work with the current context if781* the name is empty) ?782* @param name string783* @exception NamingException See callUnbind784*/785public void unbind(String name) throws NamingException {786unbind(new CompositeName(name));787}788789/**790* Converts the "Name" name into a NameComponent[] object and791* performs the unbind operation. Uses callUnbind. Throws an792* invalid name exception if the name is empty.793* @param name string794* @exception NamingException See callUnbind795*/796public void unbind(Name name)797throws NamingException {798if (name.size() == 0 )799throw new InvalidNameException("Name is empty");800NameComponent[] path = CNNameParser.nameToCosName(name);801try {802callUnbind(path);803} catch (CannotProceedException e) {804javax.naming.Context cctx = getContinuationContext(e);805cctx.unbind(e.getRemainingName());806}807}808809/**810* Renames an object. Since COS Naming does not support a rename811* api, this method unbinds the object with the "oldName" and812* creates a new binding.813* @param oldName string, existing name for the binding.814* @param newName string, name used to replace.815* @exception NamingException See bind816*/817public void rename(String oldName,String newName)818throws NamingException {819rename(new CompositeName(oldName), new CompositeName(newName));820}821822/**823* Renames an object. Since COS Naming does not support a rename824* api, this method unbinds the object with the "oldName" and825* creates a new binding.826* @param oldName JNDI Name, existing name for the binding.827* @param newName JNDI Name, name used to replace.828* @exception NamingException See bind829*/830public void rename(Name oldName,Name newName)831throws NamingException {832if (_nc == null)833throw new ConfigurationException(834"Context does not have a corresponding NamingContext");835if (oldName.size() == 0 || newName.size() == 0)836throw new InvalidNameException("One or both names empty");837java.lang.Object obj = lookup(oldName);838bind(newName,obj);839unbind(oldName);840}841842/**843* Returns a NameClassEnumeration object which has a list of name844* class pairs. Lists the current context if the name is empty.845* @param name string846* @exception NamingException All exceptions thrown by lookup847* with a non-null argument848* @return a list of name-class objects as a NameClassEnumeration.849*/850public NamingEnumeration<NameClassPair> list(String name) throws NamingException {851return list(new CompositeName(name));852}853854/**855* Returns a NameClassEnumeration object which has a list of name856* class pairs. Lists the current context if the name is empty.857* @param name JNDI Name858* @exception NamingException All exceptions thrown by lookup859* @return a list of name-class objects as a NameClassEnumeration.860*/861@SuppressWarnings("unchecked")862public NamingEnumeration<NameClassPair> list(Name name)863throws NamingException {864return (NamingEnumeration)listBindings(name);865}866867/**868* Returns a BindingEnumeration object which has a list of name869* object pairs. Lists the current context if the name is empty.870* @param name string871* @exception NamingException all exceptions returned by lookup872* @return a list of bindings as a BindingEnumeration.873*/874public NamingEnumeration<javax.naming.Binding> listBindings(String name)875throws NamingException {876return listBindings(new CompositeName(name));877}878879/**880* Returns a BindingEnumeration object which has a list of name881* class pairs. Lists the current context if the name is empty.882* @param name JNDI Name883* @exception NamingException all exceptions returned by lookup.884* @return a list of bindings as a BindingEnumeration.885*/886public NamingEnumeration<javax.naming.Binding> listBindings(Name name)887throws NamingException {888if (_nc == null)889throw new ConfigurationException(890"Context does not have a corresponding NamingContext");891if (name.size() > 0) {892try {893java.lang.Object obj = lookup(name);894if (obj instanceof CNCtx) {895return new CNBindingEnumeration(896(CNCtx) obj, true, _env);897} else {898throw new NotContextException(name.toString());899}900} catch (NamingException ne) {901throw ne;902} catch (BAD_PARAM e) {903NamingException ne =904new NotContextException(name.toString());905ne.setRootCause(e);906throw ne;907}908}909return new CNBindingEnumeration(this, false, _env);910}911912/**913* Calls the destroy on the COS Naming Server914* @param nc The NamingContext object to use.915* @exception NotEmpty when the context is not empty and cannot be destroyed.916*/917private void callDestroy(NamingContext nc)918throws NamingException {919if (_nc == null)920throw new ConfigurationException(921"Context does not have a corresponding NamingContext");922try {923nc.destroy();924} catch (Exception e) {925throw ExceptionMapper.mapException(e, this, null);926}927}928929/**930* Uses the callDestroy function to destroy the context. If name is931* empty destroys the current context.932* @param name string933* @exception OperationNotSupportedException when list is invoked934* with a non-null argument935*/936public void destroySubcontext(String name) throws NamingException {937destroySubcontext(new CompositeName(name));938}939940/**941* Uses the callDestroy function to destroy the context. Destroys942* the current context if name is empty.943* @param name JNDI Name944* @exception OperationNotSupportedException when list is invoked945* with a non-null argument946*/947public void destroySubcontext(Name name)948throws NamingException {949if (_nc == null)950throw new ConfigurationException(951"Context does not have a corresponding NamingContext");952NamingContext the_nc = _nc;953NameComponent[] path = CNNameParser.nameToCosName(name);954if ( name.size() > 0) {955try {956javax.naming.Context ctx =957(javax.naming.Context) callResolve(path);958CNCtx cnc = (CNCtx)ctx;959the_nc = cnc._nc;960cnc.close(); //remove the reference to the context961} catch (ClassCastException e) {962throw new NotContextException(name.toString());963} catch (CannotProceedException e) {964javax.naming.Context cctx = getContinuationContext(e);965cctx.destroySubcontext(e.getRemainingName());966return;967} catch (NameNotFoundException e) {968// If leaf is the one missing, return success969// as per JNDI spec970971if (e.getRootCause() instanceof NotFound &&972leafNotFound((NotFound)e.getRootCause(),973path[path.length-1])) {974return; // leaf missing OK975}976throw e;977} catch (NamingException e) {978throw e;979}980}981callDestroy(the_nc);982callUnbind(path);983}984985/**986* Calls the bind_new_context COS naming api to create a new subcontext.987* @param path NameComponent[] object988* @exception NotFound No objects under the name.989* @exception CannotProceed Unable to obtain a continuation context990* @exception InvalidName Name not understood.991* @exception AlreadyBound An object is already bound to this name.992* @return the new context object.993*/994private javax.naming.Context callBindNewContext(NameComponent[] path)995throws NamingException {996if (_nc == null)997throw new ConfigurationException(998"Context does not have a corresponding NamingContext");999try {1000NamingContext nctx = _nc.bind_new_context(path);1001return new CNCtx(_orb, orbTracker, nctx, _env,1002makeFullName(path));1003} catch (Exception e) {1004throw ExceptionMapper.mapException(e, this, path);1005}1006}10071008/**1009* Uses the callBindNewContext convenience function to create a new1010* context. Throws an invalid name exception if the name is empty.1011* @param name string1012* @exception NamingException See callBindNewContext1013* @return the new context object.1014*/1015public javax.naming.Context createSubcontext(String name)1016throws NamingException {1017return createSubcontext(new CompositeName(name));1018}10191020/**1021* Uses the callBindNewContext convenience function to create a new1022* context. Throws an invalid name exception if the name is empty.1023* @param name string1024* @exception NamingException See callBindNewContext1025* @return the new context object.1026*/1027public javax.naming.Context createSubcontext(Name name)1028throws NamingException {1029if (name.size() == 0 )1030throw new InvalidNameException("Name is empty");1031NameComponent[] path = CNNameParser.nameToCosName(name);1032try {1033return callBindNewContext(path);1034} catch (CannotProceedException e) {1035javax.naming.Context cctx = getContinuationContext(e);1036return cctx.createSubcontext(e.getRemainingName());1037}1038}10391040/**1041* Is mapped to resolve in the COS Naming api.1042* @param name string1043* @exception NamingException See lookup.1044* @return the resolved object.1045*/1046public java.lang.Object lookupLink(String name) throws NamingException {1047return lookupLink(new CompositeName(name));1048}10491050/**1051* Is mapped to resolve in the COS Naming api.1052* @param name string1053* @exception NamingException See lookup.1054* @return the resolved object.1055*/1056public java.lang.Object lookupLink(Name name) throws NamingException {1057return lookup(name);1058}10591060/**1061* Allow access to the name parser object.1062* @param String JNDI name, is ignored since there is only one Name1063* Parser object.1064* @exception NamingException --1065* @return NameParser object1066*/1067public NameParser getNameParser(String name) throws NamingException {1068return parser;1069}10701071/**1072* Allow access to the name parser object.1073* @param Name JNDI name, is ignored since there is only one Name1074* Parser object.1075* @exception NamingException --1076* @return NameParser object1077*/1078public NameParser getNameParser(Name name) throws NamingException {1079return parser;1080}10811082/**1083* Returns the current environment.1084* @return Environment.1085*/1086@SuppressWarnings("unchecked")1087public Hashtable<String, java.lang.Object> getEnvironment() throws NamingException {1088if (_env == null) {1089return new Hashtable<>(5, 0.75f);1090} else {1091return (Hashtable<String, java.lang.Object>)_env.clone();1092}1093}10941095public String composeName(String name, String prefix) throws NamingException {1096return composeName(new CompositeName(name),1097new CompositeName(prefix)).toString();1098}10991100public Name composeName(Name name, Name prefix) throws NamingException {1101Name result = (Name)prefix.clone();1102return result.addAll(name);1103}11041105/**1106* Adds to the environment for the current context.1107* Record change but do not reinitialize ORB.1108*1109* @param propName The property name.1110* @param propVal The ORB.1111* @return the previous value of this property if any.1112*/1113@SuppressWarnings("unchecked")1114public java.lang.Object addToEnvironment(String propName,1115java.lang.Object propValue)1116throws NamingException {1117if (_env == null) {1118_env = new Hashtable<>(7, 0.75f);1119} else {1120// copy-on-write1121_env = (Hashtable<String, java.lang.Object>)_env.clone();1122}11231124return _env.put(propName, propValue);1125}11261127// Record change but do not reinitialize ORB1128@SuppressWarnings("unchecked")1129public java.lang.Object removeFromEnvironment(String propName)1130throws NamingException {1131if (_env != null && _env.get(propName) != null) {1132// copy-on-write1133_env = (Hashtable<String, java.lang.Object>)_env.clone();1134return _env.remove(propName);1135}1136return null;1137}11381139synchronized public void incEnumCount() {1140enumCount++;1141if (debug) {1142System.out.println("incEnumCount, new count:" + enumCount);1143}1144}11451146synchronized public void decEnumCount()1147throws NamingException {1148enumCount--;1149if (debug) {1150System.out.println("decEnumCount, new count:" + enumCount +1151" isCloseCalled:" + isCloseCalled);1152}1153if ((enumCount == 0) && isCloseCalled) {1154close();1155}1156}11571158synchronized public void close() throws NamingException {11591160if (enumCount > 0) {1161isCloseCalled = true;1162return;1163}11641165// Never destroy an orb in CNCtx.1166// The orb we have is either the shared/default orb, or one passed in to a constructor1167// from elsewhere, so that orb is somebody else's reponsibility.1168}11691170protected void finalize() {1171try {1172close();1173} catch (NamingException e) {1174// ignore failures1175}1176}1177}117811791180