From 170422e1f1306a86bb2465af4f2721e5b87a622f Mon Sep 17 00:00:00 2001 From: mdb Date: Sun, 4 Mar 2001 06:37:51 +0000 Subject: [PATCH] Modified things further. Moved message manager management entirely into the default application. Derived applications now simply provide a message bundle path if they want a message manager. Also now pass an application reference to the logic instances when they are invoked so that they can easily access application-wide resources. git-svn-id: https://samskivert.googlecode.com/svn/trunk@91 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/webmacro/Application.java | 129 +++++++++++------- .../webmacro/DispatcherServlet.java | 6 +- .../java/com/samskivert/webmacro/Logic.java | 6 +- 3 files changed, 85 insertions(+), 56 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/webmacro/Application.java b/projects/samskivert/src/java/com/samskivert/webmacro/Application.java index a203a755..41c4a0ec 100644 --- a/projects/samskivert/src/java/com/samskivert/webmacro/Application.java +++ b/projects/samskivert/src/java/com/samskivert/webmacro/Application.java @@ -1,5 +1,5 @@ // -// $Id: Application.java,v 1.2 2001/03/04 06:25:48 mdb Exp $ +// $Id: Application.java,v 1.3 2001/03/04 06:37:51 mdb Exp $ package com.samskivert.webmacro; @@ -36,24 +36,84 @@ public class Application } /** - * If an application wishes to make use of the translation facilities - * provided by the message manager, it can instantiate one and make it - * available through this member function. This allows framework - * components like the MsgTool to make use of the - * application's message bundles. + * Returns the message manager in effect for this application, if one + * is in effect. */ public MessageManager getMessageManager () + { + return _msgmgr; + } + + /** + * If an application wishes to make use of the translation facilities + * provided by the message manager, it need only provide the path to + * its message resource bundle via this member function. Using a + * message manager allows framework components like the + * MsgTool to make use of the application's message + * bundles. + */ + protected String getMessageBundlePath () { return null; } /** - * The default application implementation takes the base URI and base - * package as defined in the application declaration in the servlet - * configuration and uses those to determine whether or not a given - * URI maps to this application. + * A convenience function for translating messages. */ - public void setConfig (String baseURI, String basePkg) + public final String translate (WebContext ctx, String msg) + { + return _msgmgr.getMessage(ctx.getRequest(), msg); + } + + /** + * A convenience function for translating messages. + */ + public final String translate (WebContext ctx, String msg, Object arg) + { + return _msgmgr.getMessage(ctx.getRequest(), msg, + new Object[]{ arg }); + } + + /** + * A convenience function for translating messages. + */ + public final String translate (WebContext ctx, String msg, + Object arg1, Object arg2) + { + return _msgmgr.getMessage(ctx.getRequest(), msg, + new Object[]{ arg1, arg2 }); + } + + /** + * A convenience function for translating messages. + */ + public final String translate (WebContext ctx, String msg, + Object arg1, Object arg2, Object arg3) + { + return _msgmgr.getMessage(ctx.getRequest(), msg, + new Object[]{ arg1, arg2, arg3 }); + } + + /** + * A convenience function for translating messages. + */ + public final String translate (WebContext ctx, String msg, + Object[] args) + { + return _msgmgr.getMessage(ctx.getRequest(), msg, args); + } + + /** + * Performs initializations common to all applications. We could put + * this in init() and require application derived classes to call + * super.init() but people would forget to do so and be confused. + * + *

The default application implementation takes the base URI and + * base package as defined in the application declaration in the + * servlet configuration and uses those to determine whether or not a + * given URI maps to this application. + */ + public void preInit (String baseURI, String basePkg) { // remove any trailing slash if (baseURI.endsWith("/")) { @@ -68,6 +128,12 @@ public class Application } else { _basePkg = basePkg; } + + // instantiate our message manager if the application wants one + String bpath = getMessageBundlePath(); + if (bpath != null) { + _msgmgr = new MessageManager(bpath); + } } /** @@ -95,48 +161,9 @@ public class Application return _basePkg + uri; } - /** - * A convenience function for translating messages. - */ - public final String translate (WebContext ctx, String msg) - { - MessageManager msgmgr = getMessageManager(); - return msgmgr.getMessage(ctx.getRequest(), msg); - } - - /** - * A convenience function for translating messages. - */ - public final String translate (WebContext ctx, String msg, Object arg) - { - MessageManager msgmgr = getMessageManager(); - return msgmgr.getMessage(ctx.getRequest(), msg, new Object[]{ arg }); - } - - /** - * A convenience function for translating messages. - */ - public final String translate (WebContext ctx, String msg, Object arg1, - Object arg2) - { - MessageManager msgmgr = getMessageManager(); - return msgmgr.getMessage(ctx.getRequest(), msg, - new Object[]{ arg1, arg2 }); - } - - /** - * A convenience function for translating messages. - */ - public final String translate (WebContext ctx, String msg, Object arg1, - Object arg2, Object arg3) - { - MessageManager msgmgr = getMessageManager(); - return msgmgr.getMessage(ctx.getRequest(), msg, - new Object[]{ arg1, arg2, arg3 }); - } - protected String _baseURI; protected String _basePkg; + protected MessageManager _msgmgr; /** * This is the default file extension. diff --git a/projects/samskivert/src/java/com/samskivert/webmacro/DispatcherServlet.java b/projects/samskivert/src/java/com/samskivert/webmacro/DispatcherServlet.java index e9ca4021..2100808d 100644 --- a/projects/samskivert/src/java/com/samskivert/webmacro/DispatcherServlet.java +++ b/projects/samskivert/src/java/com/samskivert/webmacro/DispatcherServlet.java @@ -1,5 +1,5 @@ // -// $Id: DispatcherServlet.java,v 1.8 2001/03/04 06:22:17 mdb Exp $ +// $Id: DispatcherServlet.java,v 1.9 2001/03/04 06:37:51 mdb Exp $ package com.samskivert.webmacro; @@ -186,7 +186,7 @@ public class DispatcherServlet extends WMServlet } // now initialize the applicaiton - app.setConfig(baseURI, basePkg); + app.preInit(baseURI, basePkg); app.init(getServletContext()); // finally add it to our list @@ -240,7 +240,7 @@ public class DispatcherServlet extends WMServlet // execute it if it exists Logic logic = resolveLogic(app, path); if (logic != null) { - logic.invoke(ctx); + logic.invoke(app, ctx); } } diff --git a/projects/samskivert/src/java/com/samskivert/webmacro/Logic.java b/projects/samskivert/src/java/com/samskivert/webmacro/Logic.java index 28a0c74f..4e37cb65 100644 --- a/projects/samskivert/src/java/com/samskivert/webmacro/Logic.java +++ b/projects/samskivert/src/java/com/samskivert/webmacro/Logic.java @@ -1,5 +1,5 @@ // -// $Id: Logic.java,v 1.1 2001/03/01 21:06:22 mdb Exp $ +// $Id: Logic.java,v 1.2 2001/03/04 06:37:51 mdb Exp $ package com.samskivert.webmacro; @@ -22,9 +22,11 @@ public interface Logic * converted into friendly error messages using the exception mapping * services. * + * @param app The application that generated this logic instance (used + * to access application-wide resources). * @param context The WebMacro context in scope for this request. * * @see ExceptionMap */ - public void invoke (WebContext context) throws Exception; + public void invoke (Application app, WebContext context) throws Exception; }