jfarcand 2003/03/26 10:21:51 Modified: catalina/src/share/org/apache/catalina/core ApplicationContext.java ApplicationContextFacade.java Log: Better handle exception: - throw only expected exception - throw RuntimeException if a method throw an unexpected exception Revision Changes Path 1.10 +5 -5 jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/core/ApplicationContext.java Index: ApplicationContext.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/core/ApplicationContext.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- ApplicationContext.java 4 Mar 2003 01:57:05 -0000 1.9 +++ ApplicationContext.java 26 Mar 2003 18:21:50 -0000 1.10 @@ -467,7 +467,7 @@ throws MalformedURLException { if (!path.startsWith("/")) { - throw new MalformedURLException(sm.getString("applicationContext.requestDispatcher.iae")); + throw new MalformedURLException(sm.getString("applicationContext.requestDispatcher.iae", path)); } path = normalize(path); 1.5 +87 -24 jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/core/ApplicationContextFacade.java Index: ApplicationContextFacade.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/core/ApplicationContextFacade.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- ApplicationContextFacade.java 20 Mar 2003 20:06:37 -0000 1.4 +++ ApplicationContextFacade.java 26 Mar 2003 18:21:50 -0000 1.5 @@ -118,7 +118,7 @@ private HashMap objectCache; - private static org.apache.commons.logging.Log log= + private static org.apache.commons.logging.Log sysLog= org.apache.commons.logging.LogFactory.getLog( ApplicationContextFacade.class ); @@ -206,7 +206,14 @@ public URL getResource(String path) throws MalformedURLException { - return (URL)doPrivileged("getResource", new Object[]{path}); + try{ + return (URL)invokeMethod(context, "getResource", new Object[]{path}); + }catch(Throwable t){ + if (t instanceof MalformedURLException){ + throw (MalformedURLException)t; + } + return null; + } } @@ -227,7 +234,14 @@ public Servlet getServlet(String name) throws ServletException { - return (Servlet)doPrivileged("getServlet", new Object[]{name}); + try{ + return (Servlet)invokeMethod(context, "getServlet", new Object[]{name}); + } catch (Throwable t){ + if (t instanceof ServletException){ + throw (ServletException) t; + } + return null; + } } @@ -301,8 +315,39 @@ } + /** + * Use reflection to invoke the requested method. Cache the method object + * to speed up the process + * @param appContext The AppliationContext object on which the method + * will be invoked + * @param methodName The method to call. + * @param params The arguments passed to the called method. + */ + private Object doPrivileged(ApplicationContext appContext, + final String methodName, + final Object[] params) { + try{ + return invokeMethod(appContext, methodName, params ); + } catch (Throwable t){ + throw new RuntimeException(t.getMessage()); + } + + } + + + /** + * Use reflection to invoke the requested method. Cache the method object + * to speed up the process + * will be invoked + * @param methodName The method to call. + * @param params The arguments passed to the called method. + */ private Object doPrivileged(final String methodName, final Object[] params){ - return doPrivileged(context, methodName,params); + try{ + return invokeMethod(context, methodName, params); + }catch(Throwable t){ + throw new RuntimeException(t.getMessage()); + } } @@ -314,9 +359,11 @@ * @param methodName The method to call. * @param params The arguments passed to the called method. */ - private Object doPrivileged(ApplicationContext appContext, + private Object invokeMethod(ApplicationContext appContext, final String methodName, - final Object[] params){ + final Object[] params) + throws Throwable{ + try{ Method method = (Method)objectCache.get(methodName); if (method == null){ @@ -327,13 +374,8 @@ return executeMethod(method,appContext,params); } catch (Exception ex){ - if (ex instanceof InvocationTargetException){ - return ((InvocationTargetException)ex).getTargetException(); - } else if (ex instanceof PrivilegedActionException){ - return ((PrivilegedActionException)ex).getException(); - } else { - return ex; - } + handleException(ex, methodName); + return null; } } @@ -348,18 +390,18 @@ private Object doPrivileged(final String methodName, final Class[] clazz, final Object[] params){ + try{ Method method = context.getClass() .getMethod(methodName, (Class[])clazz); return executeMethod(method,context,params); } catch (Exception ex){ - if (ex instanceof InvocationTargetException){ - return ((InvocationTargetException)ex).getTargetException(); - } else if (ex instanceof PrivilegedActionException){ - return ((PrivilegedActionException)ex).getException(); - } else { - return ex; - } + try{ + handleException(ex, methodName); + }catch (Throwable t){ + throw new RuntimeException(t.getMessage()); + } + return null; } } @@ -387,5 +429,26 @@ } else { return method.invoke(context, params); } + } + + + /** + * Throw the real exception. + * @param ex The current exception + */ + private void handleException(Exception ex, String methodName) throws Throwable{ + Throwable realException; + if (ex instanceof InvocationTargetException){ + realException = ((InvocationTargetException)ex).getTargetException(); + } else if (ex instanceof PrivilegedActionException){ + realException = ((PrivilegedActionException)ex).getException(); + } else { + realException = ex; + } + + if (sysLog.isDebugEnabled() ){ + sysLog.debug("ApplicationContextFacade." + methodName,ex); + } + throw realException; } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]