1. /*
  2. * @(#)NamingException.java 1.8 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.naming;
  8. /**
  9. * This is the superclass of all exceptions thrown by
  10. * operations in the Context and DirContext interfaces.
  11. * The nature of the failure is described by the name of the subclass.
  12. * This exception captures the information pinpointing where the operation
  13. * failed, such as where resolution last proceeded to.
  14. * <ul>
  15. * <li> Resolved Name. Portion of name that has been resolved.
  16. * <li> Resolved Object. Object to which resolution of name proceeded.
  17. * <li> Remaining Name. Portion of name that has not been resolved.
  18. * <li> Explanation. Detail explaining why name resolution failed.
  19. * <li> Root Exception. The exception that caused this naming exception
  20. * to be thrown.
  21. *</ul>
  22. * null is an acceptable value for any of these fields. When null,
  23. * it means that no such information has been recorded for that field.
  24. *<p>
  25. * A NamingException instance is not synchronized against concurrent
  26. * multithreaded access. Multiple threads trying to access and modify
  27. * a single NamingException instance should lock the object.
  28. *
  29. * @author Rosanna Lee
  30. * @author Scott Seligman
  31. * @version 1.8 03/01/23
  32. * @since 1.3
  33. */
  34. public class NamingException extends Exception {
  35. /**
  36. * Contains the part of the name that has been successfully resolved.
  37. * It is a composite name and can be null.
  38. * This field is initialized by the constructors.
  39. * You should access and manipulate this field
  40. * through its get and set methods.
  41. * @serial
  42. * @see #getResolvedName
  43. * @see #setResolvedName
  44. */
  45. protected Name resolvedName;
  46. /**
  47. * Contains the object to which resolution of the part of the name was
  48. * successful. Can be null.
  49. * This field is initialized by the constructors.
  50. * You should access and manipulate this field
  51. * through its get and set methods.
  52. * @serial
  53. * @see #getResolvedObj
  54. * @see #setResolvedObj
  55. */
  56. protected Object resolvedObj;
  57. /**
  58. * Contains the remaining name that has not been resolved yet.
  59. * It is a composite name and can be null.
  60. * This field is initialized by the constructors.
  61. * You should access and manipulate this field
  62. * through its get, set, "append" methods.
  63. * @serial
  64. * @see #getRemainingName
  65. * @see #setRemainingName
  66. * @see #appendRemainingName
  67. * @see #appendRemainingComponent
  68. */
  69. protected Name remainingName;
  70. /**
  71. * Contains the original exception that caused this NamingException to
  72. * be thrown. This field is set if there is additional
  73. * information that could be obtained from the original
  74. * exception, or if there original exception could not be
  75. * mapped to a subclass of NamingException.
  76. * Can be null. This field is initialized by the constructors.
  77. * You should access and manipulate this field
  78. * through its get and set methods.
  79. * @serial
  80. * @see #getRootCause
  81. * @see #setRootCause
  82. */
  83. protected Throwable rootException = null;
  84. /**
  85. * Constructs a new NamingException with an explanation.
  86. * All unspecified fields are set to null.
  87. *
  88. * @param explanation A possibly null string containing
  89. * additional detail about this exception.
  90. * @see java.lang.Throwable#getMessage
  91. */
  92. public NamingException(String explanation) {
  93. super(explanation);
  94. resolvedName = remainingName = null;
  95. resolvedObj = null;
  96. }
  97. /**
  98. * Constructs a new NamingException.
  99. * All fields are set to null.
  100. */
  101. public NamingException() {
  102. super();
  103. resolvedName = remainingName = null;
  104. resolvedObj = null;
  105. }
  106. /**
  107. * Retrieves the leading portion of the name that was resolved
  108. * successfully.
  109. *
  110. * @return The part of the name that was resolved successfully.
  111. * It is a composite name. It can be null, which means
  112. * the resolved name field has not been set.
  113. * @see #getResolvedObj
  114. * @see #setResolvedName
  115. */
  116. public Name getResolvedName() {
  117. return resolvedName;
  118. }
  119. /**
  120. * Retrieves the remaining unresolved portion of the name.
  121. * @return The part of the name that has not been resolved.
  122. * It is a composite name. It can be null, which means
  123. * the remaining name field has not been set.
  124. * @see #setRemainingName
  125. * @see #appendRemainingName
  126. * @see #appendRemainingComponent
  127. */
  128. public Name getRemainingName() {
  129. return remainingName;
  130. }
  131. /**
  132. * Retrieves the object to which resolution was successful.
  133. * This is the object to which the resolved name is bound.
  134. *
  135. * @return The possibly null object that was resolved so far.
  136. * null means that the resolved object field has not been set.
  137. * @see #getResolvedName
  138. * @see #setResolvedObj
  139. */
  140. public Object getResolvedObj() {
  141. return resolvedObj;
  142. }
  143. /**
  144. * Retrieves the explanation associated with this exception.
  145. *
  146. * @return The possibly null detail string explaining more
  147. * about this exception. If null, it means there is no
  148. * detail message for this exception.
  149. *
  150. * @see java.lang.Throwable#getMessage
  151. */
  152. public String getExplanation() {
  153. return getMessage();
  154. }
  155. /**
  156. * Sets the resolved name field of this exception.
  157. *<p>
  158. * <tt>name</tt> is a composite name. If the intent is to set
  159. * this field using a compound name or string, you must
  160. * "stringify" the compound name, and create a composite
  161. * name with a single component using the string. You can then
  162. * invoke this method using the resulting composite name.
  163. *<p>
  164. * A copy of <code>name</code> is made and stored.
  165. * Subsequent changes to <code>name</code> does not
  166. * affect the copy in this NamingException and vice versa.
  167. *
  168. * @param name The possibly null name to set resolved name to.
  169. * If null, it sets the resolved name field to null.
  170. * @see #getResolvedName
  171. */
  172. public void setResolvedName(Name name) {
  173. if (name != null)
  174. resolvedName = (Name)(name.clone());
  175. else
  176. resolvedName = null;
  177. }
  178. /**
  179. * Sets the remaining name field of this exception.
  180. *<p>
  181. * <tt>name</tt> is a composite name. If the intent is to set
  182. * this field using a compound name or string, you must
  183. * "stringify" the compound name, and create a composite
  184. * name with a single component using the string. You can then
  185. * invoke this method using the resulting composite name.
  186. *<p>
  187. * A copy of <code>name</code> is made and stored.
  188. * Subsequent changes to <code>name</code> does not
  189. * affect the copy in this NamingException and vice versa.
  190. * @param name The possibly null name to set remaining name to.
  191. * If null, it sets the remaining name field to null.
  192. * @see #getRemainingName
  193. * @see #appendRemainingName
  194. * @see #appendRemainingComponent
  195. */
  196. public void setRemainingName(Name name) {
  197. if (name != null)
  198. remainingName = (Name)(name.clone());
  199. else
  200. remainingName = null;
  201. }
  202. /**
  203. * Sets the resolved object field of this exception.
  204. * @param obj The possibly null object to set resolved object to.
  205. * If null, the resolved object field is set to null.
  206. * @see #getResolvedObj
  207. */
  208. public void setResolvedObj(Object obj) {
  209. resolvedObj = obj;
  210. }
  211. /**
  212. * Add name as the last component in remaining name.
  213. * @param name The component to add.
  214. * If name is null, this method does not do anything.
  215. * @see #setRemainingName
  216. * @see #getRemainingName
  217. * @see #appendRemainingName
  218. */
  219. public void appendRemainingComponent(String name) {
  220. if (name != null) {
  221. try {
  222. if (remainingName == null) {
  223. remainingName = new CompositeName();
  224. }
  225. remainingName.add(name);
  226. } catch (NamingException e) {
  227. throw new IllegalArgumentException(e.toString());
  228. }
  229. }
  230. }
  231. /**
  232. * Add components from 'name' as the last components in
  233. * remaining name.
  234. *<p>
  235. * <tt>name</tt> is a composite name. If the intent is to append
  236. * a compound name, you should "stringify" the compound name
  237. * then invoke the overloaded form that accepts a String parameter.
  238. *<p>
  239. * Subsequent changes to <code>name</code> does not
  240. * affect the remaining name field in this NamingException and vice versa.
  241. * @param name The possibly null name containing ordered components to add.
  242. * If name is null, this method does not do anything.
  243. * @see #setRemainingName
  244. * @see #getRemainingName
  245. * @see #appendRemainingComponent
  246. */
  247. public void appendRemainingName(Name name) {
  248. if (name == null) {
  249. return;
  250. }
  251. if (remainingName != null) {
  252. try {
  253. remainingName.addAll(name);
  254. } catch (NamingException e) {
  255. throw new IllegalArgumentException(e.toString());
  256. }
  257. } else {
  258. remainingName = (Name)(name.clone());
  259. }
  260. }
  261. /**
  262. * Retrieves the root cause of this NamingException, if any.
  263. * The root cause of a naming exception is used when the service provider
  264. * wants to indicate to the caller a non-naming related exception
  265. * but at the same time wants to use the NamingException structure
  266. * to indicate how far the naming operation proceeded.
  267. *
  268. * @return The possibly null exception that caused this naming
  269. * exception. If null, it means no root cause has been
  270. * set for this naming exception.
  271. * @see #setRootCause
  272. * @see #rootException
  273. * @see #getCause
  274. */
  275. public Throwable getRootCause() {
  276. return rootException;
  277. }
  278. /**
  279. * Records the root cause of this NamingException.
  280. * If <tt>e</tt> is <tt>this</tt>, this method does not do anything.
  281. * @param e The possibly null exception that caused the naming
  282. * operation to fail. If null, it means this naming
  283. * exception has no root cause.
  284. * @see #getRootCause
  285. * @see #rootException
  286. * @see #initCause
  287. */
  288. public void setRootCause(Throwable e) {
  289. if (e != this) {
  290. rootException = e;
  291. }
  292. }
  293. public Throwable getCause() {
  294. return getRootCause();
  295. }
  296. public Throwable initCause(Throwable cause) {
  297. super.initCause(cause);
  298. setRootCause(cause);
  299. return this;
  300. }
  301. /**
  302. * Generates the string representation of this exception.
  303. * The string representation consists of this exception's class name,
  304. * its detailed message, and if it has a root cause, the string
  305. * representation of the root cause exception, followed by
  306. * the remaining name (if it is not null).
  307. * This string is used for debugging and not meant to be interpreted
  308. * programmatically.
  309. *
  310. * @return The non-null string containing the string representation
  311. * of this exception.
  312. */
  313. public String toString() {
  314. String answer = super.toString();
  315. if (rootException != null) {
  316. answer += " [Root exception is " + rootException + "]";
  317. }
  318. if (remainingName != null) {
  319. answer += "; remaining name '" + remainingName + "'";
  320. }
  321. return answer;
  322. }
  323. /**
  324. * Generates the string representation in more detail.
  325. * This string representation consists of the information returned
  326. * by the toString() that takes no parameters, plus the string
  327. * representation of the resolved object (if it is not null).
  328. * This string is used for debugging and not meant to be interpreted
  329. * programmatically.
  330. *
  331. * @param detail If true, include details about the resolved object
  332. * in addition to the other information.
  333. * @return The non-null string containing the string representation.
  334. */
  335. public String toString(boolean detail) {
  336. if (!detail || resolvedObj == null) {
  337. return toString();
  338. } else {
  339. return (toString() + "; resolved object " + resolvedObj);
  340. }
  341. }
  342. /**
  343. * Use serialVersionUID from JNDI 1.1.1 for interoperability
  344. */
  345. private static final long serialVersionUID = -1299181962103167177L;
  346. };