0N/A/*
5171N/A * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.jndi.cosnaming;
0N/A
0N/Aimport javax.naming.*;
0N/Aimport javax.naming.spi.NamingManager;
0N/Aimport javax.naming.spi.ResolveResult;
0N/A
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.Vector;
0N/Aimport java.net.MalformedURLException;
0N/Aimport java.net.URL;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.InputStreamReader;
0N/Aimport java.io.BufferedReader;
0N/Aimport java.io.IOException;
0N/A
0N/Aimport org.omg.CosNaming.*;
0N/Aimport org.omg.CosNaming.NamingContextPackage.*;
0N/Aimport org.omg.CORBA.*;
0N/A
0N/Aimport com.sun.jndi.toolkit.corba.CorbaUtils;
0N/A
0N/A// Needed for creating default ORB
0N/Aimport java.applet.Applet;
0N/A
0N/A/**
0N/A * Provides a bridge to the CosNaming server provided by
0N/A * JavaIDL. This class provides the InitialContext from CosNaming.
0N/A *
0N/A * @author Raj Krishnamurthy
0N/A * @author Rosanna Lee
0N/A */
0N/A
0N/Apublic class CNCtx implements javax.naming.Context {
0N/A
0N/A private final static boolean debug = false;
0N/A
5171N/A /*
5171N/A * Implement one shared ORB among all CNCtx. However, there is a public constructor
5171N/A * accepting an ORB, so we need the option of using a given ORB.
5171N/A */
5171N/A private static ORB _defaultOrb;
0N/A ORB _orb; // used by ExceptionMapper and RMI/IIOP factory
0N/A public NamingContext _nc; // public for accessing underlying NamingContext
5171N/A
5171N/A private synchronized static ORB getDefaultOrb() {
5171N/A if (_defaultOrb == null) {
5171N/A _defaultOrb = CorbaUtils.getOrb(null, -1,
5171N/A new Hashtable<String, java.lang.Object>());
5171N/A }
5171N/A return _defaultOrb;
5171N/A }
5171N/A
0N/A private NameComponent[] _name = null;
0N/A
0N/A Hashtable _env; // used by ExceptionMapper
0N/A static final CNNameParser parser = new CNNameParser();
0N/A
0N/A private static final String FED_PROP = "com.sun.jndi.cosnaming.federation";
0N/A boolean federation = false;
0N/A
0N/A // Reference counter for tracking _orb references
0N/A OrbReuseTracker orbTracker = null;
0N/A int enumCount;
0N/A boolean isCloseCalled = false;
0N/A
0N/A /**
0N/A * Create a CNCtx object. Gets the initial naming
0N/A * reference for the COS Naming Service from the ORB.
0N/A * The ORB can be passed in via the java.naming.corba.orb property
0N/A * or be created using properties in the environment properties.
0N/A * @param env Environment properties for initializing name service.
0N/A * @exception NamingException Cannot initialize ORB or naming context.
0N/A */
0N/A CNCtx(Hashtable env) throws NamingException {
0N/A if (env != null) {
0N/A env = (Hashtable) env.clone();
0N/A }
0N/A _env = env;
0N/A federation = "true".equals(env != null ? env.get(FED_PROP) : null);
0N/A initOrbAndRootContext(env);
0N/A }
0N/A
0N/A private CNCtx() {
0N/A }
0N/A
0N/A /**
0N/A * This method is used by the iiop and iiopname URL Context factories.
0N/A */
0N/A public static ResolveResult createUsingURL(String url, Hashtable env)
0N/A throws NamingException {
0N/A CNCtx ctx = new CNCtx();
0N/A if (env != null) {
0N/A env = (Hashtable) env.clone();
0N/A }
0N/A ctx._env = env;
0N/A String rest = ctx.initUsingUrl(
0N/A env != null ?
0N/A (org.omg.CORBA.ORB) env.get("java.naming.corba.orb")
0N/A : null,
0N/A url, env);
0N/A
0N/A // rest is the INS name
0N/A // Return the parsed form to prevent subsequent lookup
0N/A // from parsing the string as a composite name
5171N/A // The caller should be aware that a toString() of the name,
5171N/A // which came from the environment will yield its INS syntax,
5171N/A // rather than a composite syntax
0N/A return new ResolveResult(ctx, parser.parse(rest));
0N/A }
0N/A
0N/A /**
0N/A * Creates a CNCtx object which supports the javax.naming
0N/A * apis given a COS Naming Context object.
0N/A * @param orb The ORB used by this context
0N/A * @param tracker The ORB reuse tracker for tracking references to the
0N/A * orb object
0N/A * @param nctx The COS NamingContext object associated with this context
0N/A * @param name The name of this context relative to the root
0N/A */
0N/A
0N/A CNCtx(ORB orb, OrbReuseTracker tracker, NamingContext nctx, Hashtable env,
0N/A NameComponent[]name)
0N/A throws NamingException {
0N/A if (orb == null || nctx == null)
0N/A throw new ConfigurationException(
0N/A "Must supply ORB or NamingContext");
5171N/A if (orb != null) {
5171N/A _orb = orb;
5171N/A } else {
5171N/A _orb = getDefaultOrb();
0N/A }
0N/A _nc = nctx;
0N/A _env = env;
0N/A _name = name;
0N/A federation = "true".equals(env != null ? env.get(FED_PROP) : null);
0N/A }
0N/A
0N/A NameComponent[] makeFullName(NameComponent[] child) {
0N/A if (_name == null || _name.length == 0) {
0N/A return child;
0N/A }
0N/A NameComponent[] answer = new NameComponent[_name.length+child.length];
0N/A
0N/A // parent
0N/A System.arraycopy(_name, 0, answer, 0, _name.length);
0N/A
0N/A // child
0N/A System.arraycopy(child, 0, answer, _name.length, child.length);
0N/A return answer;
0N/A }
0N/A
0N/A
0N/A public String getNameInNamespace() throws NamingException {
0N/A if (_name == null || _name.length == 0) {
0N/A return "";
0N/A }
0N/A return CNNameParser.cosNameToInsString(_name);
0N/A }
0N/A
0N/A /**
0N/A * These are the URL schemes that need to be processed.
0N/A * IOR and corbaloc URLs can be passed directly to ORB.string_to_object()
0N/A */
0N/A private static boolean isCorbaUrl(String url) {
0N/A return url.startsWith("iiop://")
0N/A || url.startsWith("iiopname://")
0N/A || url.startsWith("corbaname:")
0N/A ;
0N/A }
0N/A
0N/A /**
0N/A * Initializes the COS Naming Service.
0N/A * This method initializes the three instance fields:
0N/A * _nc : The root naming context.
0N/A * _orb: The ORB to use for connecting RMI/IIOP stubs and for
0N/A * getting the naming context (_nc) if one was not specified
0N/A * explicitly via PROVIDER_URL.
0N/A * _name: The name of the root naming context.
0N/A *<p>
0N/A * _orb is obtained from java.naming.corba.orb if it has been set.
0N/A * Otherwise, _orb is created using the host/port from PROVIDER_URL
0N/A * (if it contains an "iiop" or "iiopname" URL), or from initialization
0N/A * properties specified in env.
0N/A *<p>
0N/A * _nc is obtained from the IOR stored in PROVIDER_URL if it has been
0N/A * set and does not contain an "iiop" or "iiopname" URL. It can be
0N/A * a stringified IOR, "corbaloc" URL, "corbaname" URL,
0N/A * or a URL (such as file/http/ftp) to a location
0N/A * containing a stringified IOR. If PROVIDER_URL has not been
0N/A * set in this way, it is obtained from the result of
0N/A * ORB.resolve_initial_reference("NameService");
0N/A *<p>
0N/A * _name is obtained from the "iiop", "iiopname", or "corbaname" URL.
0N/A * It is the empty name by default.
0N/A *
0N/A * @param env Environment The possibly null environment.
0N/A * @exception NamingException When an error occurs while initializing the
0N/A * ORB or the naming context.
0N/A */
0N/A private void initOrbAndRootContext(Hashtable env) throws NamingException {
0N/A org.omg.CORBA.ORB inOrb = null;
0N/A String ncIor = null;
0N/A
5171N/A if (inOrb == null && env != null) {
0N/A inOrb = (org.omg.CORBA.ORB) env.get("java.naming.corba.orb");
0N/A }
0N/A
5171N/A if (inOrb == null)
5171N/A inOrb = getDefaultOrb(); // will create a default ORB if none exists
5171N/A
0N/A // Extract PROVIDER_URL from environment
0N/A String provUrl = null;
0N/A if (env != null) {
0N/A provUrl = (String)env.get(javax.naming.Context.PROVIDER_URL);
0N/A }
0N/A
0N/A if (provUrl != null && !isCorbaUrl(provUrl)) {
0N/A // Initialize the root naming context by using the IOR supplied
0N/A // in the PROVIDER_URL
0N/A ncIor = getStringifiedIor(provUrl);
0N/A setOrbAndRootContext(inOrb, ncIor);
0N/A } else if (provUrl != null) {
0N/A // Initialize the root naming context by using the URL supplied
0N/A // in the PROVIDER_URL
0N/A String insName = initUsingUrl(inOrb, provUrl, env);
0N/A
0N/A // If name supplied in URL, resolve it to a NamingContext
0N/A if (insName.length() > 0) {
0N/A _name = parser.nameToCosName(parser.parse(insName));
0N/A try {
0N/A org.omg.CORBA.Object obj = _nc.resolve(_name);
0N/A _nc = NamingContextHelper.narrow(obj);
0N/A if (_nc == null) {
0N/A throw new ConfigurationException(insName +
0N/A " does not name a NamingContext");
0N/A }
0N/A } catch (org.omg.CORBA.BAD_PARAM e) {
0N/A throw new ConfigurationException(insName +
0N/A " does not name a NamingContext");
0N/A } catch (Exception e) {
0N/A throw ExceptionMapper.mapException(e, this, _name);
0N/A }
0N/A }
0N/A } else {
0N/A // No PROVIDER_URL supplied; initialize using defaults
5171N/A if (debug) {
5171N/A System.err.println("Getting default ORB: " + inOrb + env);
0N/A }
0N/A setOrbAndRootContext(inOrb, (String)null);
0N/A }
0N/A }
0N/A
0N/A
0N/A private String initUsingUrl(ORB orb, String url, Hashtable env)
0N/A throws NamingException {
0N/A if (url.startsWith("iiop://") || url.startsWith("iiopname://")) {
0N/A return initUsingIiopUrl(orb, url, env);
0N/A } else {
0N/A return initUsingCorbanameUrl(orb, url, env);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Handles "iiop" and "iiopname" URLs (INS 98-10-11)
0N/A */
0N/A private String initUsingIiopUrl(ORB defOrb, String url, Hashtable env)
0N/A throws NamingException {
5171N/A
5171N/A if (defOrb == null)
5171N/A defOrb = getDefaultOrb();
5171N/A
0N/A try {
0N/A IiopUrl parsedUrl = new IiopUrl(url);
0N/A
0N/A Vector addrs = parsedUrl.getAddresses();
0N/A IiopUrl.Address addr;
0N/A NamingException savedException = null;
0N/A
0N/A for (int i = 0; i < addrs.size(); i++) {
0N/A addr = (IiopUrl.Address)addrs.elementAt(i);
0N/A
0N/A try {
5171N/A try {
5171N/A String tmpUrl = "corbaloc:iiop:" + addr.host
5171N/A + ":" + addr.port + "/NameService";
5171N/A if (debug) {
5171N/A System.err.println("Using url: " + tmpUrl);
5171N/A }
5171N/A org.omg.CORBA.Object rootCtx =
5171N/A defOrb.string_to_object(tmpUrl);
5171N/A setOrbAndRootContext(defOrb, rootCtx);
5171N/A return parsedUrl.getStringName();
5171N/A } catch (Exception e) {} // keep going
0N/A
0N/A // Get ORB
0N/A if (debug) {
0N/A System.err.println("Getting ORB for " + addr.host
0N/A + " and port " + addr.port);
0N/A }
0N/A
0N/A // Assign to fields
5171N/A setOrbAndRootContext(defOrb, (String)null);
0N/A return parsedUrl.getStringName();
0N/A
0N/A } catch (NamingException ne) {
0N/A savedException = ne;
0N/A }
0N/A }
0N/A if (savedException != null) {
0N/A throw savedException;
0N/A } else {
0N/A throw new ConfigurationException("Problem with URL: " + url);
0N/A }
0N/A } catch (MalformedURLException e) {
0N/A throw new ConfigurationException(e.getMessage());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Initializes using "corbaname" URL (INS 99-12-03)
0N/A */
0N/A private String initUsingCorbanameUrl(ORB orb, String url, Hashtable env)
0N/A throws NamingException {
5171N/A
5171N/A if (orb == null)
5171N/A orb = getDefaultOrb();
5171N/A
0N/A try {
0N/A CorbanameUrl parsedUrl = new CorbanameUrl(url);
0N/A
0N/A String corbaloc = parsedUrl.getLocation();
0N/A String cosName = parsedUrl.getStringName();
0N/A
0N/A setOrbAndRootContext(orb, corbaloc);
0N/A
0N/A return parsedUrl.getStringName();
0N/A } catch (MalformedURLException e) {
0N/A throw new ConfigurationException(e.getMessage());
0N/A }
0N/A }
0N/A
0N/A private void setOrbAndRootContext(ORB orb, String ncIor)
0N/A throws NamingException {
0N/A _orb = orb;
0N/A try {
0N/A org.omg.CORBA.Object ncRef;
0N/A if (ncIor != null) {
0N/A if (debug) {
0N/A System.err.println("Passing to string_to_object: " + ncIor);
0N/A }
0N/A ncRef = _orb.string_to_object(ncIor);
0N/A } else {
0N/A ncRef = _orb.resolve_initial_references("NameService");
0N/A }
0N/A if (debug) {
0N/A System.err.println("Naming Context Ref: " + ncRef);
0N/A }
0N/A _nc = NamingContextHelper.narrow(ncRef);
0N/A if (_nc == null) {
0N/A if (ncIor != null) {
0N/A throw new ConfigurationException(
0N/A "Cannot convert IOR to a NamingContext: " + ncIor);
0N/A } else {
0N/A throw new ConfigurationException(
0N/A"ORB.resolve_initial_references(\"NameService\") does not return a NamingContext");
0N/A }
0N/A }
0N/A } catch (org.omg.CORBA.ORBPackage.InvalidName in) {
0N/A NamingException ne =
0N/A new ConfigurationException(
0N/A"COS Name Service not registered with ORB under the name 'NameService'");
0N/A ne.setRootCause(in);
0N/A throw ne;
0N/A } catch (org.omg.CORBA.COMM_FAILURE e) {
0N/A NamingException ne =
0N/A new CommunicationException("Cannot connect to ORB");
0N/A ne.setRootCause(e);
0N/A throw ne;
0N/A } catch (org.omg.CORBA.BAD_PARAM e) {
0N/A NamingException ne = new ConfigurationException(
0N/A "Invalid URL or IOR: " + ncIor);
0N/A ne.setRootCause(e);
0N/A throw ne;
0N/A } catch (org.omg.CORBA.INV_OBJREF e) {
0N/A NamingException ne = new ConfigurationException(
0N/A "Invalid object reference: " + ncIor);
0N/A ne.setRootCause(e);
0N/A throw ne;
0N/A }
0N/A }
0N/A
0N/A private void setOrbAndRootContext(ORB orb, org.omg.CORBA.Object ncRef)
0N/A throws NamingException {
0N/A _orb = orb;
0N/A try {
0N/A _nc = NamingContextHelper.narrow(ncRef);
0N/A if (_nc == null) {
0N/A throw new ConfigurationException(
0N/A "Cannot convert object reference to NamingContext: " + ncRef);
0N/A }
0N/A } catch (org.omg.CORBA.COMM_FAILURE e) {
0N/A NamingException ne =
0N/A new CommunicationException("Cannot connect to ORB");
0N/A ne.setRootCause(e);
0N/A throw ne;
0N/A }
0N/A }
0N/A
0N/A private String getStringifiedIor(String url) throws NamingException {
0N/A if (url.startsWith("IOR:") || url.startsWith("corbaloc:")) {
0N/A return url;
0N/A } else {
0N/A InputStream in = null;
0N/A try {
0N/A URL u = new URL(url);
0N/A in = u.openStream();
0N/A if (in != null) {
0N/A BufferedReader bufin =
0N/A new BufferedReader(new InputStreamReader(in, "8859_1"));
0N/A String str;
0N/A while ((str = bufin.readLine()) != null) {
0N/A if (str.startsWith("IOR:")) {
0N/A return str;
0N/A }
0N/A }
0N/A }
0N/A } catch (IOException e) {
0N/A NamingException ne =
0N/A new ConfigurationException("Invalid URL: " + url);
0N/A ne.setRootCause(e);
0N/A throw ne;
0N/A } finally {
0N/A try {
0N/A if (in != null) {
0N/A in.close();
0N/A }
0N/A } catch (IOException e) {
0N/A NamingException ne =
0N/A new ConfigurationException("Invalid URL: " + url);
0N/A ne.setRootCause(e);
0N/A throw ne;
0N/A }
0N/A }
0N/A throw new ConfigurationException(url + " does not contain an IOR");
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Does the job of calling the COS Naming API,
0N/A * resolve, and performs the exception mapping. If the resolved
0N/A * object is a COS Naming Context (sub-context), then this function
0N/A * returns a new JNDI naming context object.
0N/A * @param path the NameComponent[] object.
0N/A * @exception NotFound No objects under the name.
0N/A * @exception CannotProceed Unable to obtain a continuation context
0N/A * @exception InvalidName Name not understood.
0N/A * @return Resolved object returned by the COS Name Server.
0N/A */
0N/A java.lang.Object callResolve(NameComponent[] path)
0N/A throws NamingException {
0N/A try {
0N/A org.omg.CORBA.Object obj = _nc.resolve(path);
0N/A try {
0N/A NamingContext nc =
0N/A NamingContextHelper.narrow(obj);
0N/A if (nc != null) {
0N/A return new CNCtx(_orb, orbTracker, nc, _env,
0N/A makeFullName(path));
0N/A } else {
0N/A return obj;
0N/A }
0N/A } catch (org.omg.CORBA.SystemException e) {
0N/A return obj;
0N/A }
0N/A } catch (Exception e) {
0N/A throw ExceptionMapper.mapException(e, this, path);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Converts the "String" name into a CompositeName
0N/A * returns the object resolved by the COS Naming api,
0N/A * resolve. Returns the current context if the name is empty.
0N/A * Returns either an org.omg.CORBA.Object or javax.naming.Context object.
0N/A * @param name string used to resolve the object.
0N/A * @exception NamingException See callResolve.
0N/A * @return the resolved object
0N/A */
0N/A public java.lang.Object lookup(String name) throws NamingException {
0N/A if (debug) {
0N/A System.out.println("Looking up: " + name);
0N/A }
0N/A return lookup(new CompositeName(name));
0N/A }
0N/A
0N/A /**
0N/A * Converts the "Name" name into a NameComponent[] object and
0N/A * returns the object resolved by the COS Naming api,
0N/A * resolve. Returns the current context if the name is empty.
0N/A * Returns either an org.omg.CORBA.Object or javax.naming.Context object.
0N/A * @param name JNDI Name used to resolve the object.
0N/A * @exception NamingException See callResolve.
0N/A * @return the resolved object
0N/A */
0N/A public java.lang.Object lookup(Name name)
0N/A throws NamingException {
0N/A if (_nc == null)
0N/A throw new ConfigurationException(
0N/A "Context does not have a corresponding NamingContext");
0N/A if (name.size() == 0 )
0N/A return this; // %%% should clone() so that env can be changed
0N/A NameComponent[] path = CNNameParser.nameToCosName(name);
0N/A
0N/A try {
0N/A java.lang.Object answer = callResolve(path);
0N/A
0N/A try {
0N/A return NamingManager.getObjectInstance(answer, name, this, _env);
0N/A } catch (NamingException e) {
0N/A throw e;
0N/A } catch (Exception e) {
0N/A NamingException ne = new NamingException(
0N/A "problem generating object using object factory");
0N/A ne.setRootCause(e);
0N/A throw ne;
0N/A }
0N/A } catch (CannotProceedException cpe) {
0N/A javax.naming.Context cctx = getContinuationContext(cpe);
0N/A return cctx.lookup(cpe.getRemainingName());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Performs bind or rebind in the context depending on whether the
0N/A * flag rebind is set. The only objects allowed to be bound are of
0N/A * types org.omg.CORBA.Object, org.omg.CosNaming.NamingContext.
0N/A * You can use a state factory to turn other objects (such as
0N/A * Remote) into these acceptable forms.
0N/A *
0N/A * Uses the COS Naming apis bind/rebind or
0N/A * bind_context/rebind_context.
0N/A * @param pth NameComponent[] object
0N/A * @param obj Object to be bound.
0N/A * @param rebind perform rebind ? if true performs a rebind.
0N/A * @exception NotFound No objects under the name.
0N/A * @exception CannotProceed Unable to obtain a continuation context
0N/A * @exception AlreadyBound An object is already bound to this name.
0N/A */
0N/A private void callBindOrRebind(NameComponent[] pth, Name name,
0N/A java.lang.Object obj, boolean rebind) throws NamingException {
0N/A if (_nc == null)
0N/A throw new ConfigurationException(
0N/A "Context does not have a corresponding NamingContext");
0N/A try {
0N/A // Call state factories to convert
0N/A obj = NamingManager.getStateToBind(obj, name, this, _env);
0N/A
0N/A if (obj instanceof CNCtx) {
0N/A // Use naming context object reference
0N/A obj = ((CNCtx)obj)._nc;
0N/A }
0N/A
0N/A if ( obj instanceof org.omg.CosNaming.NamingContext) {
0N/A NamingContext nobj =
0N/A NamingContextHelper.narrow((org.omg.CORBA.Object)obj);
0N/A if (rebind)
0N/A _nc.rebind_context(pth,nobj);
0N/A else
0N/A _nc.bind_context(pth,nobj);
0N/A
0N/A } else if (obj instanceof org.omg.CORBA.Object) {
0N/A if (rebind)
0N/A _nc.rebind(pth,(org.omg.CORBA.Object)obj);
0N/A else
0N/A _nc.bind(pth,(org.omg.CORBA.Object)obj);
0N/A }
0N/A else
0N/A throw new IllegalArgumentException(
0N/A "Only instances of org.omg.CORBA.Object can be bound");
0N/A } catch (BAD_PARAM e) {
0N/A // probably narrow() failed?
0N/A NamingException ne = new NotContextException(name.toString());
0N/A ne.setRootCause(e);
0N/A throw ne;
0N/A } catch (Exception e) {
0N/A throw ExceptionMapper.mapException(e, this, pth);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Converts the "Name" name into a NameComponent[] object and
0N/A * performs the bind operation. Uses callBindOrRebind. Throws an
0N/A * invalid name exception if the name is empty. We need a name to
0N/A * bind the object even when we work within the current context.
0N/A * @param name JNDI Name object
0N/A * @param obj Object to be bound.
0N/A * @exception NamingException See callBindOrRebind
0N/A */
0N/A public void bind(Name name, java.lang.Object obj)
0N/A throws NamingException {
0N/A if (name.size() == 0 ) {
0N/A throw new InvalidNameException("Name is empty");
0N/A }
0N/A
0N/A if (debug) {
0N/A System.out.println("Bind: " + name);
0N/A }
0N/A NameComponent[] path = CNNameParser.nameToCosName(name);
0N/A
0N/A try {
0N/A callBindOrRebind(path, name, obj, false);
0N/A } catch (CannotProceedException e) {
0N/A javax.naming.Context cctx = getContinuationContext(e);
0N/A cctx.bind(e.getRemainingName(), obj);
0N/A }
0N/A }
0N/A
0N/A static private javax.naming.Context
0N/A getContinuationContext(CannotProceedException cpe)
0N/A throws NamingException {
0N/A try {
0N/A return NamingManager.getContinuationContext(cpe);
0N/A } catch (CannotProceedException e) {
0N/A java.lang.Object resObj = e.getResolvedObj();
0N/A if (resObj instanceof Reference) {
0N/A Reference ref = (Reference)resObj;
0N/A RefAddr addr = ref.get("nns");
0N/A if (addr.getContent() instanceof javax.naming.Context) {
0N/A NamingException ne = new NameNotFoundException(
0N/A "No object reference bound for specified name");
0N/A ne.setRootCause(cpe.getRootCause());
0N/A ne.setRemainingName(cpe.getRemainingName());
0N/A throw ne;
0N/A }
0N/A }
0N/A throw e;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Converts the "String" name into a CompositeName object and
0N/A * performs the bind operation. Uses callBindOrRebind. Throws an
0N/A * invalid name exception if the name is empty.
0N/A * @param name string
0N/A * @param obj Object to be bound.
0N/A * @exception NamingException See callBindOrRebind
0N/A */
0N/A public void bind(String name, java.lang.Object obj) throws NamingException {
0N/A bind(new CompositeName(name), obj);
0N/A }
0N/A
0N/A /**
0N/A * Converts the "Name" name into a NameComponent[] object and
0N/A * performs the rebind operation. Uses callBindOrRebind. Throws an
0N/A * invalid name exception if the name is empty. We must have a name
0N/A * to rebind the object to even if we are working within the current
0N/A * context.
0N/A * @param name string
0N/A * @param obj Object to be bound.
0N/A * @exception NamingException See callBindOrRebind
0N/A */
0N/A public void rebind(Name name, java.lang.Object obj)
0N/A throws NamingException {
0N/A if (name.size() == 0 ) {
0N/A throw new InvalidNameException("Name is empty");
0N/A }
0N/A NameComponent[] path = CNNameParser.nameToCosName(name);
0N/A try {
0N/A callBindOrRebind(path, name, obj, true);
0N/A } catch (CannotProceedException e) {
0N/A javax.naming.Context cctx = getContinuationContext(e);
0N/A cctx.rebind(e.getRemainingName(), obj);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Converts the "String" name into a CompositeName object and
0N/A * performs the rebind operation. Uses callBindOrRebind. Throws an
0N/A * invalid name exception if the name is an empty string.
0N/A * @param name string
0N/A * @param obj Object to be bound.
0N/A * @exception NamingException See callBindOrRebind
0N/A */
0N/A public void rebind(String name, java.lang.Object obj)
0N/A throws NamingException {
0N/A rebind(new CompositeName(name), obj);
0N/A }
0N/A
0N/A /**
0N/A * Calls the unbind api of COS Naming and uses the exception mapper
0N/A * class to map the exceptions
0N/A * @param path NameComponent[] object
0N/A * @exception NotFound No objects under the name. If leaf
0N/A * is not found, that's OK according to the JNDI spec
0N/A * @exception CannotProceed Unable to obtain a continuation context
0N/A * @exception InvalidName Name not understood.
0N/A */
0N/A private void callUnbind(NameComponent[] path) throws NamingException {
0N/A if (_nc == null)
0N/A throw new ConfigurationException(
0N/A "Context does not have a corresponding NamingContext");
0N/A try {
0N/A _nc.unbind(path);
0N/A } catch (NotFound e) {
0N/A // If leaf is the one missing, return success
0N/A // as per JNDI spec
0N/A
0N/A if (leafNotFound(e, path[path.length-1])) {
0N/A ; // do nothing
0N/A } else {
0N/A throw ExceptionMapper.mapException(e, this, path);
0N/A }
0N/A } catch (Exception e) {
0N/A throw ExceptionMapper.mapException(e, this, path);
0N/A }
0N/A }
0N/A
0N/A private boolean leafNotFound(NotFound e, NameComponent leaf) {
0N/A
0N/A // This test is not foolproof because some name servers
0N/A // always just return one component in rest_of_name
0N/A // so you might not be able to tell whether that is
0N/A // the leaf (e.g. aa/aa/aa, which one is missing?)
0N/A
0N/A NameComponent rest;
0N/A return e.why.value() == NotFoundReason._missing_node &&
0N/A e.rest_of_name.length == 1 &&
0N/A (rest=e.rest_of_name[0]).id.equals(leaf.id) &&
0N/A (rest.kind == leaf.kind ||
0N/A (rest.kind != null && rest.kind.equals(leaf.kind)));
0N/A }
0N/A
0N/A /**
0N/A * Converts the "String" name into a CompositeName object and
0N/A * performs the unbind operation. Uses callUnbind. If the name is
0N/A * empty, throws an invalid name exception. Do we unbind the
0N/A * current context (JNDI spec says work with the current context if
0N/A * the name is empty) ?
0N/A * @param name string
0N/A * @exception NamingException See callUnbind
0N/A */
0N/A public void unbind(String name) throws NamingException {
0N/A unbind(new CompositeName(name));
0N/A }
0N/A
0N/A /**
0N/A * Converts the "Name" name into a NameComponent[] object and
0N/A * performs the unbind operation. Uses callUnbind. Throws an
0N/A * invalid name exception if the name is empty.
0N/A * @param name string
0N/A * @exception NamingException See callUnbind
0N/A */
0N/A public void unbind(Name name)
0N/A throws NamingException {
0N/A if (name.size() == 0 )
0N/A throw new InvalidNameException("Name is empty");
0N/A NameComponent[] path = CNNameParser.nameToCosName(name);
0N/A try {
0N/A callUnbind(path);
0N/A } catch (CannotProceedException e) {
0N/A javax.naming.Context cctx = getContinuationContext(e);
0N/A cctx.unbind(e.getRemainingName());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Renames an object. Since COS Naming does not support a rename
0N/A * api, this method unbinds the object with the "oldName" and
0N/A * creates a new binding.
0N/A * @param oldName string, existing name for the binding.
0N/A * @param newName string, name used to replace.
0N/A * @exception NamingException See bind
0N/A */
0N/A public void rename(String oldName,String newName)
0N/A throws NamingException {
0N/A rename(new CompositeName(oldName), new CompositeName(newName));
0N/A }
0N/A
0N/A /**
0N/A * Renames an object. Since COS Naming does not support a rename
0N/A * api, this method unbinds the object with the "oldName" and
0N/A * creates a new binding.
0N/A * @param oldName JNDI Name, existing name for the binding.
0N/A * @param newName JNDI Name, name used to replace.
0N/A * @exception NamingException See bind
0N/A */
0N/A public void rename(Name oldName,Name newName)
0N/A throws NamingException {
0N/A if (_nc == null)
0N/A throw new ConfigurationException(
0N/A "Context does not have a corresponding NamingContext");
0N/A if (oldName.size() == 0 || newName.size() == 0)
0N/A throw new InvalidNameException("One or both names empty");
0N/A java.lang.Object obj = lookup(oldName);
0N/A bind(newName,obj);
0N/A unbind(oldName);
0N/A }
0N/A
0N/A /**
0N/A * Returns a NameClassEnumeration object which has a list of name
0N/A * class pairs. Lists the current context if the name is empty.
0N/A * @param name string
0N/A * @exception NamingException All exceptions thrown by lookup
0N/A * with a non-null argument
0N/A * @return a list of name-class objects as a NameClassEnumeration.
0N/A */
0N/A public NamingEnumeration list(String name) throws NamingException {
0N/A return list(new CompositeName(name));
0N/A }
0N/A
0N/A /**
0N/A * Returns a NameClassEnumeration object which has a list of name
0N/A * class pairs. Lists the current context if the name is empty.
0N/A * @param name JNDI Name
0N/A * @exception NamingException All exceptions thrown by lookup
0N/A * @return a list of name-class objects as a NameClassEnumeration.
0N/A */
0N/A public NamingEnumeration list(Name name)
0N/A throws NamingException {
0N/A return listBindings(name);
0N/A }
0N/A
0N/A /**
0N/A * Returns a BindingEnumeration object which has a list of name
0N/A * object pairs. Lists the current context if the name is empty.
0N/A * @param name string
0N/A * @exception NamingException all exceptions returned by lookup
0N/A * @return a list of bindings as a BindingEnumeration.
0N/A */
0N/A public NamingEnumeration listBindings(String name)
0N/A throws NamingException {
0N/A return listBindings(new CompositeName(name));
0N/A }
0N/A
0N/A /**
0N/A * Returns a BindingEnumeration object which has a list of name
0N/A * class pairs. Lists the current context if the name is empty.
0N/A * @param name JNDI Name
0N/A * @exception NamingException all exceptions returned by lookup.
0N/A * @return a list of bindings as a BindingEnumeration.
0N/A */
0N/A public NamingEnumeration listBindings(Name name)
0N/A throws NamingException {
0N/A if (_nc == null)
0N/A throw new ConfigurationException(
0N/A "Context does not have a corresponding NamingContext");
0N/A if (name.size() > 0) {
0N/A try {
0N/A java.lang.Object obj = lookup(name);
0N/A if (obj instanceof CNCtx) {
0N/A return new CNBindingEnumeration(
0N/A (CNCtx) obj, true, _env);
0N/A } else {
0N/A throw new NotContextException(name.toString());
0N/A }
0N/A } catch (NamingException ne) {
0N/A throw ne;
0N/A } catch (BAD_PARAM e) {
0N/A NamingException ne =
0N/A new NotContextException(name.toString());
0N/A ne.setRootCause(e);
0N/A throw ne;
0N/A }
0N/A }
0N/A return new CNBindingEnumeration(this, false, _env);
0N/A }
0N/A
0N/A /**
0N/A * Calls the destroy on the COS Naming Server
0N/A * @param nc The NamingContext object to use.
0N/A * @exception NotEmpty when the context is not empty and cannot be destroyed.
0N/A */
0N/A private void callDestroy(NamingContext nc)
0N/A throws NamingException {
0N/A if (_nc == null)
0N/A throw new ConfigurationException(
0N/A "Context does not have a corresponding NamingContext");
0N/A try {
0N/A nc.destroy();
0N/A } catch (Exception e) {
0N/A throw ExceptionMapper.mapException(e, this, null);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Uses the callDestroy function to destroy the context. If name is
0N/A * empty destroys the current context.
0N/A * @param name string
0N/A * @exception OperationNotSupportedException when list is invoked
0N/A * with a non-null argument
0N/A */
0N/A public void destroySubcontext(String name) throws NamingException {
0N/A destroySubcontext(new CompositeName(name));
0N/A }
0N/A
0N/A /**
0N/A * Uses the callDestroy function to destroy the context. Destroys
0N/A * the current context if name is empty.
0N/A * @param name JNDI Name
0N/A * @exception OperationNotSupportedException when list is invoked
0N/A * with a non-null argument
0N/A */
0N/A public void destroySubcontext(Name name)
0N/A throws NamingException {
0N/A if (_nc == null)
0N/A throw new ConfigurationException(
0N/A "Context does not have a corresponding NamingContext");
0N/A NamingContext the_nc = _nc;
0N/A NameComponent[] path = CNNameParser.nameToCosName(name);
0N/A if ( name.size() > 0) {
0N/A try {
0N/A javax.naming.Context ctx =
0N/A (javax.naming.Context) callResolve(path);
0N/A CNCtx cnc = (CNCtx)ctx;
0N/A the_nc = cnc._nc;
0N/A cnc.close(); //remove the reference to the context
0N/A } catch (ClassCastException e) {
0N/A throw new NotContextException(name.toString());
0N/A } catch (CannotProceedException e) {
0N/A javax.naming.Context cctx = getContinuationContext(e);
0N/A cctx.destroySubcontext(e.getRemainingName());
0N/A return;
0N/A } catch (NameNotFoundException e) {
0N/A // If leaf is the one missing, return success
0N/A // as per JNDI spec
0N/A
0N/A if (e.getRootCause() instanceof NotFound &&
0N/A leafNotFound((NotFound)e.getRootCause(),
0N/A path[path.length-1])) {
0N/A return; // leaf missing OK
0N/A }
0N/A throw e;
0N/A } catch (NamingException e) {
0N/A throw e;
0N/A }
0N/A }
0N/A callDestroy(the_nc);
0N/A callUnbind(path);
0N/A }
0N/A
0N/A /**
0N/A * Calls the bind_new_context COS naming api to create a new subcontext.
0N/A * @param path NameComponent[] object
0N/A * @exception NotFound No objects under the name.
0N/A * @exception CannotProceed Unable to obtain a continuation context
0N/A * @exception InvalidName Name not understood.
0N/A * @exception AlreadyBound An object is already bound to this name.
0N/A * @return the new context object.
0N/A */
0N/A private javax.naming.Context callBindNewContext(NameComponent[] path)
0N/A throws NamingException {
0N/A if (_nc == null)
0N/A throw new ConfigurationException(
0N/A "Context does not have a corresponding NamingContext");
0N/A try {
0N/A NamingContext nctx = _nc.bind_new_context(path);
0N/A return new CNCtx(_orb, orbTracker, nctx, _env,
0N/A makeFullName(path));
0N/A } catch (Exception e) {
0N/A throw ExceptionMapper.mapException(e, this, path);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Uses the callBindNewContext convenience function to create a new
0N/A * context. Throws an invalid name exception if the name is empty.
0N/A * @param name string
0N/A * @exception NamingException See callBindNewContext
0N/A * @return the new context object.
0N/A */
0N/A public javax.naming.Context createSubcontext(String name)
0N/A throws NamingException {
0N/A return createSubcontext(new CompositeName(name));
0N/A }
0N/A
0N/A /**
0N/A * Uses the callBindNewContext convenience function to create a new
0N/A * context. Throws an invalid name exception if the name is empty.
0N/A * @param name string
0N/A * @exception NamingException See callBindNewContext
0N/A * @return the new context object.
0N/A */
0N/A public javax.naming.Context createSubcontext(Name name)
0N/A throws NamingException {
0N/A if (name.size() == 0 )
0N/A throw new InvalidNameException("Name is empty");
0N/A NameComponent[] path = CNNameParser.nameToCosName(name);
0N/A try {
0N/A return callBindNewContext(path);
0N/A } catch (CannotProceedException e) {
0N/A javax.naming.Context cctx = getContinuationContext(e);
0N/A return cctx.createSubcontext(e.getRemainingName());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Is mapped to resolve in the COS Naming api.
0N/A * @param name string
0N/A * @exception NamingException See lookup.
0N/A * @return the resolved object.
0N/A */
0N/A public java.lang.Object lookupLink(String name) throws NamingException {
0N/A return lookupLink(new CompositeName(name));
0N/A }
0N/A
0N/A /**
0N/A * Is mapped to resolve in the COS Naming api.
0N/A * @param name string
0N/A * @exception NamingException See lookup.
0N/A * @return the resolved object.
0N/A */
0N/A public java.lang.Object lookupLink(Name name) throws NamingException {
0N/A return lookup(name);
0N/A }
0N/A
0N/A /**
0N/A * Allow access to the name parser object.
0N/A * @param String JNDI name, is ignored since there is only one Name
0N/A * Parser object.
0N/A * @exception NamingException --
0N/A * @return NameParser object
0N/A */
0N/A public NameParser getNameParser(String name) throws NamingException {
0N/A return parser;
0N/A }
0N/A
0N/A /**
0N/A * Allow access to the name parser object.
0N/A * @param Name JNDI name, is ignored since there is only one Name
0N/A * Parser object.
0N/A * @exception NamingException --
0N/A * @return NameParser object
0N/A */
0N/A public NameParser getNameParser(Name name) throws NamingException {
0N/A return parser;
0N/A }
0N/A
0N/A /**
0N/A * Returns the current environment.
0N/A * @return Environment.
0N/A */
0N/A public Hashtable getEnvironment() throws NamingException {
0N/A if (_env == null) {
0N/A return new Hashtable(5, 0.75f);
0N/A } else {
0N/A return (Hashtable)_env.clone();
0N/A }
0N/A }
0N/A
0N/A public String composeName(String name, String prefix) throws NamingException {
0N/A return composeName(new CompositeName(name),
0N/A new CompositeName(prefix)).toString();
0N/A }
0N/A
0N/A public Name composeName(Name name, Name prefix) throws NamingException {
0N/A Name result = (Name)prefix.clone();
0N/A return result.addAll(name);
0N/A }
0N/A
0N/A /**
0N/A * Adds to the environment for the current context.
0N/A * Record change but do not reinitialize ORB.
0N/A *
0N/A * @param propName The property name.
0N/A * @param propVal The ORB.
0N/A * @return the previous value of this property if any.
0N/A */
0N/A public java.lang.Object addToEnvironment(String propName,
0N/A java.lang.Object propValue)
0N/A throws NamingException {
0N/A if (_env == null) {
0N/A _env = new Hashtable(7, 0.75f);
0N/A } else {
0N/A // copy-on-write
0N/A _env = (Hashtable)_env.clone();
0N/A }
0N/A
0N/A return _env.put(propName, propValue);
0N/A }
0N/A
0N/A // Record change but do not reinitialize ORB
0N/A public java.lang.Object removeFromEnvironment(String propName)
0N/A throws NamingException {
0N/A if (_env != null && _env.get(propName) != null) {
0N/A // copy-on-write
0N/A _env = (Hashtable)_env.clone();
0N/A return _env.remove(propName);
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A synchronized public void incEnumCount() {
0N/A enumCount++;
0N/A if (debug) {
0N/A System.out.println("incEnumCount, new count:" + enumCount);
0N/A }
0N/A }
0N/A
0N/A synchronized public void decEnumCount()
0N/A throws NamingException {
0N/A enumCount--;
0N/A if (debug) {
0N/A System.out.println("decEnumCount, new count:" + enumCount +
0N/A " isCloseCalled:" + isCloseCalled);
0N/A }
0N/A if ((enumCount == 0) && isCloseCalled) {
0N/A close();
0N/A }
0N/A }
0N/A
0N/A synchronized public void close() throws NamingException {
5171N/A
0N/A if (enumCount > 0) {
0N/A isCloseCalled = true;
0N/A return;
0N/A }
5171N/A
5171N/A // Never destroy an orb in CNCtx.
5171N/A // The orb we have is either the shared/default orb, or one passed in to a constructor
5171N/A // from elsewhere, so that orb is somebody else's reponsibility.
0N/A }
0N/A
0N/A protected void finalize() {
0N/A try {
0N/A close();
0N/A } catch (NamingException e) {
0N/A // ignore failures
0N/A }
0N/A }
0N/A}