01: /*
02: jGuard is a security framework based on top of jaas (java authentication and authorization security).
03: it is written for web applications, to resolve simply, access control problems.
04: version $Name$
05: http://sourceforge.net/projects/jguard/
06:
07: Copyright (C) 2004 Charles GAY
08:
09: 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.lang.reflect.Constructor;
31: import java.lang.reflect.InvocationTargetException;
32: import java.util.Locale;
33: import java.util.ResourceBundle;
34:
35: import org.apache.commons.logging.Log;
36: import org.apache.commons.logging.LogFactory;
37:
38: /**
39: * Utility class for Throwable.
40: * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
41: *
42: */
43: public class ThrowableUtils {
44:
45: private static final Log logger = LogFactory
46: .getLog(ThrowableUtils.class);
47:
48: /**
49: * add Localization for Throwable thrown.
50: * @param le
51: * @return
52: */
53: public static Throwable localizeThrowable(Throwable le,
54: Locale locale) {
55: //authentication failed
56: logger.error("authentication failed.LoginException "
57: + le.getMessage(), le);
58: Class throwableClass = le.getClass();
59: Class[] clazz = new Class[] { String.class };
60: try {
61: Constructor constructor = throwableClass
62: .getConstructor(clazz);
63: ResourceBundle rb = ResourceBundleUtils
64: .getResourceBundle(locale);
65: Throwable localizedThrowable = new LocalizedThrowable(le,
66: rb);
67: Object[] objects = new Object[] { localizedThrowable
68: .getLocalizedMessage() };
69: Throwable throwable = (Throwable) constructor
70: .newInstance(objects);
71: return throwable;
72: } catch (SecurityException e) {
73: logger
74: .error("we cannot localize LoginException for security resitrictions");
75: return le;
76: } catch (NoSuchMethodException e) {
77: logger
78: .error("we cannot localize LoginException method not found ");
79: return le;
80: } catch (IllegalArgumentException e) {
81: logger
82: .error("we cannot localize LoginException arguments are illegal ");
83: return le;
84: } catch (InstantiationException e) {
85: logger
86: .error("we cannot localize LoginException we cannot instantiate the wrapped exception ");
87: return le;
88: } catch (IllegalAccessException e) {
89: logger
90: .error("we cannot localize LoginException we cannot access to the exception ");
91: return le;
92: } catch (InvocationTargetException e) {
93: logger
94: .error("we cannot localize LoginException we cannot invoke the exception ");
95: return le;
96: }
97: }
98:
99: }
|