Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/naming/NamingException.java
38829 views
/*1* Copyright (c) 1999, 2003, 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 javax.naming;2627/**28* This is the superclass of all exceptions thrown by29* operations in the Context and DirContext interfaces.30* The nature of the failure is described by the name of the subclass.31* This exception captures the information pinpointing where the operation32* failed, such as where resolution last proceeded to.33* <ul>34* <li> Resolved Name. Portion of name that has been resolved.35* <li> Resolved Object. Object to which resolution of name proceeded.36* <li> Remaining Name. Portion of name that has not been resolved.37* <li> Explanation. Detail explaining why name resolution failed.38* <li> Root Exception. The exception that caused this naming exception39* to be thrown.40*</ul>41* null is an acceptable value for any of these fields. When null,42* it means that no such information has been recorded for that field.43*<p>44* A NamingException instance is not synchronized against concurrent45* multithreaded access. Multiple threads trying to access and modify46* a single NamingException instance should lock the object.47*<p>48* This exception has been retrofitted to conform to49* the general purpose exception-chaining mechanism. The50* <i>root exception</i> (or <i>root cause</i>) is the same object as the51* <i>cause</i> returned by the {@link Throwable#getCause()} method.52*53* @author Rosanna Lee54* @author Scott Seligman55* @since 1.356*/575859public class NamingException extends Exception {60/**61* Contains the part of the name that has been successfully resolved.62* It is a composite name and can be null.63* This field is initialized by the constructors.64* You should access and manipulate this field65* through its get and set methods.66* @serial67* @see #getResolvedName68* @see #setResolvedName69*/70protected Name resolvedName;71/**72* Contains the object to which resolution of the part of the name was73* successful. Can be null.74* This field is initialized by the constructors.75* You should access and manipulate this field76* through its get and set methods.77* @serial78* @see #getResolvedObj79* @see #setResolvedObj80*/81protected Object resolvedObj;82/**83* Contains the remaining name that has not been resolved yet.84* It is a composite name and can be null.85* This field is initialized by the constructors.86* You should access and manipulate this field87* through its get, set, "append" methods.88* @serial89* @see #getRemainingName90* @see #setRemainingName91* @see #appendRemainingName92* @see #appendRemainingComponent93*/94protected Name remainingName;9596/**97* Contains the original exception that caused this NamingException to98* be thrown. This field is set if there is additional99* information that could be obtained from the original100* exception, or if the original exception could not be101* mapped to a subclass of NamingException.102* Can be null.103*<p>104* This field predates the general-purpose exception chaining facility.105* The {@link #initCause(Throwable)} and {@link #getCause()} methods106* are now the preferred means of accessing this information.107*108* @serial109* @see #getRootCause110* @see #setRootCause(Throwable)111* @see #initCause(Throwable)112* @see #getCause113*/114protected Throwable rootException = null;115116/**117* Constructs a new NamingException with an explanation.118* All unspecified fields are set to null.119*120* @param explanation A possibly null string containing121* additional detail about this exception.122* @see java.lang.Throwable#getMessage123*/124public NamingException(String explanation) {125super(explanation);126resolvedName = remainingName = null;127resolvedObj = null;128}129130/**131* Constructs a new NamingException.132* All fields are set to null.133*/134public NamingException() {135super();136resolvedName = remainingName = null;137resolvedObj = null;138}139140/**141* Retrieves the leading portion of the name that was resolved142* successfully.143*144* @return The part of the name that was resolved successfully.145* It is a composite name. It can be null, which means146* the resolved name field has not been set.147* @see #getResolvedObj148* @see #setResolvedName149*/150public Name getResolvedName() {151return resolvedName;152}153154/**155* Retrieves the remaining unresolved portion of the name.156* @return The part of the name that has not been resolved.157* It is a composite name. It can be null, which means158* the remaining name field has not been set.159* @see #setRemainingName160* @see #appendRemainingName161* @see #appendRemainingComponent162*/163public Name getRemainingName() {164return remainingName;165}166167/**168* Retrieves the object to which resolution was successful.169* This is the object to which the resolved name is bound.170*171* @return The possibly null object that was resolved so far.172* null means that the resolved object field has not been set.173* @see #getResolvedName174* @see #setResolvedObj175*/176public Object getResolvedObj() {177return resolvedObj;178}179180/**181* Retrieves the explanation associated with this exception.182*183* @return The possibly null detail string explaining more184* about this exception. If null, it means there is no185* detail message for this exception.186*187* @see java.lang.Throwable#getMessage188*/189public String getExplanation() {190return getMessage();191}192193/**194* Sets the resolved name field of this exception.195*<p>196* <tt>name</tt> is a composite name. If the intent is to set197* this field using a compound name or string, you must198* "stringify" the compound name, and create a composite199* name with a single component using the string. You can then200* invoke this method using the resulting composite name.201*<p>202* A copy of <code>name</code> is made and stored.203* Subsequent changes to <code>name</code> does not204* affect the copy in this NamingException and vice versa.205*206* @param name The possibly null name to set resolved name to.207* If null, it sets the resolved name field to null.208* @see #getResolvedName209*/210public void setResolvedName(Name name) {211if (name != null)212resolvedName = (Name)(name.clone());213else214resolvedName = null;215}216217/**218* Sets the remaining name field of this exception.219*<p>220* <tt>name</tt> is a composite name. If the intent is to set221* this field using a compound name or string, you must222* "stringify" the compound name, and create a composite223* name with a single component using the string. You can then224* invoke this method using the resulting composite name.225*<p>226* A copy of <code>name</code> is made and stored.227* Subsequent changes to <code>name</code> does not228* affect the copy in this NamingException and vice versa.229* @param name The possibly null name to set remaining name to.230* If null, it sets the remaining name field to null.231* @see #getRemainingName232* @see #appendRemainingName233* @see #appendRemainingComponent234*/235public void setRemainingName(Name name) {236if (name != null)237remainingName = (Name)(name.clone());238else239remainingName = null;240}241242/**243* Sets the resolved object field of this exception.244* @param obj The possibly null object to set resolved object to.245* If null, the resolved object field is set to null.246* @see #getResolvedObj247*/248public void setResolvedObj(Object obj) {249resolvedObj = obj;250}251252/**253* Add name as the last component in remaining name.254* @param name The component to add.255* If name is null, this method does not do anything.256* @see #setRemainingName257* @see #getRemainingName258* @see #appendRemainingName259*/260public void appendRemainingComponent(String name) {261if (name != null) {262try {263if (remainingName == null) {264remainingName = new CompositeName();265}266remainingName.add(name);267} catch (NamingException e) {268throw new IllegalArgumentException(e.toString());269}270}271}272273/**274* Add components from 'name' as the last components in275* remaining name.276*<p>277* <tt>name</tt> is a composite name. If the intent is to append278* a compound name, you should "stringify" the compound name279* then invoke the overloaded form that accepts a String parameter.280*<p>281* Subsequent changes to <code>name</code> does not282* affect the remaining name field in this NamingException and vice versa.283* @param name The possibly null name containing ordered components to add.284* If name is null, this method does not do anything.285* @see #setRemainingName286* @see #getRemainingName287* @see #appendRemainingComponent288*/289public void appendRemainingName(Name name) {290if (name == null) {291return;292}293if (remainingName != null) {294try {295remainingName.addAll(name);296} catch (NamingException e) {297throw new IllegalArgumentException(e.toString());298}299} else {300remainingName = (Name)(name.clone());301}302}303304/**305* Retrieves the root cause of this NamingException, if any.306* The root cause of a naming exception is used when the service provider307* wants to indicate to the caller a non-naming related exception308* but at the same time wants to use the NamingException structure309* to indicate how far the naming operation proceeded.310*<p>311* This method predates the general-purpose exception chaining facility.312* The {@link #getCause()} method is now the preferred means of obtaining313* this information.314*315* @return The possibly null exception that caused this naming316* exception. If null, it means no root cause has been317* set for this naming exception.318* @see #setRootCause319* @see #rootException320* @see #getCause321*/322public Throwable getRootCause() {323return rootException;324}325326/**327* Records the root cause of this NamingException.328* If <tt>e</tt> is <tt>this</tt>, this method does not do anything.329*<p>330* This method predates the general-purpose exception chaining facility.331* The {@link #initCause(Throwable)} method is now the preferred means332* of recording this information.333*334* @param e The possibly null exception that caused the naming335* operation to fail. If null, it means this naming336* exception has no root cause.337* @see #getRootCause338* @see #rootException339* @see #initCause340*/341public void setRootCause(Throwable e) {342if (e != this) {343rootException = e;344}345}346347/**348* Returns the cause of this exception. The cause is the349* throwable that caused this naming exception to be thrown.350* Returns <code>null</code> if the cause is nonexistent or351* unknown.352*353* @return the cause of this exception, or <code>null</code> if the354* cause is nonexistent or unknown.355* @see #initCause(Throwable)356* @since 1.4357*/358public Throwable getCause() {359return getRootCause();360}361362/**363* Initializes the cause of this exception to the specified value.364* The cause is the throwable that caused this naming exception to be365* thrown.366*<p>367* This method may be called at most once.368*369* @param cause the cause, which is saved for later retrieval by370* the {@link #getCause()} method. A <tt>null</tt> value371* indicates that the cause is nonexistent or unknown.372* @return a reference to this <code>NamingException</code> instance.373* @throws IllegalArgumentException if <code>cause</code> is this374* exception. (A throwable cannot be its own cause.)375* @throws IllegalStateException if this method has already376* been called on this exception.377* @see #getCause378* @since 1.4379*/380public Throwable initCause(Throwable cause) {381super.initCause(cause);382setRootCause(cause);383return this;384}385386/**387* Generates the string representation of this exception.388* The string representation consists of this exception's class name,389* its detailed message, and if it has a root cause, the string390* representation of the root cause exception, followed by391* the remaining name (if it is not null).392* This string is used for debugging and not meant to be interpreted393* programmatically.394*395* @return The non-null string containing the string representation396* of this exception.397*/398public String toString() {399String answer = super.toString();400401if (rootException != null) {402answer += " [Root exception is " + rootException + "]";403}404if (remainingName != null) {405answer += "; remaining name '" + remainingName + "'";406}407return answer;408}409410/**411* Generates the string representation in more detail.412* This string representation consists of the information returned413* by the toString() that takes no parameters, plus the string414* representation of the resolved object (if it is not null).415* This string is used for debugging and not meant to be interpreted416* programmatically.417*418* @param detail If true, include details about the resolved object419* in addition to the other information.420* @return The non-null string containing the string representation.421*/422public String toString(boolean detail) {423if (!detail || resolvedObj == null) {424return toString();425} else {426return (toString() + "; resolved object " + resolvedObj);427}428}429430/**431* Use serialVersionUID from JNDI 1.1.1 for interoperability432*/433private static final long serialVersionUID = -1299181962103167177L;434};435436437