From 2d916f4b2b36b9fd9853d78d6ae1013719ed0bdc Mon Sep 17 00:00:00 2001 From: mdb Date: Thu, 24 Jan 2002 06:32:38 +0000 Subject: [PATCH] No longer get/setApplicationContext(), now get/setApplicationAttribute(). git-svn-id: https://samskivert.googlecode.com/svn/trunk@540 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/velocity/Application.java | 16 ++++++++++- .../velocity/DispatcherServlet.java | 13 ++++----- .../velocity/ServletContextLogger.java | 27 +++++++++++-------- .../ServletContextResourceLoader.java | 8 +++--- .../velocity/SiteResourceManager.java | 13 +++++---- 5 files changed, 46 insertions(+), 31 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/velocity/Application.java b/projects/samskivert/src/java/com/samskivert/velocity/Application.java index 81c20442..ae1fbf6a 100644 --- a/projects/samskivert/src/java/com/samskivert/velocity/Application.java +++ b/projects/samskivert/src/java/com/samskivert/velocity/Application.java @@ -1,5 +1,5 @@ // -// $Id: Application.java,v 1.7 2001/11/06 20:55:51 mdb Exp $ +// $Id: Application.java,v 1.8 2002/01/24 06:32:38 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -23,6 +23,8 @@ package com.samskivert.velocity; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; +import org.apache.velocity.app.Velocity; + import com.samskivert.Log; import com.samskivert.servlet.IndiscriminateSiteIdentifier; import com.samskivert.servlet.MessageManager; @@ -40,6 +42,14 @@ import com.samskivert.util.StringUtil; */ public class Application { + /** + * An initialized application automatically registers itself as a + * Velocity application attribute so that it can be retrieved by + * Velocity plugins using + * getApplicationAttribute(VELOCITY_ATTR_KEY). + */ + public static final String VELOCITY_ATTR_KEY = "!application!"; + /** * Performs initializations common to all applications. Applications * should override {@link #willInit} to perform initializations that @@ -63,6 +73,10 @@ public class Application // keep this around for later _context = context; + // stick ourselves into an application attribute so that we can be + // accessed by Velocity plugins + Velocity.setApplicationAttribute(VELOCITY_ATTR_KEY, this); + // let the derived application do pre-init stuff willInit(config); diff --git a/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java b/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java index 62f3806f..e0985fce 100644 --- a/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java +++ b/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java @@ -1,5 +1,5 @@ // -// $Id: DispatcherServlet.java,v 1.13 2001/11/20 21:33:03 mdb Exp $ +// $Id: DispatcherServlet.java,v 1.14 2002/01/24 06:32:38 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -150,10 +150,6 @@ import com.samskivert.util.StringUtil; */ public class DispatcherServlet extends VelocityServlet { - /** - * We load our velocity properties from the classpath rather than from - * a file. - */ /** * Initialize ourselves and our application. */ @@ -180,13 +176,14 @@ public class DispatcherServlet extends VelocityServlet Log.logStackTrace(t); } - // stick the application into the Velocity application context - Velocity.setApplicationContext(_app); - // now let velocity initialize itself super.initVelocity(config); } + /** + * We load our velocity properties from the classpath rather than from + * a file. + */ protected Properties loadConfiguration (ServletConfig config) throws IOException { diff --git a/projects/samskivert/src/java/com/samskivert/velocity/ServletContextLogger.java b/projects/samskivert/src/java/com/samskivert/velocity/ServletContextLogger.java index 7f227e06..4f94b559 100644 --- a/projects/samskivert/src/java/com/samskivert/velocity/ServletContextLogger.java +++ b/projects/samskivert/src/java/com/samskivert/velocity/ServletContextLogger.java @@ -1,5 +1,5 @@ // -// $Id: ServletContextLogger.java,v 1.3 2001/11/20 21:32:34 mdb Exp $ +// $Id: ServletContextLogger.java,v 1.4 2002/01/24 06:32:38 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -54,22 +54,27 @@ public class ServletContextLogger implements LogSystem // if we weren't constructed with a servlet context, try to obtain // one via the application context if (_sctx == null) { - Object context = rsvc.getApplicationContext(); - if (context != null) { - if (context instanceof Application) { - _sctx = ((Application)context).getServletContext(); - } else if (context instanceof ServletContext) { - _sctx = (ServletContext)context; - } + // first look for the servlet context directly + _sctx = (ServletContext) + rsvc.getApplicationAttribute("ServletContext"); + } + + // if that didn't work, look for an application + if (_sctx == null) { + // first look for an application + Application app = (Application)rsvc.getApplicationAttribute( + Application.VELOCITY_ATTR_KEY); + if (app != null) { + _sctx = app.getServletContext(); } } // if we still don't have one, complain if (_sctx == null) { rsvc.warn("ServletContextLogger: servlet context was not " + - "supplied as application context. A user of the " + - "servlet context logger must call " + - "Velocity.setApplicationContext(getServletContext())."); + "supplied. A user of the servlet context logger must " + + "call Velocity.setApplicationAttribute(" + + "\"ServletContext\", getServletContext())."); } } diff --git a/projects/samskivert/src/java/com/samskivert/velocity/ServletContextResourceLoader.java b/projects/samskivert/src/java/com/samskivert/velocity/ServletContextResourceLoader.java index b72f1751..2ff0a99d 100644 --- a/projects/samskivert/src/java/com/samskivert/velocity/ServletContextResourceLoader.java +++ b/projects/samskivert/src/java/com/samskivert/velocity/ServletContextResourceLoader.java @@ -1,5 +1,5 @@ // -// $Id: ServletContextResourceLoader.java,v 1.3 2001/11/06 04:49:32 mdb Exp $ +// $Id: ServletContextResourceLoader.java,v 1.4 2002/01/24 06:32:38 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -67,13 +67,13 @@ public class ServletContextResourceLoader extends ResourceLoader // the web framework was kind enough to slip this into the runtime // instance when it started up - _sctx = (ServletContext)rsvc.getApplicationContext(); + _sctx = (ServletContext)rsvc.getApplicationAttribute("ServletContext"); if (_sctx == null) { rsvc.warn("ServletContextResourceLoader: servlet context " + "was not supplied as application context. A " + "user of the servlet context resource loader " + - "must call Velocity.setApplicationContext(" + - "getServletContext())."); + "must call Velocity.setApplicationAttribute(" + + "\"ServletContext\", getServletContext())."); } rsvc.info("ServletContextResourceLoader : initialization complete."); diff --git a/projects/samskivert/src/java/com/samskivert/velocity/SiteResourceManager.java b/projects/samskivert/src/java/com/samskivert/velocity/SiteResourceManager.java index 2a0c6bee..f19852b8 100644 --- a/projects/samskivert/src/java/com/samskivert/velocity/SiteResourceManager.java +++ b/projects/samskivert/src/java/com/samskivert/velocity/SiteResourceManager.java @@ -1,5 +1,5 @@ // -// $Id: SiteResourceManager.java,v 1.4 2001/11/06 20:16:47 mdb Exp $ +// $Id: SiteResourceManager.java,v 1.5 2002/01/24 06:32:38 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -55,13 +55,12 @@ public class SiteResourceManager extends ResourceManagerImpl // the web framework was kind enough to slip this into the runtime // instance when it started up - Application app = (Application)rsvc.getApplicationContext(); + Application app = (Application)rsvc.getApplicationAttribute( + Application.VELOCITY_ATTR_KEY); if (app == null) { - rsvc.warn("SiteResourceManager: Application reference " + - "was not supplied as application context. A " + - "user of the site resource manager must supply " + - "a reference to the Application instance via " + - "Velocity.setApplicationContext()."); + rsvc.warn("SiteResourceManager: No application was initialized. " + + "A user of the site resource manager must ensure that " + + "an application is instantiated and initialized."); } // get handles on the good stuff