001: package org.tigris.scarab.util;
002:
003: /* ================================================================
004: * Copyright (c) 2000-2002 CollabNet. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by Collab.Net <http://www.Collab.Net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of Collab.Net.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: *
043: * ====================================================================
044: *
045: * This software consists of voluntary contributions made by many
046: * individuals on behalf of Collab.Net.
047: */
048:
049: // Turbine
050: import org.apache.turbine.TurbineException;
051: import org.tigris.scarab.tools.ScarabLocalizationTool;
052: import org.tigris.scarab.tools.localization.L10NMessage;
053: import org.tigris.scarab.tools.localization.LocalizationKey;
054: import org.tigris.scarab.tools.localization.Localizable;
055:
056: /**
057: This class extends TurbineException and does not change its
058: functionality. It should be used to mark Scarab specific
059: exceptions.
060: In order to ensure localization of Exception messages,
061: ScarabException adds a new type of message, the L10NMessage.
062:
063: @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
064: @version $Id: ScarabException.java 10176 2006-06-15 09:39:17Z dabbous $
065: */
066: public class ScarabException extends TurbineException //implements Localizable
067: {
068: /**
069: * The exception message in non-localized form.
070: * Further infos, see the {@link #getMessage(L10N) getmessage } methods below.
071: */
072: private Localizable l10nMessage;
073:
074: /**
075: * Constructs a new <code>ScarabException</code> with specified
076: * resource and no parameters.
077: * @param theKey the l10n error key.
078: */
079: public ScarabException(LocalizationKey theKey) {
080: l10nMessage = new L10NMessage(theKey);
081: }
082:
083: /**
084: * Constructs a new <code>ScarabException</code> with specified
085: * resource and a nested Throwable.
086: * @param theKey the l10n error key.
087: * @param nested
088: */
089: public ScarabException(LocalizationKey theKey, Throwable nested) {
090: super (nested);
091: l10nMessage = new L10NMessage(theKey, nested);
092: }
093:
094: /**
095: * Constructs a new <code>ScarabException</code> with specified
096: * Localizable .
097: * @param theL10nInstance the l10n error key.
098: */
099: public ScarabException(Localizable theL10nInstance) {
100: l10nMessage = theL10nInstance;
101: }
102:
103: /**
104: * Constructs a new <code>ScarabException</code> with specified
105: * Localizable and a nested Throwable.
106: * @param theL10nInstance the l10n error key.
107: * @param nested
108: */
109: public ScarabException(Localizable theL10nInstance, Throwable nested) {
110: super (nested);
111: l10nMessage = theL10nInstance;
112: }
113:
114: /**
115: * Constructs a new <code>ScarabException</code> with specified
116: * resource and a list of parameters.
117: * @param theL10nInstance the l10n error key.
118: * @param theParams
119: */
120: public ScarabException(LocalizationKey theKey, Object[] theParams) {
121: l10nMessage = new L10NMessage(theKey, theParams);
122: }
123:
124: /**
125: * convenience constructor: Constructs a new <code>ScarabException</code>
126: * with specified resource and one parameter.
127: * @param theL10nInstance the l10n error key.
128: * @param p1
129: */
130: public ScarabException(LocalizationKey theKey, Object p1) {
131: this (theKey, new Object[] { p1 });
132: }
133:
134: /**
135: * convenience constructor: Constructs a new <code>ScarabException</code>
136: * with specified resource and two parameters.
137: * @param theL10nInstance the l10n error key.
138: * @param p1
139: * @param p2
140: */
141: public ScarabException(LocalizationKey theKey, Object p1, Object p2) {
142: this (theKey, new Object[] { p1, p2 });
143: }
144:
145: /**
146: * convenience constructor: Constructs a new <code>ScarabException</code>
147: * with specified resource and three parameters.
148: * @param theL10nInstance the l10n error key.
149: * @param p1
150: * @param p2
151: * @param p3
152: */
153: public ScarabException(LocalizationKey theKey, Object p1,
154: Object p2, Object p3) {
155: this (theKey, new Object[] { p1, p2, p3 });
156: }
157:
158: /**
159: * convenience constructor: Constructs a new <code>ScarabException</code>
160: * with specified resource, nested Throwable and an aritrary set of parameters.
161: *
162: * @param theKey
163: * @param nested
164: * @param theParams
165: */
166: public ScarabException(LocalizationKey theKey, Throwable nested,
167: Object[] theParams) {
168: this (new L10NMessage(theKey, theParams), nested);
169: }
170:
171: /**
172: * convenience constructor: Constructs a new <code>ScarabException</code>
173: * with specified resource, nested Throwable and one parameter.
174: * @param theKey
175: * @param nested
176: * @param p1
177: */
178: public ScarabException(LocalizationKey theKey, Throwable nested,
179: Object p1) {
180: this (new L10NMessage(theKey, p1), nested);
181: }
182:
183: /**
184: * convenience constructor: Constructs a new <code>ScarabException</code>
185: * with specified resource, nested Throwable and two parameters.
186: * @param theKey
187: * @param nested
188: * @param p1
189: * @param p2
190: */
191: public ScarabException(LocalizationKey theKey, Throwable nested,
192: Object p1, Object p2) {
193: this (new L10NMessage(theKey, p1, p2), nested);
194: }
195:
196: /**
197: * convenience constructor: Constructs a new <code>ScarabException</code>
198: * with specified resource, nested Throwable and three parameters.
199: * @param theKey
200: * @param nested
201: * @param p1
202: * @param p2
203: * @param p3
204: */
205: public ScarabException(LocalizationKey theKey, Throwable nested,
206: Object p1, Object p2, Object p3) {
207: this (new L10NMessage(theKey, p1, p2, p3), nested);
208: }
209:
210: /**
211: * return the L10NInstance, or null, if no L10N key was given.
212: * @return
213: */
214: public Localizable getL10nMessage() {
215: return l10nMessage;
216: }
217:
218: /**
219: * return the localized message by use of the
220: * given ScarabLocalizationTool. For further infos see
221: * {@link #getMessage() getMessage }
222: *
223: * @deprecated Does not follow core java patterns. Use setLocalizer(..) then getLocalizedMessage() instead.
224: *
225: * @param l10n
226: * @return
227: */
228: public String getMessage(final ScarabLocalizationTool l10n) {
229: return (l10nMessage == null) ? super .getMessage() : l10nMessage
230: .getMessage(l10n);
231: }
232:
233: /**
234: * return the localized message in english.
235: * Note: It is preferrable to use
236: * {@link #getMessage(ScarabLocalizationTool) getMessage }
237: * Currently it is possible, that a ScarabException
238: * contains NO L10NInstance. This is due to the deprecated
239: * constructors {@link #ScarabException() ScarabException }
240: * and {@link #ScarabException(String) ScarabException }
241: * Eventually (after these constructors have been deleted
242: * from the code base) we guarantee, that ScarabException
243: * is fully localized.
244: *
245: * @return localized english text
246: */
247: public String getMessage() {
248: return (l10nMessage == null) ? super .getMessage() : l10nMessage
249: .getMessage();
250: }
251:
252: /**
253: * Holds value of property localizer.
254: */
255: private ScarabLocalizationTool localizer;
256:
257: /**
258: * Setter for property l10n.
259: * @param l10n New value of property l10n.
260: */
261: public void setLocalizer(final ScarabLocalizationTool localizer) {
262:
263: this .localizer = localizer;
264: }
265:
266: public String getLocalizedMessage() {
267: return (l10nMessage == null) ? super.getMessage()
268: : (localizer == null) ? l10nMessage.getMessage()
269: : l10nMessage.getMessage(localizer);
270: }
271: }
|