001: /*---------------------------------------------------------------------------*\
002: $Id: FeedException.java 7041 2007-09-09 01:04:47Z bmc $
003: ---------------------------------------------------------------------------
004: This software is released under a BSD-style license:
005:
006: Copyright (c) 2004-2007 Brian M. Clapper. All rights reserved.
007:
008: Redistribution and use in source and binary forms, with or without
009: modification, are permitted provided that the following conditions are
010: met:
011:
012: 1. Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014:
015: 2. The end-user documentation included with the redistribution, if any,
016: must include the following acknowlegement:
017:
018: "This product includes software developed by Brian M. Clapper
019: (bmc@clapper.org, http://www.clapper.org/bmc/). That software is
020: copyright (c) 2004-2007 Brian M. Clapper."
021:
022: Alternately, this acknowlegement may appear in the software itself,
023: if wherever such third-party acknowlegements normally appear.
024:
025: 3. Neither the names "clapper.org", "curn", nor any of the names of the
026: project contributors may be used to endorse or promote products
027: derived from this software without prior written permission. For
028: written permission, please contact bmc@clapper.org.
029:
030: 4. Products derived from this software may not be called "curn", nor may
031: "clapper.org" appear in their names without prior written permission
032: of Brian M. Clapper.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
036: MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
037: NO EVENT SHALL BRIAN M. CLAPPER BE LIABLE FOR ANY DIRECT, INDIRECT,
038: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
039: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
040: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
041: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
043: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044: \*---------------------------------------------------------------------------*/
045:
046: package org.clapper.curn;
047:
048: import org.clapper.util.misc.BundleUtil;
049:
050: import java.util.Locale;
051:
052: /**
053: * <tt>FeedException</tt> is thrown when there's an error with a feed.
054: * It contains the corresponding <tt>FeedInfo</tt> object, and its
055: * <tt>getMessage()</tt> method includes the feed URL in the message.
056: *
057: * @version <tt>$Revision: 7041 $</tt>
058: */
059: public class FeedException extends CurnException {
060: /*----------------------------------------------------------------------*\
061: Private Static Variables
062: \*----------------------------------------------------------------------*/
063:
064: /**
065: * See JDK 1.5 version of java.io.Serializable
066: */
067: private static final long serialVersionUID = 1L;
068:
069: /*----------------------------------------------------------------------*\
070: Private Data Items
071: \*----------------------------------------------------------------------*/
072:
073: private final FeedInfo feedInfo;
074:
075: /*----------------------------------------------------------------------*\
076: Constructor
077: \*----------------------------------------------------------------------*/
078:
079: /**
080: * Default constructor, for an exception with no nested exception and
081: * no message.
082: *
083: * @param feedInfo the <tt>FeedInfo</tt> object for the feed
084: */
085: public FeedException(FeedInfo feedInfo) {
086: super ();
087: this .feedInfo = feedInfo;
088: }
089:
090: /**
091: * Constructs an exception containing another exception, but no message
092: * of its own.
093: *
094: * @param feedInfo the <tt>FeedInfo</tt> object for the feed
095: * @param exception the exception to contain
096: */
097: public FeedException(FeedInfo feedInfo, Throwable exception) {
098: super (exception);
099: this .feedInfo = feedInfo;
100: }
101:
102: /**
103: * Constructs an exception containing an error message, but no
104: * nested exception.
105: *
106: * @param feedInfo the <tt>FeedInfo</tt> object for the feed
107: * @param message the message to associate with this exception
108: */
109: public FeedException(FeedInfo feedInfo, String message) {
110: super (message);
111: this .feedInfo = feedInfo;
112: }
113:
114: /**
115: * Constructs an exception containing another exception and a message.
116: *
117: * @param feedInfo the <tt>FeedInfo</tt> object for the feed
118: * @param message the message to associate with this exception
119: * @param exception the exception to contain
120: */
121: public FeedException(FeedInfo feedInfo, String message,
122: Throwable exception) {
123: super (message, exception);
124: this .feedInfo = feedInfo;
125: }
126:
127: /**
128: * Constructs an exception containing a resource bundle name, a message
129: * key, and a default message (in case the resource bundle can't be
130: * found). Using this constructor is equivalent to calling the
131: * {@link #FeedException(FeedInfo,String,String,String,Object[])}
132: * constructor, with a null pointer for the <tt>Object[]</tt>
133: * parameter. Calls to <tt>getMesage(Locale)</tt> will attempt to
134: * retrieve the top-most message (i.e., the message from this
135: * exception, not from nested exceptions) by querying the named
136: * resource bundle. Calls to <tt>printStackTrace(PrintWriter,Locale)</tt>
137: * will do the same, where applicable. The message is not retrieved
138: * until one of those methods is called, because the desired locale is
139: * passed into <tt>getMessage()</tt> and <tt>printStackTrace()</tt>,
140: * not this constructor.
141: *
142: * @param feedInfo the <tt>FeedInfo</tt> object for the feed
143: * @param bundleName resource bundle name
144: * @param messageKey the key to the message to find in the bundle
145: * @param defaultMsg the default message
146: *
147: * @see #FeedException(FeedInfo,String,String,String,Object[])
148: */
149: public FeedException(FeedInfo feedInfo, String bundleName,
150: String messageKey, String defaultMsg) {
151: super (bundleName, messageKey, defaultMsg);
152: this .feedInfo = feedInfo;
153: }
154:
155: /**
156: * Constructs an exception containing a resource bundle name, a message
157: * key, and a default message (in case the resource bundle can't be
158: * found). Using this constructor is equivalent to calling the
159: * {@link #FeedException(FeedInfo,String,String,String,Object[],Throwable)}
160: * constructor, with a null pointer for the <tt>Object[]</tt> parameter.
161: * Calls to <tt>getMessage(Locale)</tt> will attempt to
162: * retrieve the top-most message (i.e., the message from this exception,
163: * not from nested exceptions) by querying the named resource bundle.
164: * Calls to <tt>printStackTrace(PrintWriter,Locale)</tt>
165: * will do the same, where applicable. The message is not retrieved
166: * until one of those methods is called, because the desired locale is
167: * passed into <tt>getMessage()</tt> and <tt>printStackTrace()</tt>,
168: * not this constructor.
169: *
170: * @param feedInfo the <tt>FeedInfo</tt> object for the feed
171: * @param bundleName resource bundle name
172: * @param messageKey the key to the message to find in the bundle
173: * @param defaultMsg the default message
174: * @param msgParams parameters to the message, if any, or null
175: *
176: * @see #FeedException(FeedInfo,String,String,String,Object[],Throwable)
177: */
178: public FeedException(FeedInfo feedInfo, String bundleName,
179: String messageKey, String defaultMsg, Object[] msgParams) {
180: super (bundleName, messageKey, defaultMsg, msgParams);
181: this .feedInfo = feedInfo;
182: }
183:
184: /**
185: * Constructs an exception containing a resource bundle name, a message
186: * key, a default message (in case the resource bundle can't be found), and
187: * another exception. Using this constructor is equivalent to calling the
188: * {@link #FeedException(FeedInfo,String,String,String,Object[],Throwable)}
189: * constructor, with a null pointer for the <tt>Object[]</tt>
190: * parameter. Calls to <tt>getMessage(Locale)</tt> will attempt to
191: * retrieve the top-most message (i.e., the message from this
192: * exception, not from nested exceptions) by querying the named
193: * resource bundle. Calls to <tt>printStackTrace(PrintWriter,Locale)</tt>
194: * will do the same, where applicable. The message is not retrieved
195: * until one of those methods is called, because the desired locale is
196: * passed into <tt>getMessage()</tt> and <tt>printStackTrace()</tt>,
197: * not this constructor.
198: *
199: * @param feedInfo the <tt>FeedInfo</tt> object for the feed
200: * @param bundleName resource bundle name
201: * @param messageKey the key to the message to find in the bundle
202: * @param defaultMsg the default message
203: * @param exception the exception to nest
204: *
205: * @see #FeedException(FeedInfo,String,String,String,Object[],Throwable)
206: */
207: public FeedException(FeedInfo feedInfo, String bundleName,
208: String messageKey, String defaultMsg, Throwable exception) {
209: super (bundleName, messageKey, defaultMsg, null, exception);
210: this .feedInfo = feedInfo;
211: }
212:
213: /**
214: * Constructs an exception containing a resource bundle name, a message
215: * key, a default message format (in case the resource bundle can't be
216: * found), arguments to be incorporated in the message via
217: * <tt>java.text.MessageFormat</tt>, and another exception.
218: * Calls to <tt>getMessage(Locale)</tt> will attempt to retrieve the
219: * top-most message (i.e., the message from this exception, not from
220: * nested exceptions) by querying the named resource bundle. Calls to
221: * <tt>printStackTrace(PrintWriter,Locale)</tt> will do the same, where
222: * applicable. The message is not retrieved until one of those methods
223: * is called, because the desired locale is passed into
224: * <tt>getMessage()</tt> and <tt>printStackTrace()</tt>, not this
225: * constructor.
226: *
227: * @param feedInfo the <tt>FeedInfo</tt> object for the feed
228: * @param bundleName resource bundle name
229: * @param messageKey the key to the message to find in the bundle
230: * @param defaultMsg the default message
231: * @param msgParams parameters to the message, if any, or null
232: * @param exception exception to be nested
233: *
234: * @see #FeedException(FeedInfo,String,String,String,Object[])
235: */
236: public FeedException(FeedInfo feedInfo, String bundleName,
237: String messageKey, String defaultMsg, Object[] msgParams,
238: Throwable exception) {
239: super (bundleName, messageKey, defaultMsg, msgParams, exception);
240: this .feedInfo = feedInfo;
241: }
242:
243: /*----------------------------------------------------------------------*\
244: Public Methods
245: \*----------------------------------------------------------------------*/
246:
247: /**
248: * Returns the error message string for this exception. If the
249: * exception was instantiated with a message of its own, then that
250: * message is returned. Otherwise, this method returns the class name,
251: * along with the class name of the first nested exception, if any.
252: * Unlike the parent <tt>Exception</tt> class, this method will never
253: * return null.
254: *
255: * @return the error message string for this exception
256: */
257: public String getMessage() {
258: return getMessage(Locale.getDefault());
259: }
260:
261: /**
262: * Returns the error message string for this exception. If the
263: * exception was instantiated with a message of its own, then that
264: * message is returned. Otherwise, this method returns the class name,
265: * along with the class name of the first nested exception, if any.
266: * Unlike the parent <tt>Exception</tt> class, this method will never
267: * return null. If a localized version of the message is available, it
268: * will be returned.
269: *
270: * @param locale the locale to use, or null for the default
271: *
272: * @return the error message string for this exception
273: */
274: public String getMessage(final Locale locale) {
275: StringBuilder buf = new StringBuilder();
276:
277: buf.append(BundleUtil.getMessage(Constants.BUNDLE_NAME, locale,
278: "FeedException.feedPrefix", "feed"));
279: buf.append(" \"");
280: buf.append(feedInfo.getURL().toString());
281: buf.append("\": ");
282:
283: buf.append(super.getMessage(locale));
284:
285: return buf.toString();
286: }
287:
288: }
|