No longer get/setApplicationContext(), now get/setApplicationAttribute().

git-svn-id: https://samskivert.googlecode.com/svn/trunk@540 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2002-01-24 06:32:38 +00:00
parent 3cb0209c52
commit 2d916f4b2b
5 changed files with 46 additions and 31 deletions
@@ -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 // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -23,6 +23,8 @@ package com.samskivert.velocity;
import javax.servlet.ServletConfig; import javax.servlet.ServletConfig;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import org.apache.velocity.app.Velocity;
import com.samskivert.Log; import com.samskivert.Log;
import com.samskivert.servlet.IndiscriminateSiteIdentifier; import com.samskivert.servlet.IndiscriminateSiteIdentifier;
import com.samskivert.servlet.MessageManager; import com.samskivert.servlet.MessageManager;
@@ -40,6 +42,14 @@ import com.samskivert.util.StringUtil;
*/ */
public class Application public class Application
{ {
/**
* An initialized application automatically registers itself as a
* Velocity application attribute so that it can be retrieved by
* Velocity plugins using
* <code>getApplicationAttribute(VELOCITY_ATTR_KEY)</code>.
*/
public static final String VELOCITY_ATTR_KEY = "!application!";
/** /**
* Performs initializations common to all applications. Applications * Performs initializations common to all applications. Applications
* should override {@link #willInit} to perform initializations that * should override {@link #willInit} to perform initializations that
@@ -63,6 +73,10 @@ public class Application
// keep this around for later // keep this around for later
_context = context; _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 // let the derived application do pre-init stuff
willInit(config); willInit(config);
@@ -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 // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -150,10 +150,6 @@ import com.samskivert.util.StringUtil;
*/ */
public class DispatcherServlet extends VelocityServlet public class DispatcherServlet extends VelocityServlet
{ {
/**
* We load our velocity properties from the classpath rather than from
* a file.
*/
/** /**
* Initialize ourselves and our application. * Initialize ourselves and our application.
*/ */
@@ -180,13 +176,14 @@ public class DispatcherServlet extends VelocityServlet
Log.logStackTrace(t); Log.logStackTrace(t);
} }
// stick the application into the Velocity application context
Velocity.setApplicationContext(_app);
// now let velocity initialize itself // now let velocity initialize itself
super.initVelocity(config); super.initVelocity(config);
} }
/**
* We load our velocity properties from the classpath rather than from
* a file.
*/
protected Properties loadConfiguration (ServletConfig config) protected Properties loadConfiguration (ServletConfig config)
throws IOException throws IOException
{ {
@@ -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 // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // 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 // if we weren't constructed with a servlet context, try to obtain
// one via the application context // one via the application context
if (_sctx == null) { if (_sctx == null) {
Object context = rsvc.getApplicationContext(); // first look for the servlet context directly
if (context != null) { _sctx = (ServletContext)
if (context instanceof Application) { rsvc.getApplicationAttribute("ServletContext");
_sctx = ((Application)context).getServletContext(); }
} else if (context instanceof ServletContext) {
_sctx = (ServletContext)context; // 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 we still don't have one, complain
if (_sctx == null) { if (_sctx == null) {
rsvc.warn("ServletContextLogger: servlet context was not " + rsvc.warn("ServletContextLogger: servlet context was not " +
"supplied as application context. A user of the " + "supplied. A user of the servlet context logger must " +
"servlet context logger must call " + "call Velocity.setApplicationAttribute(" +
"Velocity.setApplicationContext(getServletContext())."); "\"ServletContext\", getServletContext()).");
} }
} }
@@ -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 // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // 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 // the web framework was kind enough to slip this into the runtime
// instance when it started up // instance when it started up
_sctx = (ServletContext)rsvc.getApplicationContext(); _sctx = (ServletContext)rsvc.getApplicationAttribute("ServletContext");
if (_sctx == null) { if (_sctx == null) {
rsvc.warn("ServletContextResourceLoader: servlet context " + rsvc.warn("ServletContextResourceLoader: servlet context " +
"was not supplied as application context. A " + "was not supplied as application context. A " +
"user of the servlet context resource loader " + "user of the servlet context resource loader " +
"must call Velocity.setApplicationContext(" + "must call Velocity.setApplicationAttribute(" +
"getServletContext())."); "\"ServletContext\", getServletContext()).");
} }
rsvc.info("ServletContextResourceLoader : initialization complete."); rsvc.info("ServletContextResourceLoader : initialization complete.");
@@ -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 // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // 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 // the web framework was kind enough to slip this into the runtime
// instance when it started up // instance when it started up
Application app = (Application)rsvc.getApplicationContext(); Application app = (Application)rsvc.getApplicationAttribute(
Application.VELOCITY_ATTR_KEY);
if (app == null) { if (app == null) {
rsvc.warn("SiteResourceManager: Application reference " + rsvc.warn("SiteResourceManager: No application was initialized. " +
"was not supplied as application context. A " + "A user of the site resource manager must ensure that " +
"user of the site resource manager must supply " + "an application is instantiated and initialized.");
"a reference to the Application instance via " +
"Velocity.setApplicationContext().");
} }
// get handles on the good stuff // get handles on the good stuff