Path: blob/master/src/java.naming/share/classes/javax/naming/NamingException.java
41152 views
/*1* Copyright (c) 1999, 2019, 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*/81@SuppressWarnings("serial") // Not statically typed as Serializable82protected Object resolvedObj;83/**84* Contains the remaining name that has not been resolved yet.85* It is a composite name and can be null.86* This field is initialized by the constructors.87* You should access and manipulate this field88* through its get, set, "append" methods.89* @serial90* @see #getRemainingName91* @see #setRemainingName92* @see #appendRemainingName93* @see #appendRemainingComponent94*/95protected Name remainingName;9697/**98* Contains the original exception that caused this NamingException to99* be thrown. This field is set if there is additional100* information that could be obtained from the original101* exception, or if the original exception could not be102* mapped to a subclass of NamingException.103* Can be null.104*<p>105* This field predates the general-purpose exception chaining facility.106* The {@link #initCause(Throwable)} and {@link #getCause()} methods107* are now the preferred means of accessing this information.108*109* @serial110* @see #getRootCause111* @see #setRootCause(Throwable)112* @see #initCause(Throwable)113* @see #getCause114*/115protected Throwable rootException = null;116117/**118* Constructs a new NamingException with an explanation.119* All unspecified fields are set to null.120*121* @param explanation A possibly null string containing122* additional detail about this exception.123* @see java.lang.Throwable#getMessage124*/125public NamingException(String explanation) {126super(explanation);127resolvedName = remainingName = null;128resolvedObj = null;129}130131/**132* Constructs a new NamingException.133* All fields are set to null.134*/135public NamingException() {136super();137resolvedName = remainingName = null;138resolvedObj = null;139}140141/**142* Retrieves the leading portion of the name that was resolved143* successfully.144*145* @return The part of the name that was resolved successfully.146* It is a composite name. It can be null, which means147* the resolved name field has not been set.148* @see #getResolvedObj149* @see #setResolvedName150*/151public Name getResolvedName() {152return resolvedName;153}154155/**156* Retrieves the remaining unresolved portion of the name.157* @return The part of the name that has not been resolved.158* It is a composite name. It can be null, which means159* the remaining name field has not been set.160* @see #setRemainingName161* @see #appendRemainingName162* @see #appendRemainingComponent163*/164public Name getRemainingName() {165return remainingName;166}167168/**169* Retrieves the object to which resolution was successful.170* This is the object to which the resolved name is bound.171*172* @return The possibly null object that was resolved so far.173* null means that the resolved object field has not been set.174* @see #getResolvedName175* @see #setResolvedObj176*/177public Object getResolvedObj() {178return resolvedObj;179}180181/**182* Retrieves the explanation associated with this exception.183*184* @return The possibly null detail string explaining more185* about this exception. If null, it means there is no186* detail message for this exception.187*188* @see java.lang.Throwable#getMessage189*/190public String getExplanation() {191return getMessage();192}193194/**195* Sets the resolved name field of this exception.196*<p>197* {@code name} is a composite name. If the intent is to set198* this field using a compound name or string, you must199* "stringify" the compound name, and create a composite200* name with a single component using the string. You can then201* invoke this method using the resulting composite name.202*<p>203* A copy of {@code name} is made and stored.204* Subsequent changes to {@code name} do not205* affect the copy in this NamingException and vice versa.206*207* @param name The possibly null name to set resolved name to.208* If null, it sets the resolved name field to null.209* @see #getResolvedName210*/211public void setResolvedName(Name name) {212if (name != null)213resolvedName = (Name)(name.clone());214else215resolvedName = null;216}217218/**219* Sets the remaining name field of this exception.220*<p>221* {@code name} is a composite name. If the intent is to set222* this field using a compound name or string, you must223* "stringify" the compound name, and create a composite224* name with a single component using the string. You can then225* invoke this method using the resulting composite name.226*<p>227* A copy of {@code name} is made and stored.228* Subsequent changes to {@code name} do not229* affect the copy in this NamingException and vice versa.230* @param name The possibly null name to set remaining name to.231* If null, it sets the remaining name field to null.232* @see #getRemainingName233* @see #appendRemainingName234* @see #appendRemainingComponent235*/236public void setRemainingName(Name name) {237if (name != null)238remainingName = (Name)(name.clone());239else240remainingName = null;241}242243/**244* Sets the resolved object field of this exception.245* @param obj The possibly null object to set resolved object to.246* If null, the resolved object field is set to null.247* @see #getResolvedObj248*/249public void setResolvedObj(Object obj) {250resolvedObj = obj;251}252253/**254* Add name as the last component in remaining name.255* @param name The component to add.256* If name is null, this method does not do anything.257* @see #setRemainingName258* @see #getRemainingName259* @see #appendRemainingName260*/261public void appendRemainingComponent(String name) {262if (name != null) {263try {264if (remainingName == null) {265remainingName = new CompositeName();266}267remainingName.add(name);268} catch (NamingException e) {269throw new IllegalArgumentException(e.toString());270}271}272}273274/**275* Add components from 'name' as the last components in276* remaining name.277*<p>278* {@code name} is a composite name. If the intent is to append279* a compound name, you should "stringify" the compound name280* then invoke the overloaded form that accepts a String parameter.281*<p>282* Subsequent changes to {@code name} do not283* affect the remaining name field in this NamingException and vice versa.284* @param name The possibly null name containing ordered components to add.285* If name is null, this method does not do anything.286* @see #setRemainingName287* @see #getRemainingName288* @see #appendRemainingComponent289*/290public void appendRemainingName(Name name) {291if (name == null) {292return;293}294if (remainingName != null) {295try {296remainingName.addAll(name);297} catch (NamingException e) {298throw new IllegalArgumentException(e.toString());299}300} else {301remainingName = (Name)(name.clone());302}303}304305/**306* Retrieves the root cause of this NamingException, if any.307* The root cause of a naming exception is used when the service provider308* wants to indicate to the caller a non-naming related exception309* but at the same time wants to use the NamingException structure310* to indicate how far the naming operation proceeded.311*<p>312* This method predates the general-purpose exception chaining facility.313* The {@link #getCause()} method is now the preferred means of obtaining314* this information.315*316* @return The possibly null exception that caused this naming317* exception. If null, it means no root cause has been318* set for this naming exception.319* @see #setRootCause320* @see #rootException321* @see #getCause322*/323public Throwable getRootCause() {324return rootException;325}326327/**328* Records the root cause of this NamingException.329* If {@code e} is {@code this}, this method does not do anything.330*<p>331* This method predates the general-purpose exception chaining facility.332* The {@link #initCause(Throwable)} method is now the preferred means333* of recording this information.334*335* @param e The possibly null exception that caused the naming336* operation to fail. If null, it means this naming337* exception has no root cause.338* @see #getRootCause339* @see #rootException340* @see #initCause341*/342public void setRootCause(Throwable e) {343if (e != this) {344rootException = e;345}346}347348/**349* Returns the cause of this exception. The cause is the350* throwable that caused this naming exception to be thrown.351* Returns {@code null} if the cause is nonexistent or352* unknown.353*354* @return the cause of this exception, or {@code null} if the355* cause is nonexistent or unknown.356* @see #initCause(Throwable)357* @since 1.4358*/359public Throwable getCause() {360return getRootCause();361}362363/**364* Initializes the cause of this exception to the specified value.365* The cause is the throwable that caused this naming exception to be366* thrown.367*<p>368* This method may be called at most once.369*370* @param cause the cause, which is saved for later retrieval by371* the {@link #getCause()} method. A {@code null} value372* indicates that the cause is nonexistent or unknown.373* @return a reference to this {@code NamingException} instance.374* @throws IllegalArgumentException if {@code cause} is this375* exception. (A throwable cannot be its own cause.)376* @throws IllegalStateException if this method has already377* been called on this exception.378* @see #getCause379* @since 1.4380*/381public Throwable initCause(Throwable cause) {382super.initCause(cause);383setRootCause(cause);384return this;385}386387/**388* Generates the string representation of this exception.389* The string representation consists of this exception's class name,390* its detailed message, and if it has a root cause, the string391* representation of the root cause exception, followed by392* the remaining name (if it is not null).393* This string is used for debugging and not meant to be interpreted394* programmatically.395*396* @return The non-null string containing the string representation397* of this exception.398*/399public String toString() {400String answer = super.toString();401402if (rootException != null) {403answer += " [Root exception is " + rootException + "]";404}405if (remainingName != null) {406answer += "; remaining name '" + remainingName + "'";407}408return answer;409}410411/**412* Generates the string representation in more detail.413* This string representation consists of the information returned414* by the toString() that takes no parameters, plus the string415* representation of the resolved object (if it is not null).416* This string is used for debugging and not meant to be interpreted417* programmatically.418*419* @param detail If true, include details about the resolved object420* in addition to the other information.421* @return The non-null string containing the string representation.422*/423public String toString(boolean detail) {424if (!detail || resolvedObj == null) {425return toString();426} else {427return (toString() + "; resolved object " + resolvedObj);428}429}430431/**432* Use serialVersionUID from JNDI 1.1.1 for interoperability433*/434private static final long serialVersionUID = -1299181962103167177L;435};436437438