1 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 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 44 public class ThrowableUtils { 45 46 private static final Log logger = LogFactory.getLog(ThrowableUtils.class); 47 48 53 public static Throwable localizeThrowable(Throwable le,Locale locale) { 54 logger.error("authentication failed.LoginException "+le.getMessage(),le); 56 Class throwableClass = le.getClass(); 57 Class [] clazz = new Class []{String .class}; 58 try { 59 Constructor constructor = throwableClass.getConstructor(clazz); 60 ResourceBundle rb = ResourceBundleUtils.getResourceBundle(locale); 61 Throwable localizedThrowable = new LocalizedThrowable(le,rb); 62 Object [] objects = new Object []{localizedThrowable.getLocalizedMessage()}; 63 Throwable throwable = (Throwable ) constructor.newInstance(objects); 64 return throwable; 65 } catch (SecurityException e) { 66 logger.error("we cannot localize LoginException for security resitrictions"); 67 return le; 68 } catch (NoSuchMethodException e) { 69 logger.error("we cannot localize LoginException method not found "); 70 return le; 71 } catch (IllegalArgumentException e) { 72 logger.error("we cannot localize LoginException arguments are illegal "); 73 return le; 74 } catch (InstantiationException e) { 75 logger.error("we cannot localize LoginException we cannot instantiate the wrapped exception "); 76 return le; 77 } catch (IllegalAccessException e) { 78 logger.error("we cannot localize LoginException we cannot access to the exception "); 79 return le; 80 } catch (InvocationTargetException e) { 81 logger.error("we cannot localize LoginException we cannot invoke the exception "); 82 return le; 83 } 84 } 85 86 87 88 } 89 | Popular Tags |