- Revision
- 356
- Author
- mauro
- Date
- 2007-11-12 14:32:20 -0600 (Mon, 12 Nov 2007)
Log Message
Added monitoring to WaffleServlet and HierarchicalArgumentResolver.
Modified Paths
- trunk/core/src/main/java/org/codehaus/waffle/action/HierarchicalArgumentResolver.java
- trunk/core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java
- trunk/core/src/main/java/org/codehaus/waffle/monitor/ActionMonitor.java
- trunk/core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java
- trunk/core/src/test/java/org/codehaus/waffle/action/HierarchicalArgumentResolverTest.java
- trunk/core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java
- trunk/core/src/test/java/org/codehaus/waffle/testmodel/StubMonitor.java
Diff
Modified: trunk/core/src/main/java/org/codehaus/waffle/action/HierarchicalArgumentResolver.java (355 => 356)
--- trunk/core/src/main/java/org/codehaus/waffle/action/HierarchicalArgumentResolver.java 2007-11-12 19:46:19 UTC (rev 355) +++ trunk/core/src/main/java/org/codehaus/waffle/action/HierarchicalArgumentResolver.java 2007-11-12 20:32:20 UTC (rev 356) @@ -10,31 +10,44 @@ *****************************************************************************/ package org.codehaus.waffle.action; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import org.codehaus.waffle.monitor.ActionMonitor; + /** - * This implementation attempts to resolve the arguments value through the following order (returning the - * first not null value found): - * - * 1. Parameter - * 2. Request attribute - * 3. Session attribute - * 4. Application attribute - * - * else returns null + * Hierarchical implementation attempts to resolve the arguments value through the + * following ordered scoped (returning the first not null value found): * - * @author Micheal Ward + * <ol> + * <li>1. PARAMETER</li> + * <li>2. REQUEST attribute</li> + * <li>3. SESSION attribute</li> + * <li>4. APPLICATION attribute</li> + * </ol> + * + * If none are found, returns <code>null</code> + * + * @author Michael Ward + * @author Mauro Talevi */ public class HierarchicalArgumentResolver implements ArgumentResolver { + + public enum Scope { + PARAMETER, REQUEST, SESSION, APPLICATION + }; + private final Pattern pattern = Pattern.compile("\\{(\\w+)\\}"); private final ServletContext servletContext; + private final ActionMonitor actionMonitor; - public HierarchicalArgumentResolver(ServletContext servletContext) { + public HierarchicalArgumentResolver(ServletContext servletContext, ActionMonitor actionMonitor) { this.servletContext = servletContext; + this.actionMonitor = actionMonitor; } public Object resolve(HttpServletRequest request, String name) { @@ -43,26 +56,30 @@ if (matcher.matches()) { name = matcher.group(1); Object value = request.getParameter(name); + Scope scope = Scope.PARAMETER; if (value == null) { value = request.getAttribute(name); - + scope = Scope.REQUEST; if (value == null) { HttpSession session = request.getSession(); if (session != null) { value = session.getAttribute(name); + scope = Scope.SESSION; } if (value == null) { value = servletContext.getAttribute(name); + scope = Scope.APPLICATION; } } } - - return value; // not found return null + actionMonitor.argumentResolved(name, value, scope); + return value; // return value, could be null } + actionMonitor.argumentNameNotMatched(name, pattern.pattern()); return name; // return name as the value }
Modified: trunk/core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java (355 => 356)
--- trunk/core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java 2007-11-12 19:46:19 UTC (rev 355) +++ trunk/core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java 2007-11-12 20:32:20 UTC (rev 356) @@ -17,6 +17,7 @@ import java.util.Set; import org.codehaus.waffle.action.MethodDefinition; +import org.codehaus.waffle.action.HierarchicalArgumentResolver.Scope; import org.codehaus.waffle.validation.BindErrorMessage; /** @@ -61,6 +62,14 @@ write(INFO, "Method name '" + methodName + "' found for key '" + methodKey + "' among keys " + keys); } + public void argumentResolved(String name, Object value, Scope scope) { + write(INFO, "Argument name '" + name + "' resolved to '" + value + "' in scope " + scope); + } + + public void argumentNameNotMatched(String name, String pattern) { + write(WARN, "Argument name '" + name + "' not matched by pattern '" + pattern + "'" ); + } + public void actionMethodExecutionFailed(Exception exception) { trace(exception); }
Modified: trunk/core/src/main/java/org/codehaus/waffle/monitor/ActionMonitor.java (355 => 356)
--- trunk/core/src/main/java/org/codehaus/waffle/monitor/ActionMonitor.java 2007-11-12 19:46:19 UTC (rev 355) +++ trunk/core/src/main/java/org/codehaus/waffle/monitor/ActionMonitor.java 2007-11-12 20:32:20 UTC (rev 356) @@ -13,6 +13,7 @@ import java.util.Set; import org.codehaus.waffle.action.MethodDefinition; +import org.codehaus.waffle.action.HierarchicalArgumentResolver.Scope; /** * A monitor for action-related events @@ -29,7 +30,12 @@ void actionMethodFound(MethodDefinition methodDefinition); + void actionMethodExecutionFailed(Exception cause); + + void argumentResolved(String name, Object value, Scope scope); + + void argumentNameNotMatched(String name, String pattern); + void methodNameResolved(String methodName, String methodKey, Set<String> keys); - void actionMethodExecutionFailed(Exception cause); }
Modified: trunk/core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java (355 => 356)
--- trunk/core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java 2007-11-12 19:46:19 UTC (rev 355) +++ trunk/core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java 2007-11-12 20:32:20 UTC (rev 356) @@ -10,10 +10,18 @@ *****************************************************************************/ package org.codehaus.waffle.servlet; -import org.codehaus.waffle.ComponentRegistry; import static org.codehaus.waffle.Constants.ERRORS_KEY; import static org.codehaus.waffle.Constants.VIEW_PREFIX_KEY; import static org.codehaus.waffle.Constants.VIEW_SUFFIX_KEY; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.codehaus.waffle.ComponentRegistry; import org.codehaus.waffle.action.ActionMethodExecutor; import org.codehaus.waffle.action.ActionMethodInvocationException; import org.codehaus.waffle.action.ActionMethodResponse; @@ -23,62 +31,68 @@ import org.codehaus.waffle.bind.RequestAttributeBinder; import org.codehaus.waffle.controller.ControllerDefinition; import org.codehaus.waffle.controller.ControllerDefinitionFactory; +import org.codehaus.waffle.monitor.ActionMonitor; import org.codehaus.waffle.validation.DefaultErrorsContext; import org.codehaus.waffle.validation.ErrorsContext; import org.codehaus.waffle.validation.Validator; import org.codehaus.waffle.view.RedirectView; import org.codehaus.waffle.view.View; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - /** * Waffle's FrontController for handling user requests. * * @author Michael Ward + * @author Mauro Talevi */ [EMAIL PROTECTED]("serial") public class WaffleServlet extends HttpServlet { + private static final String DEFAULT_VIEW_SUFFIX = ".jspx"; private static final String DEFAULT_VIEW_PREFIX = "/"; private static final String EMPTY = ""; - private ControllerDefinitionFactory controllerDefinitionFactory; - private DataBinder dataBinder; private ActionMethodExecutor actionMethodExecutor; private ActionMethodResponseHandler actionMethodResponseHandler; + private ActionMonitor actionMonitor; + private DataBinder dataBinder; + private RequestAttributeBinder requestAttributeBinder; + private ControllerDefinitionFactory controllerDefinitionFactory; private Validator validator; - private RequestAttributeBinder requestAttributeBinder; private String viewPrefix; private String viewSuffix; private boolean componentsRetrieved = false; + /** + * Default constructor used by servlet container + */ public WaffleServlet() { + // initialisation will be performed in init() method } /** * Constructor required by builder and useful for testing * - * @param controllerDefinitionFactory - * @param dataBinder * @param actionMethodExecutor * @param actionMethodResponseHandler + * @param actionMonitor + * @param dataBinder + * @param requestAttributeBinder + * @param controllerDefinitionFactory * @param validator - * @param requestAttributeBinder */ - public WaffleServlet(ControllerDefinitionFactory controllerDefinitionFactory, + public WaffleServlet(ActionMethodExecutor actionMethodExecutor, + ActionMethodResponseHandler actionMethodResponseHandler, + ActionMonitor actionMonitor, DataBinder dataBinder, - ActionMethodExecutor actionMethodExecutor, - ActionMethodResponseHandler actionMethodResponseHandler, - Validator validator, - RequestAttributeBinder requestAttributeBinder) { - this.controllerDefinitionFactory = controllerDefinitionFactory; - this.dataBinder = dataBinder; + RequestAttributeBinder requestAttributeBinder, + ControllerDefinitionFactory controllerDefinitionFactory, + Validator validator) { this.actionMethodExecutor = actionMethodExecutor; this.actionMethodResponseHandler = actionMethodResponseHandler; + this.actionMonitor = actionMonitor; + this.dataBinder = dataBinder; + this.requestAttributeBinder = requestAttributeBinder; + this.controllerDefinitionFactory = controllerDefinitionFactory; this.validator = validator; - this.requestAttributeBinder = requestAttributeBinder; componentsRetrieved = true; } @@ -97,12 +111,13 @@ // Retrieve required components from the Component Registry ComponentRegistry componentRegistry = ServletContextHelper .getComponentRegistry(getServletContext()); - controllerDefinitionFactory = componentRegistry.getControllerDefinitionFactory(); - dataBinder = componentRegistry.getDataBinder(); actionMethodExecutor = componentRegistry.getActionMethodExecutor(); actionMethodResponseHandler = componentRegistry.getActionMethodResponseHandler(); + actionMonitor = componentRegistry.getActionMonitor(); + dataBinder = componentRegistry.getDataBinder(); + requestAttributeBinder = componentRegistry.getRequestAttributeBinder(); + controllerDefinitionFactory = componentRegistry.getControllerDefinitionFactory(); validator = componentRegistry.getValidator(); - requestAttributeBinder = componentRegistry.getRequestAttributeBinder(); } } @@ -167,6 +182,7 @@ requestAttributeBinder.bind(request, controllerDefinition.getController()); actionMethodResponseHandler.handle(request, response, actionMethodResponse); } catch (ActionMethodInvocationException e) { + actionMonitor.actionMethodExecutionFailed(e); log("ERROR: " + e.getMessage()); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); }
Modified: trunk/core/src/test/java/org/codehaus/waffle/action/HierarchicalArgumentResolverTest.java (355 => 356)
--- trunk/core/src/test/java/org/codehaus/waffle/action/HierarchicalArgumentResolverTest.java 2007-11-12 19:46:19 UTC (rev 355) +++ trunk/core/src/test/java/org/codehaus/waffle/action/HierarchicalArgumentResolverTest.java 2007-11-12 20:32:20 UTC (rev 356) @@ -6,6 +6,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; +import org.codehaus.waffle.monitor.SilentMonitor; import org.jmock.Expectations; import org.jmock.Mockery; import org.jmock.integration.junit4.JMock; @@ -24,7 +25,7 @@ @Test public void canResolveNameWhenArgumentNotInCurlyBrackets() { - ArgumentResolver argumentResolver = new HierarchicalArgumentResolver(null); + ArgumentResolver argumentResolver = new HierarchicalArgumentResolver(null, new SilentMonitor()); assertEquals("foobar", argumentResolver.resolve(null, "foobar")); } @@ -39,7 +40,7 @@ } }); - ArgumentResolver argumentResolver = new HierarchicalArgumentResolver(null); + ArgumentResolver argumentResolver = new HierarchicalArgumentResolver(null, new SilentMonitor()); assertEquals("bar", argumentResolver.resolve(request, "{foo}")); } @@ -56,7 +57,7 @@ } }); - ArgumentResolver argumentResolver = new HierarchicalArgumentResolver(null); + ArgumentResolver argumentResolver = new HierarchicalArgumentResolver(null, new SilentMonitor()); assertEquals("bar", argumentResolver.resolve(request, "{foo}")); } @@ -83,7 +84,7 @@ will(returnValue(session)); } }); - ArgumentResolver argumentResolver = new HierarchicalArgumentResolver(null); + ArgumentResolver argumentResolver = new HierarchicalArgumentResolver(null, new SilentMonitor()); assertEquals("bar", argumentResolver.resolve(request, "{foo}")); } @@ -120,7 +121,7 @@ } }); - ArgumentResolver argumentResolver = new HierarchicalArgumentResolver(servletContext); + ArgumentResolver argumentResolver = new HierarchicalArgumentResolver(servletContext, new SilentMonitor()); assertEquals("bar", argumentResolver.resolve(request, "{foo}")); } @@ -148,7 +149,7 @@ } }); - ArgumentResolver argumentResolver = new HierarchicalArgumentResolver(servletContext); + ArgumentResolver argumentResolver = new HierarchicalArgumentResolver(servletContext, new SilentMonitor()); assertEquals("bar", argumentResolver.resolve(request, "{foo}")); } }
Modified: trunk/core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java (355 => 356)
--- trunk/core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java 2007-11-12 19:46:19 UTC (rev 355) +++ trunk/core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java 2007-11-12 20:32:20 UTC (rev 356) @@ -41,6 +41,7 @@ import org.codehaus.waffle.context.pico.PicoContextContainer; import org.codehaus.waffle.controller.ControllerDefinition; import org.codehaus.waffle.controller.ControllerDefinitionFactory; +import org.codehaus.waffle.monitor.ActionMonitor; import org.codehaus.waffle.monitor.SilentMonitor; import org.codehaus.waffle.validation.ErrorsContext; import org.codehaus.waffle.validation.Validator; @@ -79,12 +80,13 @@ one(servletContext).getAttribute(ComponentRegistry.class.getName()); will(returnValue(componentRegistry)); // Component Registry... - one(componentRegistry).getControllerDefinitionFactory(); - one(componentRegistry).getDataBinder(); one(componentRegistry).getActionMethodExecutor(); one(componentRegistry).getActionMethodResponseHandler(); + one(componentRegistry).getActionMonitor(); + one(componentRegistry).getDataBinder(); + one(componentRegistry).getRequestAttributeBinder(); + one(componentRegistry).getControllerDefinitionFactory(); one(componentRegistry).getValidator(); - one(componentRegistry).getRequestAttributeBinder(); }}); WaffleServlet servlet = new WaffleServlet() { @@ -145,12 +147,12 @@ }}); // stub out what we don't want called ... execute it - WaffleServlet waffleServlet = new WaffleServlet(null, + WaffleServlet waffleServlet = new WaffleServlet(new InterceptingActionMethodExecutor(), + actionMethodResponseHandler, + new SilentMonitor(), new OgnlDataBinder(new DefaultTypeConverter(), null, new SilentMonitor()), - new InterceptingActionMethodExecutor(), - actionMethodResponseHandler, - validator, - requestAttributeBinder) { + requestAttributeBinder, + null, validator) { @Override protected ControllerDefinition getControllerDefinition(HttpServletRequest request, HttpServletResponse response) { return new ControllerDefinition("no name", nonDispatchingController, methodDefinition); @@ -206,12 +208,12 @@ }}); // stub out what we don't want called ... execute it - WaffleServlet waffleServlet = new WaffleServlet(null, + WaffleServlet waffleServlet = new WaffleServlet(new InterceptingActionMethodExecutor(), + actionMethodResponseHandler, + new SilentMonitor(), new OgnlDataBinder(new DefaultTypeConverter(), null, new SilentMonitor()), - new InterceptingActionMethodExecutor(), - actionMethodResponseHandler, - validator, - requestAttributeBinder) { + requestAttributeBinder, + null, validator) { @Override protected ControllerDefinition getControllerDefinition(HttpServletRequest request, HttpServletResponse response) { return new ControllerDefinition("no name", nonDispatchingController, methodDefinition); @@ -287,11 +289,11 @@ // stub out what we don't want called ... execute it WaffleServlet waffleServlet = new WaffleServlet(null, + actionMethodResponseHandler, + new SilentMonitor(), new OgnlDataBinder(new DefaultTypeConverter(), null, new SilentMonitor()), - null, - actionMethodResponseHandler, - validator, - requestAttributeBinder) { + requestAttributeBinder, + null, validator) { @Override protected ControllerDefinition getControllerDefinition(HttpServletRequest request, HttpServletResponse response) { return new ControllerDefinition("no name", nonDispatchingController, null); @@ -342,11 +344,18 @@ // Mock ActionMethodExecutor final ActionMethodExecutor actionMethodExecutor = mockery.mock(ActionMethodExecutor.class); + final ActionMethodInvocationException actionMethodInvocationException = new ActionMethodInvocationException("fake from test"); mockery.checking(new Expectations() {{ one(actionMethodExecutor).execute(with(any(ActionMethodResponse.class)), with(any(ControllerDefinition.class))); - will(throwException(new ActionMethodInvocationException("fake from test"))); + will(throwException(actionMethodInvocationException)); }}); + // Mock ActionMonitor + final ActionMonitor actionMonitor = mockery.mock(ActionMonitor.class); + mockery.checking(new Expectations() {{ + allowing(actionMonitor).actionMethodExecutionFailed(actionMethodInvocationException); + }}); + // Mock Validator final Validator validator = mockery.mock(Validator.class); mockery.checking(new Expectations() {{ @@ -362,6 +371,10 @@ mockMethodExecutorField.setAccessible(true); mockMethodExecutorField.set(waffleServlet, actionMethodExecutor); + Field mockMonitorField = WaffleServlet.class.getDeclaredField("actionMonitor"); + mockMonitorField.setAccessible(true); + mockMonitorField.set(waffleServlet, actionMonitor); + Field validatorFactoryField = WaffleServlet.class.getDeclaredField("validator"); validatorFactoryField.setAccessible(true); validatorFactoryField.set(waffleServlet, validator);
Modified: trunk/core/src/test/java/org/codehaus/waffle/testmodel/StubMonitor.java (355 => 356)
--- trunk/core/src/test/java/org/codehaus/waffle/testmodel/StubMonitor.java 2007-11-12 19:46:19 UTC (rev 355) +++ trunk/core/src/test/java/org/codehaus/waffle/testmodel/StubMonitor.java 2007-11-12 20:32:20 UTC (rev 356) @@ -3,6 +3,7 @@ import java.util.Set; import org.codehaus.waffle.action.MethodDefinition; +import org.codehaus.waffle.action.HierarchicalArgumentResolver.Scope; import org.codehaus.waffle.monitor.ActionMonitor; import org.codehaus.waffle.monitor.BindMonitor; import org.codehaus.waffle.validation.BindErrorMessage; @@ -27,9 +28,16 @@ public void actionMethodExecutionFailed(Exception exception) { } + public void argumentNameNotMatched(String name, String pattern) { + } + + public void argumentResolved(String name, Object value, Scope scope) { + } + public void bindFailed(Object bindModel, BindErrorMessage errorMessage) { } public void bindFailed(Object controller, Throwable cause) { } + }
To unsubscribe from this list please visit:
