0N/A/*
2362N/A * Copyright (c) 1999, 2005, 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.directory.*;
0N/Aimport javax.naming.spi.*;
0N/A
0N/Aimport org.omg.CosNaming.*;
0N/Aimport org.omg.CosNaming.NamingContextPackage.*;
0N/Aimport org.omg.CORBA.*;
0N/A
0N/A/**
0N/A * A convenience class to map the COS Naming exceptions to the JNDI exceptions.
0N/A * @author Raj Krishnamurthy
0N/A */
0N/A
0N/Apublic final class ExceptionMapper {
0N/A private ExceptionMapper() {} // ensure no instance
0N/A private static final boolean debug = false;
0N/A
0N/A public static final NamingException mapException(Exception e,
0N/A CNCtx ctx, NameComponent[] inputName) throws NamingException {
0N/A if (e instanceof NamingException) {
0N/A return (NamingException)e;
0N/A }
0N/A
0N/A if (e instanceof RuntimeException) {
0N/A throw (RuntimeException)e;
0N/A }
0N/A
0N/A NamingException ne;
0N/A if (e instanceof NotFound) {
0N/A if (ctx.federation) {
0N/A return tryFed((NotFound)e, ctx, inputName);
0N/A
0N/A } else {
0N/A ne = new NameNotFoundException();
0N/A }
0N/A
0N/A } else if (e instanceof CannotProceed) {
0N/A
0N/A ne = new CannotProceedException();
0N/A NamingContext nc = ((CannotProceed) e).cxt;
0N/A NameComponent[] rest = ((CannotProceed) e).rest_of_name;
0N/A
0N/A // %%% We assume that rest returns *all* unprocessed components.
0N/A // Don't' know if that is a good assumption, given
0N/A // NotFound doesn't set rest as expected. -RL
0N/A if (inputName != null && (inputName.length > rest.length)) {
0N/A NameComponent[] resolvedName =
0N/A new NameComponent[inputName.length - rest.length];
0N/A System.arraycopy(inputName, 0, resolvedName, 0, resolvedName.length);
0N/A // Wrap resolved NamingContext inside a CNCtx
0N/A // Guess that its name (which is relative to ctx)
0N/A // is the part of inputName minus rest_of_name
0N/A ne.setResolvedObj(new CNCtx(ctx._orb, ctx.orbTracker, nc,
0N/A ctx._env,
0N/A ctx.makeFullName(resolvedName)));
0N/A } else {
0N/A ne.setResolvedObj(ctx);
0N/A }
0N/A
0N/A ne.setRemainingName(CNNameParser.cosNameToName(rest));
0N/A
0N/A } else if (e instanceof InvalidName) {
0N/A ne = new InvalidNameException();
0N/A } else if (e instanceof AlreadyBound) {
0N/A ne = new NameAlreadyBoundException();
0N/A } else if (e instanceof NotEmpty) {
0N/A ne = new ContextNotEmptyException();
0N/A } else {
0N/A ne = new NamingException("Unknown reasons");
0N/A }
0N/A
0N/A ne.setRootCause(e);
0N/A return ne;
0N/A }
0N/A
0N/A private static final NamingException tryFed(NotFound e, CNCtx ctx,
0N/A NameComponent[] inputName) throws NamingException {
0N/A NameComponent[] rest = ((NotFound) e).rest_of_name;
0N/A
0N/A if (debug) {
0N/A System.out.println(((NotFound)e).why.value());
0N/A System.out.println(rest.length);
0N/A }
0N/A
0N/A // %%% Using 1.2 & 1.3 Sun's tnameserv, 'rest' contains only the first
0N/A // component that failed, not *rest* as advertized. This is useless
0N/A // because what if you have something like aa/aa/aa/aa/aa.
0N/A // If one of those is not found, you get "aa" as 'rest'.
0N/A if (rest.length == 1 && inputName != null) {
0N/A // Check that we're not talking to 1.2/1.3 Sun tnameserv
0N/A NameComponent lastIn = inputName[inputName.length-1];
0N/A if (rest[0].id.equals(lastIn.id) &&
0N/A rest[0].kind != null &&
0N/A rest[0].kind.equals(lastIn.kind)) {
0N/A // Might be legit
0N/A ;
0N/A } else {
0N/A // Due to 1.2/1.3 bug that always returns single-item 'rest'
0N/A NamingException ne = new NameNotFoundException();
0N/A ne.setRemainingName(CNNameParser.cosNameToName(rest));
0N/A ne.setRootCause(e);
0N/A throw ne;
0N/A }
0N/A }
0N/A // Fixed in 1.4; perform calculations based on correct (1.4) behavior
0N/A
0N/A // Calculate the components of the name that has been resolved
0N/A NameComponent[] resolvedName = null;
0N/A int len = 0;
0N/A if (inputName != null && (inputName.length >= rest.length)) {
0N/A
0N/A if (e.why == NotFoundReason.not_context) {
0N/A // First component of rest is found but not a context; keep it
0N/A // as part of resolved name
0N/A len = inputName.length - (rest.length - 1);
0N/A
0N/A // Remove resolved component from rest
0N/A if (rest.length == 1) {
0N/A // No more remaining
0N/A rest = null;
0N/A } else {
0N/A NameComponent[] tmp = new NameComponent[rest.length-1];
0N/A System.arraycopy(rest, 1, tmp, 0, tmp.length);
0N/A rest = tmp;
0N/A }
0N/A } else {
0N/A len = inputName.length - rest.length;
0N/A }
0N/A
0N/A if (len > 0) {
0N/A resolvedName = new NameComponent[len];
0N/A System.arraycopy(inputName, 0, resolvedName, 0, len);
0N/A }
0N/A }
0N/A
0N/A // Create CPE and set common fields
0N/A CannotProceedException cpe = new CannotProceedException();
0N/A cpe.setRootCause(e);
0N/A if (rest != null && rest.length > 0) {
0N/A cpe.setRemainingName(CNNameParser.cosNameToName(rest));
0N/A }
0N/A cpe.setEnvironment(ctx._env);
0N/A
0N/A if (debug) {
0N/A System.out.println("rest of name: " + cpe.getRemainingName());
0N/A }
0N/A
0N/A // Lookup resolved name to get resolved object
0N/A final java.lang.Object resolvedObj =
0N/A (resolvedName != null) ? ctx.callResolve(resolvedName) : ctx;
0N/A
0N/A if (resolvedObj instanceof javax.naming.Context) {
0N/A // obj is a context and child is not found
0N/A // try getting its nns dynamically by constructing
0N/A // a Reference containing obj.
0N/A RefAddr addr = new RefAddr("nns") {
0N/A public java.lang.Object getContent() {
0N/A return resolvedObj;
0N/A }
0N/A private static final long serialVersionUID =
0N/A 669984699392133792L;
0N/A };
0N/A Reference ref = new Reference("java.lang.Object", addr);
0N/A
0N/A // Resolved name has trailing slash to indicate nns
0N/A CompositeName cname = new CompositeName();
0N/A cname.add(""); // add trailing slash
0N/A
0N/A cpe.setResolvedObj(ref);
0N/A cpe.setAltName(cname);
0N/A cpe.setAltNameCtx((javax.naming.Context)resolvedObj);
0N/A
0N/A return cpe;
0N/A } else {
0N/A // Not a context, use object factory to transform object.
0N/A
0N/A Name cname = CNNameParser.cosNameToName(resolvedName);
0N/A java.lang.Object resolvedObj2;
0N/A try {
0N/A resolvedObj2 = NamingManager.getObjectInstance(resolvedObj,
0N/A cname, ctx, ctx._env);
0N/A } catch (NamingException ge) {
0N/A throw ge;
0N/A } catch (Exception ge) {
0N/A NamingException ne = new NamingException(
0N/A "problem generating object using object factory");
0N/A ne.setRootCause(ge);
0N/A throw ne;
0N/A }
0N/A
0N/A // If a context, continue operation with context
0N/A if (resolvedObj2 instanceof javax.naming.Context) {
0N/A cpe.setResolvedObj(resolvedObj2);
0N/A } else {
0N/A // Add trailing slash
0N/A cname.add("");
0N/A cpe.setAltName(cname);
0N/A
0N/A // Create nns reference
0N/A final java.lang.Object rf2 = resolvedObj2;
0N/A RefAddr addr = new RefAddr("nns") {
0N/A public java.lang.Object getContent() {
0N/A return rf2;
0N/A }
0N/A private static final long serialVersionUID =
0N/A -785132553978269772L;
0N/A };
0N/A Reference ref = new Reference("java.lang.Object", addr);
0N/A cpe.setResolvedObj(ref);
0N/A cpe.setAltNameCtx(ctx);
0N/A }
0N/A return cpe;
0N/A }
0N/A }
0N/A}