libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / javax / naming / NamingException.java
blob7a5c35eb57b25ce3a5c8746f1a64809728f590af
1 /* NamingException.java -- Superclass of all naming Exceptions
2 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.naming;
40 import gnu.java.lang.CPStringBuilder;
42 import java.io.PrintStream;
43 import java.io.PrintWriter;
45 /**
46 * Superclass of all naming Exceptions.
47 * Can contain extra information about the root cause of this exception
48 * (for example when the original exception was not a subclass of
49 * <code>NamingException</code>), the part of the <code>Name</code> that
50 * could be resolved (including the <code>Object</code> it resolved to)
51 * and the part of the <code>Name</code> that could not be resolved when
52 * the exception occured.
54 * @since 1.3
55 * @author Anthony Green (green@redhat.com)
56 * @author Mark Wielaard (mark@klomp.org)
58 public class NamingException extends Exception
60 private static final long serialVersionUID = -1299181962103167177L;
62 /**
63 * The root cause of this exception. Might be null. Set by calling
64 * <code>setRootCause()</code>, can be accessed by calling
65 * <code>getRootCause()</code>.
67 protected Throwable rootException;
69 /**
70 * If the exception was caused while resolving a <code>Name</code> then
71 * this field contains that part of the name that could be resolved.
72 * Field might be null. Set by calling <code>setResolvedName()</code>.
73 * Can be accessed by calling <code>getResolvedName</code>.
75 protected Name resolvedName;
77 /**
78 * If the exception was caused while resolving a <code>Name</code> then
79 * this field contains the object that part of the name could be resolved to.
80 * Field might be null. Set by calling <code>setResolvedObj()</code>.
81 * Can be accessed by calling <code>getResolvedObj</code>.
83 protected Object resolvedObj;
85 /**
86 * If the exception was caused while resolving a <code>Name</code> then
87 * this field contains that part of the name that could not be resolved.
88 * Field might be null. Set by calling <code>setRemainingName()</code>.
89 * The field can be extended by calling <code>appendRemainingName()</code>
90 * or <code>appendRemainingComponent()</code>.
91 * Can be accessed by calling <code>getRemainingName</code>.
93 protected Name remainingName;
95 /**
96 * Creates a new NamingException without a message. Does not set any of the
97 * <code>rootException</code>, <code>resolvedName</code>,
98 * <code>resolvedObj</code> or <code>remainingObject</code> fields.
99 * These fields can be set later.
101 public NamingException ()
103 super();
107 * Creates a new NamingException with a detailed message. Does not set
108 * the <code>rootException</code>, <code>resolvedName</code>,
109 * <code>resolvedObj</code> or <code>remainingObject,</code> fields.
110 * These fields can be set later.
112 public NamingException (String msg)
114 super(msg);
118 * Gets the root cause field <code>rootException</code> of this Exception.
120 public Throwable getRootCause ()
122 return rootException;
126 * Sets the root cause field <code>rootException</code> of this Exception.
128 public void setRootCause (Throwable e)
130 rootException = e;
134 * Gets the part of the name that could be resolved before this exception
135 * happend. Returns the <code>resolvedName</code> field of this Exception.
137 public Name getResolvedName ()
139 return resolvedName;
143 * Sets the part of the name that could be resolved before this exception
144 * happend. Sets the <code>resolvedName</code> field of this Exception.
146 public void setResolvedName (Name name)
148 resolvedName = name;
152 * Gets the Object to which (part of) the name could be resolved before this
153 * exception happend. Returns the <code>resolvedObj</code> field of this
154 * Exception.
156 public Object getResolvedObj ()
158 return resolvedObj;
162 * Sets the Object to which (part of) the name could be resolved before this
163 * exception happend. Sets the <code>resolvedObj</code> field of this
164 * Exception.
166 public void setResolvedObj (Object o)
168 resolvedObj = o;
172 * Gets the part of the name that could not be resolved before this exception
173 * happend. Returns the <code>remainingName</code> field of this Exception.
175 public Name getRemainingName ()
177 return remainingName;
181 * Sets the part of the name that could be resolved before this exception
182 * happend. Sets the <code>resolvedName</code> field of this Exception.
183 * The field can be extended by calling <code>appendRemainingName()</code>
184 * or <code>appendRemainingComponent()</code>.
186 public void setRemainingName (Name name)
188 remainingName = name;
192 * Adds the given <code>Name</code> to the <code>remainingName</code> field.
193 * Does nothing when <code>name</code> is null or when a
194 * <code>InvalidNameException</code> is thrown when adding the name.
196 * @see Name#addAll(Name)
198 public void appendRemainingName (Name name)
200 if (name != null)
203 remainingName.addAll(name);
205 catch(InvalidNameException ine) { /* ignored */ }
209 * Adds the given <code>String</code> to the <code>remainingName</code> field.
210 * Does nothing when <code>name</code> is null or when a
211 * <code>InvalidNameException</code> is thrown when adding the component.
213 * @see Name#add(String)
215 public void appendRemainingComponent (String name)
217 if (name != null)
220 remainingName.add(name);
222 catch(InvalidNameException ine) { /* ignored */ }
226 * Gets the message given to the constructor or null if no message was given.
228 * @see Throwable#getMessage()
230 public String getExplanation()
232 return getMessage();
236 * Returns a String representation of this exception and possibly including
237 * the part object that could be resolved if the given flag is set to true.
238 * Always includes the root cause and the remaining name if not null.
240 public String toString(boolean objectInfo)
242 CPStringBuilder sb = new CPStringBuilder(super.toString());
243 Throwable cause = getRootCause();
244 if (cause != null)
246 sb.append(" caused by ");
247 sb.append(cause);
249 Name remaining = getRemainingName();
250 if (remaining != null)
252 sb.append(" [remainingName: ");
253 sb.append(remaining);
255 Object resolved = getResolvedObj();
256 if (objectInfo && resolved != null)
258 if (remainingName == null)
259 sb.append(" [");
260 else
261 sb.append(", ");
262 sb.append("resolvedObj: ");
263 sb.append(resolved);
265 if ((remaining != null) || (objectInfo && resolved != null))
266 sb.append(']');
268 return sb.toString();
272 * Returns a string representation of this exception.
273 * Calls <code>toString(false)</code>.
275 public String toString()
277 return toString(false);
280 * Prints the stacktrace of this exception or of the root cause if not null.
282 public void printStackTrace()
284 Throwable cause = getRootCause();
285 if (cause != null)
286 cause.printStackTrace();
287 else
288 super.printStackTrace();
292 * Prints the stacktrace of this exception or of the root cause if not null
293 * to the given <code>PrintStream</code>.
295 public void printStackTrace(PrintStream ps)
297 Throwable cause = getRootCause();
298 if (cause != null)
299 cause.printStackTrace(ps);
300 else
301 super.printStackTrace(ps);
305 * Prints the stacktrace of this exception or of the root cause if not null
306 * to the given <code>PrintWriter</code>.
308 public void printStackTrace(PrintWriter pw)
310 Throwable cause = getRootCause();
311 if (cause != null)
312 cause.printStackTrace(pw);
313 else
314 super.printStackTrace(pw);