001: /**
002: * $Id: PEAException.java,v 1.4 2005/10/21 16:17:23 jtb Exp $
003: * Copyright 2003 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wsrp.consumer.cli;
014:
015: import java.io.PrintWriter;
016:
017: import com.sun.portal.desktop.DesktopError;
018:
019: /**
020: * A <code>PEAException</code> is thrown when there are errors.
021: */
022:
023: public class PEAException extends Exception {
024:
025: public static final String RESOURCE_BASE = "wsrpPEA";
026:
027: protected static java.util.Locale locale = java.util.Locale
028: .getDefault();
029:
030: protected java.lang.Throwable origEx = null;
031: protected java.lang.String key = null;
032: protected java.lang.Object[] tokens = null;
033:
034: /*
035: * CONSTRUCTORS
036: */
037:
038: /**
039: * Constructs an instance of the <code>PEAException</code> class.
040: * @param key key string to index into resource bundle to retieve
041: * localized message
042: */
043: public PEAException(java.lang.String key) {
044: super (key);
045: this .key = key;
046: }
047:
048: /**
049: * Constructs an instance of the <code>PEAException</code> class.
050: * @param key key string to index into resource bundle to retieve
051: * localized message
052: * @param tokens array of tokens to be used by the exception message
053: */
054: public PEAException(java.lang.String key, java.lang.Object[] tokens) {
055: super (key);
056: this .key = key;
057: this .tokens = tokens;
058: }
059:
060: /**
061: * Constructs an instance of the <code>PEAException</code> class.
062: * @param key key string to index into resource bundle to retieve
063: * localized message
064: * @param t Throwable object provided by the object which is throwing
065: */
066: public PEAException(java.lang.String key, java.lang.Throwable t) {
067: super (key);
068: origEx = t;
069: this .key = key;
070: }
071:
072: /**
073: * Constructs an instance of the <code>PEAException</code> class.
074: * @param key key string to index into resource bundle to retieve
075: * localized message
076: * @param t Throwable object provided by the object which is throwing
077: * @param tokens array of tokens to be used by the exception message
078: */
079: public PEAException(java.lang.String key, java.lang.Throwable t,
080: java.lang.Object[] tokens) {
081: super (key);
082: origEx = t;
083: this .key = key;
084: this .tokens = tokens;
085: }
086:
087: /**
088: * Constructs an instance of the <code>PEAException</code> class.
089: * @param t Throwable object provided by the object which is throwing
090: * the exception
091: */
092: public PEAException(java.lang.Throwable t) {
093: super (t.getMessage());
094: origEx = t;
095: }
096:
097: public static void setLocale(java.util.Locale loc) {
098: locale = loc;
099: }
100:
101: public String getMessage() {
102: // non-localized resource bundle
103: java.util.ResourceBundle rb = java.util.PropertyResourceBundle
104: .getBundle(RESOURCE_BASE, java.util.Locale.getDefault());
105: return getMessageFromRB(rb, key, tokens);
106: }
107:
108: public Throwable getWrapped() {
109: return origEx;
110: }
111:
112: public String getWrappedMessage() {
113: String msg = null;
114: if (origEx != null) {
115: if (origEx instanceof DesktopError) {
116: Throwable wrapped = ((DesktopError) origEx).getCause();
117: if (wrapped != null) {
118: //msg = origEx.getMessage() + wrapped.getMessage();
119: msg = wrapped.getMessage();
120: } else {
121: msg = origEx.getMessage();
122: }
123: } else {
124: msg = origEx.getMessage();
125: }
126: }
127: return msg;
128: }
129:
130: public String getLocalizedMessage() {
131: // localized resource bundle
132: java.util.ResourceBundle rb = java.util.PropertyResourceBundle
133: .getBundle(RESOURCE_BASE, locale);
134: String msg = null;
135: try {
136: msg = getMessageFromRB(rb, key, tokens);
137: } catch (Throwable t) {
138: //
139: // if it fails to look up resource, just use the key
140: // name as the error message
141: //
142: msg = key;
143: }
144: return msg;
145: }
146:
147: private String getMessageFromRB(java.util.ResourceBundle rb,
148: java.lang.String key, java.lang.Object[] tokens)
149: throws java.util.MissingResourceException {
150:
151: String msg = rb.getString(key);
152:
153: if (tokens != null && tokens.length > 0) {
154: java.text.MessageFormat mf = new java.text.MessageFormat("");
155: mf.setLocale(rb.getLocale());
156: mf.applyPattern(msg);
157: return mf.format(tokens);
158: } else {
159: return msg;
160: }
161: }
162:
163: public void printStackTrace() {
164: if (origEx != null) {
165: origEx.printStackTrace();
166: } else {
167: super .printStackTrace();
168: }
169: }
170:
171: public void printStackTrace(PrintWriter pw) {
172: if (origEx != null) {
173: origEx.printStackTrace(pw);
174: } else {
175: super.printStackTrace(pw);
176: }
177: }
178: }
|