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