NamingException.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* NamingException.java -- Superclass of all naming Exceptions
  2. Copyright (C) 2000, 2001 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.naming;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.io.PrintStream;
  34. import java.io.PrintWriter;
  35. /**
  36. * Superclass of all naming Exceptions.
  37. * Can contain extra information about the root cause of this exception
  38. * (for example when the original exception was not a subclass of
  39. * <code>NamingException</code>), the part of the <code>Name</code> that
  40. * could be resolved (including the <code>Object</code> it resolved to)
  41. * and the part of the <code>Name</code> that could not be resolved when
  42. * the exception occured.
  43. *
  44. * @since 1.3
  45. * @author Anthony Green (green@redhat.com)
  46. * @author Mark Wielaard (mark@klomp.org)
  47. */
  48. public class NamingException extends Exception
  49. {
  50. private static final long serialVersionUID = -1299181962103167177L;
  51. /**
  52. * The root cause of this exception. Might be null. Set by calling
  53. * <code>setRootCause()</code>, can be accessed by calling
  54. * <code>getRootCause()</code>.
  55. */
  56. protected Throwable rootException;
  57. /**
  58. * If the exception was caused while resolving a <code>Name</code> then
  59. * this field contains that part of the name that could be resolved.
  60. * Field might be null. Set by calling <code>setResolvedName()</code>.
  61. * Can be accessed by calling <code>getResolvedName</code>.
  62. */
  63. protected Name resolvedName;
  64. /**
  65. * If the exception was caused while resolving a <code>Name</code> then
  66. * this field contains the object that part of the name could be resolved to.
  67. * Field might be null. Set by calling <code>setResolvedObj()</code>.
  68. * Can be accessed by calling <code>getResolvedObj</code>.
  69. */
  70. protected Object resolvedObj;
  71. /**
  72. * If the exception was caused while resolving a <code>Name</code> then
  73. * this field contains that part of the name that could not be resolved.
  74. * Field might be null. Set by calling <code>setRemainingName()</code>.
  75. * The field can be extended by calling <code>appendRemainingName()</code>
  76. * or <code>appendRemainingComponent()</code>.
  77. * Can be accessed by calling <code>getRemainingName</code>.
  78. */
  79. protected Name remainingName;
  80. /**
  81. * Creates a new NamingException without a message. Does not set any of the
  82. * <code>rootException</code>, <code>resolvedName</code>,
  83. * <code>resolvedObj</code> or <code>remainingObject</code> fields.
  84. * These fields can be set later.
  85. */
  86. public NamingException ()
  87. {
  88. super();
  89. }
  90. /**
  91. * Creates a new NamingException with a detailed message. Does not set
  92. * the <code>rootException</code>, <code>resolvedName</code>,
  93. * <code>resolvedObj</code> or <code>remainingObject,</code> fields.
  94. * These fields can be set later.
  95. */
  96. public NamingException (String msg)
  97. {
  98. super(msg);
  99. }
  100. /**
  101. * Gets the root cause field <code>rootException</code> of this Exception.
  102. */
  103. public Throwable getRootCause ()
  104. {
  105. return rootException;
  106. }
  107. /**
  108. * Sets the root cause field <code>rootException</code> of this Exception.
  109. */
  110. public void setRootCause (Throwable e)
  111. {
  112. rootException = e;
  113. }
  114. /**
  115. * Gets the part of the name that could be resolved before this exception
  116. * happend. Returns the <code>resolvedName</code> field of this Exception.
  117. */
  118. public Name getResolvedName ()
  119. {
  120. return resolvedName;
  121. }
  122. /**
  123. * Sets the part of the name that could be resolved before this exception
  124. * happend. Sets the <code>resolvedName</code> field of this Exception.
  125. */
  126. public void setResolvedName (Name name)
  127. {
  128. resolvedName = name;
  129. }
  130. /**
  131. * Gets the Object to which (part of) the name could be resolved before this
  132. * exception happend. Returns the <code>resolvedObj</code> field of this
  133. * Exception.
  134. */
  135. public Object getResolvedObj ()
  136. {
  137. return resolvedObj;
  138. }
  139. /**
  140. * Sets the Object to which (part of) the name could be resolved before this
  141. * exception happend. Sets the <code>resolvedObj</code> field of this
  142. * Exception.
  143. */
  144. public void setResolvedObj (Object o)
  145. {
  146. resolvedObj = o;
  147. }
  148. /**
  149. * Gets the part of the name that could not be resolved before this exception
  150. * happend. Returns the <code>remainingName</code> field of this Exception.
  151. */
  152. public Name getRemainingName ()
  153. {
  154. return remainingName;
  155. }
  156. /**
  157. * Sets the part of the name that could be resolved before this exception
  158. * happend. Sets the <code>resolvedName</code> field of this Exception.
  159. * The field can be extended by calling <code>appendRemainingName()</code>
  160. * or <code>appendRemainingComponent()</code>.
  161. */
  162. public void setRemainingName (Name name)
  163. {
  164. remainingName = name;
  165. }
  166. /**
  167. * Adds the given <code>Name</code> to the <code>remainingName</code> field.
  168. * Does nothing when <code>name</code> is null or when a
  169. * <code>InvalidNameException</code> is thrown when adding the name.
  170. *
  171. * @see Name#addAll(Name)
  172. */
  173. public void appendRemainingName (Name name)
  174. {
  175. if (name != null)
  176. try
  177. {
  178. remainingName.addAll(name);
  179. }
  180. catch(InvalidNameException ine) { /* ignored */ }
  181. }
  182. /**
  183. * Adds the given <code>String</code> to the <code>remainingName</code> field.
  184. * Does nothing when <code>name</code> is null or when a
  185. * <code>InvalidNameException</code> is thrown when adding the component.
  186. *
  187. * @see Name#add(String)
  188. */
  189. public void appendRemainingComponent (String name)
  190. {
  191. if (name != null)
  192. try
  193. {
  194. remainingName.add(name);
  195. }
  196. catch(InvalidNameException ine) { /* ignored */ }
  197. }
  198. /**
  199. * Gets the message given to the constructor or null if no message was given.
  200. *
  201. * @see Throwable#getMessage()
  202. */
  203. public String getExplanation()
  204. {
  205. return getMessage();
  206. }
  207. /**
  208. * Returns a String representation of this exception and possibly including
  209. * the part object that could be resolved if the given flag is set to true.
  210. * Always includes the root cause and the remaining name if not null.
  211. */
  212. public String toString(boolean objectInfo)
  213. {
  214. CPStringBuilder sb = new CPStringBuilder(super.toString());
  215. Throwable cause = getRootCause();
  216. if (cause != null)
  217. {
  218. sb.append(" caused by ");
  219. sb.append(cause);
  220. }
  221. Name remaining = getRemainingName();
  222. if (remaining != null)
  223. {
  224. sb.append(" [remainingName: ");
  225. sb.append(remaining);
  226. }
  227. Object resolved = getResolvedObj();
  228. if (objectInfo && resolved != null)
  229. {
  230. if (remainingName == null)
  231. sb.append(" [");
  232. else
  233. sb.append(", ");
  234. sb.append("resolvedObj: ");
  235. sb.append(resolved);
  236. }
  237. if ((remaining != null) || (objectInfo && resolved != null))
  238. sb.append(']');
  239. return sb.toString();
  240. }
  241. /**
  242. * Returns a string representation of this exception.
  243. * Calls <code>toString(false)</code>.
  244. */
  245. public String toString()
  246. {
  247. return toString(false);
  248. }
  249. /**
  250. * Prints the stacktrace of this exception or of the root cause if not null.
  251. */
  252. public void printStackTrace()
  253. {
  254. Throwable cause = getRootCause();
  255. if (cause != null)
  256. cause.printStackTrace();
  257. else
  258. super.printStackTrace();
  259. }
  260. /**
  261. * Prints the stacktrace of this exception or of the root cause if not null
  262. * to the given <code>PrintStream</code>.
  263. */
  264. public void printStackTrace(PrintStream ps)
  265. {
  266. Throwable cause = getRootCause();
  267. if (cause != null)
  268. cause.printStackTrace(ps);
  269. else
  270. super.printStackTrace(ps);
  271. }
  272. /**
  273. * Prints the stacktrace of this exception or of the root cause if not null
  274. * to the given <code>PrintWriter</code>.
  275. */
  276. public void printStackTrace(PrintWriter pw)
  277. {
  278. Throwable cause = getRootCause();
  279. if (cause != null)
  280. cause.printStackTrace(pw);
  281. else
  282. super.printStackTrace(pw);
  283. }
  284. }