Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jndi/cosnaming/ExceptionMapper.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.directory.*;29import javax.naming.spi.*;3031import org.omg.CosNaming.*;32import org.omg.CosNaming.NamingContextPackage.*;33import org.omg.CORBA.*;3435import com.sun.jndi.toolkit.corba.CorbaUtils;3637/**38* A convenience class to map the COS Naming exceptions to the JNDI exceptions.39* @author Raj Krishnamurthy40*/4142public final class ExceptionMapper {43private ExceptionMapper() {} // ensure no instance44private static final boolean debug = false;4546public static final NamingException mapException(Exception e,47CNCtx ctx, NameComponent[] inputName) throws NamingException {48if (e instanceof NamingException) {49return (NamingException)e;50}5152if (e instanceof RuntimeException) {53throw (RuntimeException)e;54}5556NamingException ne;57if (e instanceof NotFound) {58if (ctx.federation) {59return tryFed((NotFound)e, ctx, inputName);6061} else {62ne = new NameNotFoundException();63}6465} else if (e instanceof CannotProceed) {6667ne = new CannotProceedException();68NamingContext nc = ((CannotProceed) e).cxt;69NameComponent[] rest = ((CannotProceed) e).rest_of_name;7071// %%% We assume that rest returns *all* unprocessed components.72// Don't' know if that is a good assumption, given73// NotFound doesn't set rest as expected. -RL74if (inputName != null && (inputName.length > rest.length)) {75NameComponent[] resolvedName =76new NameComponent[inputName.length - rest.length];77System.arraycopy(inputName, 0, resolvedName, 0, resolvedName.length);78// Wrap resolved NamingContext inside a CNCtx79// Guess that its name (which is relative to ctx)80// is the part of inputName minus rest_of_name81ne.setResolvedObj(new CNCtx(ctx._orb, ctx.orbTracker, nc,82ctx._env,83ctx.makeFullName(resolvedName)));84} else {85ne.setResolvedObj(ctx);86}8788ne.setRemainingName(CNNameParser.cosNameToName(rest));8990} else if (e instanceof InvalidName) {91ne = new InvalidNameException();92} else if (e instanceof AlreadyBound) {93ne = new NameAlreadyBoundException();94} else if (e instanceof NotEmpty) {95ne = new ContextNotEmptyException();96} else {97ne = new NamingException("Unknown reasons");98}99100ne.setRootCause(e);101return ne;102}103104private static final NamingException tryFed(NotFound e, CNCtx ctx,105NameComponent[] inputName) throws NamingException {106NameComponent[] rest = e.rest_of_name;107108if (debug) {109System.out.println(e.why.value());110System.out.println(rest.length);111}112113// %%% Using 1.2 & 1.3 Sun's tnameserv, 'rest' contains only the first114// component that failed, not *rest* as advertized. This is useless115// because what if you have something like aa/aa/aa/aa/aa.116// If one of those is not found, you get "aa" as 'rest'.117if (rest.length == 1 && inputName != null) {118// Check that we're not talking to 1.2/1.3 Sun tnameserv119NameComponent lastIn = inputName[inputName.length-1];120if (rest[0].id.equals(lastIn.id) &&121rest[0].kind != null &&122rest[0].kind.equals(lastIn.kind)) {123// Might be legit124;125} else {126// Due to 1.2/1.3 bug that always returns single-item 'rest'127NamingException ne = new NameNotFoundException();128ne.setRemainingName(CNNameParser.cosNameToName(rest));129ne.setRootCause(e);130throw ne;131}132}133// Fixed in 1.4; perform calculations based on correct (1.4) behavior134135// Calculate the components of the name that has been resolved136NameComponent[] resolvedName = null;137int len = 0;138if (inputName != null && (inputName.length >= rest.length)) {139140if (e.why == NotFoundReason.not_context) {141// First component of rest is found but not a context; keep it142// as part of resolved name143len = inputName.length - (rest.length - 1);144145// Remove resolved component from rest146if (rest.length == 1) {147// No more remaining148rest = null;149} else {150NameComponent[] tmp = new NameComponent[rest.length-1];151System.arraycopy(rest, 1, tmp, 0, tmp.length);152rest = tmp;153}154} else {155len = inputName.length - rest.length;156}157158if (len > 0) {159resolvedName = new NameComponent[len];160System.arraycopy(inputName, 0, resolvedName, 0, len);161}162}163164// Create CPE and set common fields165CannotProceedException cpe = new CannotProceedException();166cpe.setRootCause(e);167if (rest != null && rest.length > 0) {168cpe.setRemainingName(CNNameParser.cosNameToName(rest));169}170cpe.setEnvironment(ctx._env);171172if (debug) {173System.out.println("rest of name: " + cpe.getRemainingName());174}175176// Lookup resolved name to get resolved object177final java.lang.Object resolvedObj =178(resolvedName != null) ? ctx.callResolve(resolvedName) : ctx;179180if (resolvedObj instanceof javax.naming.Context) {181// obj is a context and child is not found182// try getting its nns dynamically by constructing183// a Reference containing obj.184RefAddr addr = new RefAddr("nns") {185public java.lang.Object getContent() {186return resolvedObj;187}188private static final long serialVersionUID =189669984699392133792L;190};191Reference ref = new Reference("java.lang.Object", addr);192193// Resolved name has trailing slash to indicate nns194CompositeName cname = new CompositeName();195cname.add(""); // add trailing slash196197cpe.setResolvedObj(ref);198cpe.setAltName(cname);199cpe.setAltNameCtx((javax.naming.Context)resolvedObj);200201return cpe;202} else {203// Not a context, use object factory to transform object.204205Name cname = CNNameParser.cosNameToName(resolvedName);206java.lang.Object resolvedObj2 = null;207try {208// Check whether object factory codebase is trusted209if (CorbaUtils.isObjectFactoryTrusted(resolvedObj)) {210resolvedObj2 = NamingManager.getObjectInstance(resolvedObj,211cname, ctx, ctx._env);212}213} catch (NamingException ge) {214throw ge;215} catch (Exception ge) {216NamingException ne = new NamingException(217"problem generating object using object factory");218ne.setRootCause(ge);219throw ne;220}221222// If a context, continue operation with context223if (resolvedObj2 instanceof javax.naming.Context) {224cpe.setResolvedObj(resolvedObj2);225} else {226// Add trailing slash227cname.add("");228cpe.setAltName(cname);229230// Create nns reference231final java.lang.Object rf2 = resolvedObj2;232RefAddr addr = new RefAddr("nns") {233public java.lang.Object getContent() {234return rf2;235}236private static final long serialVersionUID =237-785132553978269772L;238};239Reference ref = new Reference("java.lang.Object", addr);240cpe.setResolvedObj(ref);241cpe.setAltNameCtx(ctx);242}243return cpe;244}245}246}247248249