001: /*
002: jGuard is a security framework based on top of jaas (java authentication and authorization security).
003: it is written for web applications, to resolve simply, access control problems.
004: version $Name$
005: http://sourceforge.net/projects/jguard/
006:
007: Copyright (C) 2004 Charles GAY
008:
009: This library is free software; you can redistribute it and/or
010: modify it under the terms of the GNU Lesser General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: Lesser General Public License for more details.
018:
019: You should have received a copy of the GNU Lesser General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022:
023:
024: jGuard project home page:
025: http://sourceforge.net/projects/jguard/
026:
027: */
028: package net.sf.jguard.ext.util;
029:
030: import java.io.Serializable;
031: import java.util.MissingResourceException;
032: import java.util.ResourceBundle;
033:
034: import javax.security.auth.login.LoginException;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038:
039: /**
040: * Subclass of {@link LoginException} which provide a <strong>localized</strong> message.
041: * @author <a href="mailto:tandilero@users.sourceforge.net">Maximiliano Batelli</a>
042: * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
043: */
044: public class LocalizedThrowable extends Throwable implements
045: Serializable {
046:
047: private static final Log logger = LogFactory
048: .getLog(LocalizedThrowable.class);
049: /**
050: * serial version number.
051: */
052: private static final long serialVersionUID = 1L;
053:
054: private String errorKey = "";
055: private Throwable cause = null;
056: private ResourceBundle rb = null;
057:
058: /**
059: * @param message The error key
060: */
061: public LocalizedThrowable(String errorKey, ResourceBundle rb) {
062: this .errorKey = errorKey;
063: this .rb = rb;
064:
065: }
066:
067: /**
068: * @param message The Throwable to wrape to add Localization feature.
069: */
070: public LocalizedThrowable(Throwable wrappedThrowable,
071: ResourceBundle rb) {
072: this .errorKey = wrappedThrowable.getMessage();
073: this .rb = rb;
074: }
075:
076: /**
077: * @param wrappedThrowable The Throwable to wrape to add Localization feature.
078: * @param cause
079: */
080: public LocalizedThrowable(Throwable wrappedThrowable,
081: Throwable cause, ResourceBundle rb) {
082: this .errorKey = wrappedThrowable.getMessage();
083: this .cause = cause;
084: this .rb = rb;
085: }
086:
087: /**
088: *
089: * @param errorKey
090: * @param cause
091: */
092: public LocalizedThrowable(String errorKey, Throwable cause,
093: ResourceBundle rb) {
094: this .errorKey = errorKey;
095: this .cause = cause;
096: this .rb = rb;
097: }
098:
099: public String getLocalizedMessage() {
100: return getLocalizedMessage(this .rb);
101: }
102:
103: public String getLocalizedMessage(ResourceBundle rb) {
104:
105: String message = null;
106: try {
107: message = rb.getString(errorKey);
108: } catch (MissingResourceException e) {
109: logger
110: .error("Login error!!! but missing specific error key in bundle: "
111: + errorKey);
112: message = errorKey;
113: }
114: return message;
115: }
116:
117: public Throwable getCause() {
118: return cause;
119: }
120: }
|