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