KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > ext > util > LocalizedThrowable


1 /*
2  jGuard is a security framework based on top of jaas (java authentication and authorization security).
3  it is written for web applications, to resolve simply, access control problems.
4  version $Name$
5  http://sourceforge.net/projects/jguard/
6
7  Copyright (C) 2004 Charles GAY
8
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24  jGuard project home page:
25  http://sourceforge.net/projects/jguard/
26
27  */

28 package net.sf.jguard.ext.util;
29
30 import java.io.Serializable JavaDoc;
31 import java.util.MissingResourceException JavaDoc;
32 import java.util.ResourceBundle JavaDoc;
33
34 import javax.security.auth.login.LoginException JavaDoc;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 /**
40  * Subclass of {@link LoginException} which provide a <strong>localized</strong> message.
41  * @author <a HREF="mailto:tandilero@users.sourceforge.net">Maximiliano Batelli</a>
42  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
43  */

44 public class LocalizedThrowable extends Throwable JavaDoc implements Serializable JavaDoc {
45
46     
47     private static final Log logger = LogFactory.getLog(LocalizedThrowable.class);
48     /**
49      * serial version number.
50      */

51     private static final long serialVersionUID = 1L;
52     
53     private String JavaDoc errorKey = "";
54     private Throwable JavaDoc cause = null;
55     private ResourceBundle JavaDoc rb = null;
56     
57
58     /**
59      * @param message The error key
60      */

61     public LocalizedThrowable(String JavaDoc errorKey,ResourceBundle JavaDoc rb) {
62         this.errorKey = errorKey;
63         this.rb = rb;
64         
65     }
66     
67     /**
68      * @param message The Throwable to wrape to add Localization feature.
69      */

70     public LocalizedThrowable(Throwable JavaDoc wrappedThrowable,ResourceBundle JavaDoc rb) {
71         this.errorKey = wrappedThrowable.getMessage();
72         this.rb = rb;
73     }
74     
75     /**
76      * @param wrappedThrowable The Throwable to wrape to add Localization feature.
77      * @param cause
78      */

79     public LocalizedThrowable(Throwable JavaDoc wrappedThrowable,Throwable JavaDoc cause,ResourceBundle JavaDoc rb) {
80         this.errorKey = wrappedThrowable.getMessage();
81         this.cause = cause;
82         this.rb = rb;
83     }
84     
85     /**
86      *
87      * @param errorKey
88      * @param cause
89      */

90     public LocalizedThrowable(String JavaDoc errorKey,Throwable JavaDoc cause,ResourceBundle JavaDoc rb){
91         this.errorKey = errorKey;
92         this.cause = cause;
93         this.rb = rb;
94     }
95
96     
97     public String JavaDoc getLocalizedMessage(){
98         return getLocalizedMessage(this.rb);
99     }
100     
101     public String JavaDoc getLocalizedMessage(ResourceBundle JavaDoc rb) {
102
103         String JavaDoc message = null;
104         try {
105             message = rb.getString(errorKey);
106         } catch (MissingResourceException JavaDoc e) {
107             logger.error("Login error!!! but missing specific error key in bundle: " + errorKey);
108             message = errorKey;
109         }
110         return message;
111     }
112     
113     
114     public Throwable JavaDoc getCause(){
115         return cause;
116     }
117 }
118
Popular Tags