- Revision
- 361
- Author
- mauro
- Date
- 2007-11-12 18:40:17 -0600 (Mon, 12 Nov 2007)
Log Message
Refactored writing monitor to make the levels and the templates configurable and overridable by sub-classes.
Modified Paths
- trunk/core/src/main/java/org/codehaus/waffle/action/intercept/DefaultInterceptorChain.java
- trunk/core/src/main/java/org/codehaus/waffle/bind/IntrospectingRequestAttributeBinder.java
- trunk/core/src/main/java/org/codehaus/waffle/bind/OgnlDataBinder.java
- trunk/core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java
- trunk/core/src/main/java/org/codehaus/waffle/monitor/BindMonitor.java
- trunk/core/src/main/java/org/codehaus/waffle/monitor/CommonsLoggingMonitor.java
- trunk/core/src/main/java/org/codehaus/waffle/monitor/SilentMonitor.java
- trunk/core/src/test/java/org/codehaus/waffle/monitor/AbstractWritingMonitorTest.java
- trunk/core/src/test/java/org/codehaus/waffle/testmodel/StubMonitor.java
Diff
Modified: trunk/core/src/main/java/org/codehaus/waffle/action/intercept/DefaultInterceptorChain.java (360 => 361)
--- trunk/core/src/main/java/org/codehaus/waffle/action/intercept/DefaultInterceptorChain.java 2007-11-12 22:32:14 UTC (rev 360) +++ trunk/core/src/main/java/org/codehaus/waffle/action/intercept/DefaultInterceptorChain.java 2007-11-13 00:40:17 UTC (rev 361) @@ -10,14 +10,14 @@ *****************************************************************************/ package org.codehaus.waffle.action.intercept; -import org.codehaus.waffle.controller.ControllerDefinition; -import org.codehaus.waffle.monitor.ActionMonitor; - import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Iterator; import java.util.List; +import org.codehaus.waffle.controller.ControllerDefinition; +import org.codehaus.waffle.monitor.ActionMonitor; + public class DefaultInterceptorChain implements InterceptorChain { private final Iterator<MethodInterceptor> iterator; private final ActionMonitor actionMonitor;
Modified: trunk/core/src/main/java/org/codehaus/waffle/bind/IntrospectingRequestAttributeBinder.java (360 => 361)
--- trunk/core/src/main/java/org/codehaus/waffle/bind/IntrospectingRequestAttributeBinder.java 2007-11-12 22:32:14 UTC (rev 360) +++ trunk/core/src/main/java/org/codehaus/waffle/bind/IntrospectingRequestAttributeBinder.java 2007-11-13 00:40:17 UTC (rev 361) @@ -49,13 +49,13 @@ } } } catch (IntrospectionException e) { - bindMonitor.bindFailed(controller, e); + bindMonitor.bindFailedForController(controller, e); throw new WaffleException(e); } catch (IllegalAccessException e) { - bindMonitor.bindFailed(controller, e); + bindMonitor.bindFailedForController(controller, e); throw new WaffleException(e); } catch (InvocationTargetException e) { - bindMonitor.bindFailed(controller, e); + bindMonitor.bindFailedForController(controller, e); throw new WaffleException(e); } }
Modified: trunk/core/src/main/java/org/codehaus/waffle/bind/OgnlDataBinder.java (360 => 361)
--- trunk/core/src/main/java/org/codehaus/waffle/bind/OgnlDataBinder.java 2007-11-12 22:32:14 UTC (rev 360) +++ trunk/core/src/main/java/org/codehaus/waffle/bind/OgnlDataBinder.java 2007-11-13 00:40:17 UTC (rev 361) @@ -55,12 +55,12 @@ String message = bindErrorMessageResolver.resolve(model, name, value); BindErrorMessage errorMessage = new BindErrorMessage(name, value, message); errorsContext.addErrorMessage(errorMessage); - bindMonitor.bindFailed(model, errorMessage); + bindMonitor.bindFailedForModel(model, errorMessage); } catch (BindException e) { // by convention BindExceptions should provide the correct bind error message to display to the end-user BindErrorMessage errorMessage = new BindErrorMessage(name, value, e.getMessage()); errorsContext.addErrorMessage(errorMessage); - bindMonitor.bindFailed(model, errorMessage); + bindMonitor.bindFailedForModel(model, errorMessage); } } }
Modified: trunk/core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java (360 => 361)
--- trunk/core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java 2007-11-12 22:32:14 UTC (rev 360) +++ trunk/core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java 2007-11-13 00:40:17 UTC (rev 361) @@ -16,6 +16,12 @@ import static org.codehaus.waffle.monitor.Monitor.Level.WARN; import java.lang.reflect.Method; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; import java.util.Set; import javax.servlet.http.HttpServletResponse; @@ -33,76 +39,157 @@ */ public abstract class AbstractWritingMonitor implements ActionMonitor, BindMonitor { - /** - * Writes message for a given level. Concrete implementations should provide writing functionality. - * - * @param level the Level - * @param message the message to write - */ - protected abstract void write(Level level, String message); + private Map<String, Level> levels; + private Map<String, String> templates; + + protected AbstractWritingMonitor(){ + this.levels = eventLevels(); + this.templates = eventTemplates(); + } + + protected Map<String, Level> eventLevels() { + Map<String, Level> levels = new HashMap<String, Level>(); + levels.put("defaultActionMethodFound", INFO); + levels.put("defaultActionMethodCached", DEBUG); + levels.put("pragmaticActionMethodFound", INFO); + levels.put("actionMethodFound", INFO); + levels.put("actionMethodExecuted", INFO); + levels.put("actionMethodExecutionFailed", WARN); + levels.put("methodNameResolved", INFO); + levels.put("methodIntercepted", INFO); + levels.put("argumentNameResolved", INFO); + levels.put("argumentNameNotMatched", INFO); + levels.put("bindFailedForModel", INFO); + levels.put("bindFailedForController", INFO); + levels.put("responseIsCommitted", INFO); + levels.put("viewDispatched", INFO); + return levels; + } - /** - * Traces an exception. Concrete implementations should provide writing functionality. - * - * @param exception the Exception to trace - */ - protected abstract void trace(Exception exception); + protected Map<String, String> eventTemplates() { + Map<String, String> templates = new HashMap<String, String>(); + templates.put("defaultActionMethodFound", "Default ActionMethod found: {0}"); + templates.put("defaultActionMethodCached", "Default ActionMethod cached for controller {0}: {1}"); + templates.put("pragmaticActionMethodFound", "Pragmatic ActionMethod found: {0}"); + templates.put("actionMethodFound", "ActionMethod found: {0}"); + templates.put("actionMethodExecuted", "ActionMethod executed with response: {0}"); + templates.put("actionMethodExecutionFailed", "ActionMethod failed: {0}"); + templates.put("methodNameResolved", "Method name ''{0}'' found for key ''{1}'' among keys {2}"); + templates.put("methodIntercepted", "Method ''{0}'' intercepted with arguments {1} and returned value ''{2}''"); + templates.put("argumentNameResolved", "Argument name ''{0}'' resolved to ''{1}'' in scope ''{2}''"); + templates.put("argumentNameNotMatched", "Argument name ''{0}'' not matched by pattern ''{1}''"); + templates.put("bindFailedForModel", "Bind failed for model ''{0}'': {1}"); + templates.put("bindFailedForController", "Bind failed for controller ''{0}'': {1}"); + templates.put("responseIsCommitted", "Response is committed for response: {0}"); + templates.put("viewDispatched", "View dispatched: {0}"); + return templates; + } + private Level level(String event) { + if ( !levels.containsKey(event) ){ + return Level.INFO; + } + return levels.get(event); + } + + private String template(String event) { + if ( !templates.containsKey(event) ){ + throw new NoSuchElementException(event); + } + return templates.get(event); + } + + protected String format(String template, Object ... arguments) { + return MessageFormat.format(template, arguments); + } + + protected void write(String event, Object... arguments) { + String message = format(template(event), arguments); + write(level(event), message); + for ( Exception exception : findExceptions(arguments) ){ + trace(exception); + } + } + + protected List<Exception> findExceptions(Object[] arguments) { + List<Exception> exceptions = new ArrayList<Exception>(); + for ( Object argument : arguments){ + if ( argument instanceof Exception ){ + exceptions.add((Exception)argument); + } + } + return exceptions; + } + public void defaultActionMethodFound(MethodDefinition methodDefinition) { - write(INFO, "Default ActionMethod found: " + methodDefinition); + write("defaultActionMethodFound", methodDefinition); } public void defaultActionMethodCached(Class<?> controllerType, MethodDefinition methodDefinition) { - write(DEBUG, "Default ActionMethod cached for controller " + controllerType.getName() + ": " + methodDefinition); + write("defaultActionMethodCached", controllerType, methodDefinition); } public void pragmaticActionMethodFound(MethodDefinition methodDefinition) { - write(INFO, "Pragmatic ActionMethod found: " + methodDefinition); + write("pragmaticActionMethodFound", methodDefinition); } public void actionMethodFound(MethodDefinition methodDefinition) { - write(INFO, "ActionMethod found: " + methodDefinition); + write("actionMethodFound", methodDefinition); } public void actionMethodExecuted(ActionMethodResponse actionMethodResponse) { - write(INFO, "ActionMethod executed with response: " + actionMethodResponse); + write("actionMethodExecuted", actionMethodResponse); } public void actionMethodExecutionFailed(Exception exception) { - trace(exception); + write("actionMethodExecutionFailed", exception); } public void methodNameResolved(String methodName, String methodKey, Set<String> keys) { - write(INFO, "Method name '" + methodName + "' found for key '" + methodKey + "' among keys " + keys); + write("methodNameResolved", methodName, methodKey, keys); } public void methodIntercepted(Method method, Object[] arguments, Object returnValue) { - write(INFO, "Method '" + method + "' intercepted with arguments '" + asList(arguments) - + "' and returned value '" + returnValue + "'"); + write("methodIntercepted", method, asList(arguments), returnValue); } public void argumentNameResolved(String name, Object value, Scope scope) { - write(INFO, "Argument name '" + name + "' resolved to '" + value + "' in scope " + scope); + write("argumentNameResolved", name, value, scope); } public void argumentNameNotMatched(String name, String pattern) { - write(WARN, "Argument name '" + name + "' not matched by pattern '" + pattern + "'" ); + write("argumentNameNotMatched", name, pattern); } - public void bindFailed(Object bindModel, BindErrorMessage errorMessage){ - write(WARN, "Bind failed for model '" + bindModel + "': " + errorMessage); + public void bindFailedForModel(Object bindModel, BindErrorMessage errorMessage){ + write("bindFailedForModel", bindModel, errorMessage); } - public void bindFailed(Object controller, Throwable cause){ - write(WARN, "Bind failed for controller '" + controller + "': " + cause); + public void bindFailedForController(Object controller, Throwable cause){ + write("bindFailedForController", controller, cause); } public void responseIsCommitted(HttpServletResponse response) { - write(INFO, "Reponse is already committed: "+response); + write("responseIsCommitted", response); } public void viewDispatched(View view) { - write(DEBUG, "Dispached view "+view); + write("viewDispatched", view); } + /** + * Writes message for a given level. Concrete implementations should provide writing functionality. + * + * @param level the Level + * @param message the message to write + */ + protected abstract void write(Level level, String message); + + /** + * Traces an exception. Concrete implementations should provide writing functionality. + * + * @param exception the Throwable to trace + */ + protected abstract void trace(Throwable exception); + }
Modified: trunk/core/src/main/java/org/codehaus/waffle/monitor/BindMonitor.java (360 => 361)
--- trunk/core/src/main/java/org/codehaus/waffle/monitor/BindMonitor.java 2007-11-12 22:32:14 UTC (rev 360) +++ trunk/core/src/main/java/org/codehaus/waffle/monitor/BindMonitor.java 2007-11-13 00:40:17 UTC (rev 361) @@ -20,8 +20,8 @@ */ public interface BindMonitor extends Monitor { - void bindFailed(Object bindModel, BindErrorMessage errorMessage); + void bindFailedForModel(Object bindModel, BindErrorMessage errorMessage); - void bindFailed(Object controller, Throwable cause); + void bindFailedForController(Object controller, Throwable cause); }
Modified: trunk/core/src/main/java/org/codehaus/waffle/monitor/CommonsLoggingMonitor.java (360 => 361)
--- trunk/core/src/main/java/org/codehaus/waffle/monitor/CommonsLoggingMonitor.java 2007-11-12 22:32:14 UTC (rev 360) +++ trunk/core/src/main/java/org/codehaus/waffle/monitor/CommonsLoggingMonitor.java 2007-11-13 00:40:17 UTC (rev 361) @@ -75,7 +75,7 @@ } @Override - protected void trace(Exception exception) { + protected void trace(Throwable exception) { if (log.isErrorEnabled()) { log.error(exception.getMessage(), exception); }
Modified: trunk/core/src/main/java/org/codehaus/waffle/monitor/SilentMonitor.java (360 => 361)
--- trunk/core/src/main/java/org/codehaus/waffle/monitor/SilentMonitor.java 2007-11-12 22:32:14 UTC (rev 360) +++ trunk/core/src/main/java/org/codehaus/waffle/monitor/SilentMonitor.java 2007-11-13 00:40:17 UTC (rev 361) @@ -21,7 +21,7 @@ // write nothing } - protected void trace(Exception exception) { + protected void trace(Throwable exception) { // write nothing } }
Modified: trunk/core/src/test/java/org/codehaus/waffle/monitor/AbstractWritingMonitorTest.java (360 => 361)
--- trunk/core/src/test/java/org/codehaus/waffle/monitor/AbstractWritingMonitorTest.java 2007-11-12 22:32:14 UTC (rev 360) +++ trunk/core/src/test/java/org/codehaus/waffle/monitor/AbstractWritingMonitorTest.java 2007-11-13 00:40:17 UTC (rev 361) @@ -43,7 +43,7 @@ } @Override - protected void trace(Exception exception) { + protected void trace(Throwable exception) { // will not be tested here } }; @@ -67,7 +67,7 @@ } @Override - protected void trace(Exception exception) { + protected void trace(Throwable exception) { exception.printStackTrace(new PrintWriter(monitorWriter)); } };
Modified: trunk/core/src/test/java/org/codehaus/waffle/testmodel/StubMonitor.java (360 => 361)
--- trunk/core/src/test/java/org/codehaus/waffle/testmodel/StubMonitor.java 2007-11-12 22:32:14 UTC (rev 360) +++ trunk/core/src/test/java/org/codehaus/waffle/testmodel/StubMonitor.java 2007-11-13 00:40:17 UTC (rev 361) @@ -45,10 +45,10 @@ public void argumentNameResolved(String name, Object value, Scope scope) { } - public void bindFailed(Object bindModel, BindErrorMessage errorMessage) { + public void bindFailedForModel(Object bindModel, BindErrorMessage errorMessage) { } - public void bindFailed(Object controller, Throwable cause) { + public void bindFailedForController(Object controller, Throwable cause) { } public void responseIsCommitted(HttpServletResponse response) {
To unsubscribe from this list please visit:
