Index: src/java/org/nanocontainer/nanoweb/ChainingDispatcher.java =================================================================== RCS file: /home/projects/picocontainer/scm/java/nanoweb/src/java/org/nanocontainer/nanoweb/ChainingDispatcher.java,v retrieving revision 1.3 diff -u -r1.3 ChainingDispatcher.java --- src/java/org/nanocontainer/nanoweb/ChainingDispatcher.java 29 Mar 2004 20:30:29 -0000 1.3 +++ src/java/org/nanocontainer/nanoweb/ChainingDispatcher.java 27 Apr 2004 19:02:08 -0000 @@ -14,8 +14,14 @@ * @version $Revision: 1.3 $ */ public class ChainingDispatcher implements Dispatcher { + private final String extension; + + public ChainingDispatcher(String extension) { + this.extension = extension; + } + public void dispatch(ServletContext servletContext, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String scriptPathWithoutExtension, String actionMethod, String result) throws IOException, ServletException { - String[] views = getViews(scriptPathWithoutExtension, actionMethod, result, ".vm"); + String[] views = getViews(scriptPathWithoutExtension, actionMethod, result); boolean didDispatch = false; for (int i = 0; i < views.length; i++) { @@ -37,32 +43,32 @@ } } - String[] getViews(String scriptPathWithoutExtension, String actionMethod, String result, String extension) { + String[] getViews(String scriptPathWithoutExtension, String actionMethod, String result) { String[] views = new String[4]; - views[0] = getScriptPathUnderscoreActionNameUnderscoreResultView(scriptPathWithoutExtension, actionMethod, result, extension); - views[1] = getScriptPathUnderscoreResultView(scriptPathWithoutExtension, result, extension); - views[2] = getActionFolderPathResultView(scriptPathWithoutExtension, result, extension); - views[3] = getActionRootResultView(result, extension); + views[0] = getScriptPathUnderscoreActionNameUnderscoreResultView(scriptPathWithoutExtension, actionMethod, result); + views[1] = getScriptPathUnderscoreResultView(scriptPathWithoutExtension, result); + views[2] = getActionFolderPathResultView(scriptPathWithoutExtension, result); + views[3] = getActionRootResultView(result); return views; } - private String getScriptPathUnderscoreResultView(String scriptPathWithoutExtension, String result, String extension) { + private String getScriptPathUnderscoreResultView(String scriptPathWithoutExtension, String result) { return scriptPathWithoutExtension + "_" + result + extension; } - private String getScriptPathUnderscoreActionNameUnderscoreResultView(String scriptPathWithoutExtension, String actionMethod, String result, String extension) { + private String getScriptPathUnderscoreActionNameUnderscoreResultView(String scriptPathWithoutExtension, String actionMethod, String result) { return scriptPathWithoutExtension + "_" + actionMethod + "_" + result + extension; } - private String getActionFolderPathResultView(String scriptPathWithoutExtension, String result, String extension) { + private String getActionFolderPathResultView(String scriptPathWithoutExtension, String result) { String actionFolderPath = scriptPathWithoutExtension.substring(0, scriptPathWithoutExtension.lastIndexOf("/") + 1); String view = actionFolderPath + result + extension; return view; } - private String getActionRootResultView(String result, String extension) { + private String getActionRootResultView(String result) { return "/" + result + extension; } Index: src/java/org/nanocontainer/nanoweb/NanoWebServlet.java =================================================================== RCS file: /home/projects/picocontainer/scm/java/nanoweb/src/java/org/nanocontainer/nanoweb/NanoWebServlet.java,v retrieving revision 1.7 diff -u -r1.7 NanoWebServlet.java --- src/java/org/nanocontainer/nanoweb/NanoWebServlet.java 29 Mar 2004 20:30:29 -0000 1.7 +++ src/java/org/nanocontainer/nanoweb/NanoWebServlet.java 27 Apr 2004 19:02:08 -0000 @@ -2,6 +2,7 @@ import ognl.Ognl; import ognl.OgnlException; +import org.nanocontainer.servlet.ApplicationScopeObjectReference; import org.nanocontainer.servlet.KeyConstants; import org.nanocontainer.servlet.RequestScopeObjectReference; import org.nanocontainer.servlet.ServletRequestContainerLauncher; @@ -47,10 +48,23 @@ */ public class NanoWebServlet extends HttpServlet implements KeyConstants { + private Dispatcher dispatcher; // TODO make this configurable from web.xml - private final Dispatcher dispatcher = new ChainingDispatcher(); private final CachingScriptClassLoader cachingScriptClassLoader = new CachingScriptClassLoader(); + public void init() throws ServletException { + ServletContext servletContext = getServletContext(); + MutablePicoContainer applicationContainer = getApplicationContainer(servletContext); + initDispatcher(applicationContainer); + } + + private void initDispatcher(MutablePicoContainer applicationContainer) { + dispatcher = (Dispatcher) applicationContainer.getComponentInstanceOfType(Dispatcher.class); + if (dispatcher == null) { + dispatcher = new ChainingDispatcher(".vm"); + } + } + protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { ServletRequestContainerLauncher containerLauncher = new ServletRequestContainerLauncher(getServletContext(), httpServletRequest); try { @@ -169,6 +183,11 @@ return servletPath; } + private MutablePicoContainer getApplicationContainer(ServletContext context) { + ObjectReference ref = new ApplicationScopeObjectReference(context, APPLICATION_CONTAINER); + return (MutablePicoContainer) ref.get(); + } + private MutablePicoContainer getRequestContainer(ServletRequest request) { ObjectReference ref = new RequestScopeObjectReference(request, REQUEST_CONTAINER); return (MutablePicoContainer) ref.get(); Index: src/test/org/nanocontainer/nanoweb/ChainingDispatcherTestCase.java =================================================================== RCS file: /home/projects/picocontainer/scm/java/nanoweb/src/test/org/nanocontainer/nanoweb/ChainingDispatcherTestCase.java,v retrieving revision 1.2 diff -u -r1.2 ChainingDispatcherTestCase.java --- src/test/org/nanocontainer/nanoweb/ChainingDispatcherTestCase.java 29 Mar 2004 20:30:29 -0000 1.2 +++ src/test/org/nanocontainer/nanoweb/ChainingDispatcherTestCase.java 27 Apr 2004 19:02:08 -0000 @@ -9,8 +9,8 @@ public class ChainingDispatcherTestCase extends TestCase { public void testDispatcherChain() { - ChainingDispatcher dispatcher = new ChainingDispatcher(); - String[] views = dispatcher.getViews("/foo/bar", "zap", "success", ".vm"); + ChainingDispatcher dispatcher = new ChainingDispatcher(".vm"); + String[] views = dispatcher.getViews("/foo/bar", "zap", "success"); assertEquals( "/foo/bar_zap_success.vm", views[0]); assertEquals( "/foo/bar_success.vm", views[1]); assertEquals( "/foo/success.vm", views[2]); Index: src/test/org/nanocontainer/nanoweb/NanoWebServletTestCase.java =================================================================== RCS file: /home/projects/picocontainer/scm/java/nanoweb/src/test/org/nanocontainer/nanoweb/NanoWebServletTestCase.java,v retrieving revision 1.6 diff -u -r1.6 NanoWebServletTestCase.java --- src/test/org/nanocontainer/nanoweb/NanoWebServletTestCase.java 30 Mar 2004 11:46:02 -0000 1.6 +++ src/test/org/nanocontainer/nanoweb/NanoWebServletTestCase.java 27 Apr 2004 19:02:08 -0000 @@ -34,6 +34,7 @@ */ public class NanoWebServletTestCase extends TestCase { private NanoWebServlet nanoServlet; + private MutablePicoContainer applicationContainer; private MutablePicoContainer requestContainer; private Mock requestMock; @@ -51,10 +52,13 @@ servletContextMock = new Mock(ServletContext.class); servletConfigMock = new Mock(ServletConfig.class); servletConfigMock.expectAndReturn("getServletContext", servletContextMock.proxy()); + servletConfigMock.expectAndReturn("getServletContext", servletContextMock.proxy()); servletConfigMock.expectAndReturn("getServletName", "NanoWeb"); servletConfigMock.expectAndReturn("getServletContext", servletContextMock.proxy()); servletConfigMock.expectAndReturn("getServletContext", servletContextMock.proxy()); servletContextMock.expect("log", C.args(C.eq("NanoWeb: init"))); + applicationContainer = new DefaultPicoContainer(); + servletContextMock.expectAndReturn("getAttribute", C.args(C.eq(KeyConstants.APPLICATION_CONTAINER)), applicationContainer); servletContextMock.expectAndReturn("getResource", C.args(C.eq("/test_doit_success.vm")), new URL("http://dummy/")); containerBuilderMock = new Mock(ContainerBuilder.class); servletContextMock.expectAndReturn("getAttribute", C.args(C.eq(KeyConstants.BUILDER)), containerBuilderMock.proxy());