Modified dispatcher servlet to stick servlet context into Velocity's
application context and implemented a servlet context resource loader which loads resources via ServletContext.getResourceAsStream(). git-svn-id: https://samskivert.googlecode.com/svn/trunk@414 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: DispatcherServlet.java,v 1.5 2001/11/01 21:34:03 mdb Exp $
|
// $Id: DispatcherServlet.java,v 1.6 2001/11/02 02:03:23 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
|
||||||
@@ -25,11 +25,13 @@ import java.util.HashMap;
|
|||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import javax.servlet.ServletConfig;
|
import javax.servlet.ServletConfig;
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.apache.velocity.Template;
|
import org.apache.velocity.Template;
|
||||||
|
import org.apache.velocity.app.Velocity;
|
||||||
import org.apache.velocity.context.Context;
|
import org.apache.velocity.context.Context;
|
||||||
import org.apache.velocity.exception.ParseErrorException;
|
import org.apache.velocity.exception.ParseErrorException;
|
||||||
import org.apache.velocity.exception.ResourceNotFoundException;
|
import org.apache.velocity.exception.ResourceNotFoundException;
|
||||||
@@ -167,6 +169,10 @@ public class DispatcherServlet extends VelocityServlet
|
|||||||
|
|
||||||
// Log.info("Initializing dispatcher servlet.");
|
// Log.info("Initializing dispatcher servlet.");
|
||||||
|
|
||||||
|
// initialize the Velocity application context
|
||||||
|
ServletContext sctx = getServletContext();
|
||||||
|
Velocity.setApplicationContext(sctx);
|
||||||
|
|
||||||
// load up our application configuration
|
// load up our application configuration
|
||||||
try {
|
try {
|
||||||
String appcl = config.getInitParameter(APP_CLASS_PROPS_KEY);
|
String appcl = config.getInitParameter(APP_CLASS_PROPS_KEY);
|
||||||
@@ -182,8 +188,8 @@ public class DispatcherServlet extends VelocityServlet
|
|||||||
if (StringUtil.blank(logicPkg)) {
|
if (StringUtil.blank(logicPkg)) {
|
||||||
logicPkg = "";
|
logicPkg = "";
|
||||||
}
|
}
|
||||||
_app.init(getServletContext());
|
_app.init(sctx);
|
||||||
_app.postInit(getServletContext(), logicPkg);
|
_app.postInit(sctx, logicPkg);
|
||||||
|
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
Log.warning("Error instantiating application.");
|
Log.warning("Error instantiating application.");
|
||||||
|
|||||||
+104
@@ -0,0 +1,104 @@
|
|||||||
|
//
|
||||||
|
// $Id: ServletContextResourceLoader.java,v 1.1 2001/11/02 02:03:23 mdb Exp $
|
||||||
|
//
|
||||||
|
// samskivert library - useful routines for java programs
|
||||||
|
// Copyright (C) 2001 Michael Bayne
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU Lesser General Public License as published
|
||||||
|
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
package com.samskivert.velocity;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
|
||||||
|
import org.apache.commons.collections.ExtendedProperties;
|
||||||
|
import org.apache.velocity.exception.ResourceNotFoundException;
|
||||||
|
import org.apache.velocity.runtime.RuntimeServices;
|
||||||
|
import org.apache.velocity.runtime.resource.Resource;
|
||||||
|
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Velocity resource loader that loads resources from the servlet
|
||||||
|
* context.
|
||||||
|
*
|
||||||
|
* @see ServletContext#getResource
|
||||||
|
* @see ServletContext#getResourceAsStream
|
||||||
|
*/
|
||||||
|
public class ServletContextResourceLoader extends ResourceLoader
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Called by Velocity to initialize this resource loader.
|
||||||
|
*/
|
||||||
|
public void init (ExtendedProperties config)
|
||||||
|
{
|
||||||
|
// there seems to be a tradition of logging this stuff; surely we
|
||||||
|
// don't want to buck tradition, do we?
|
||||||
|
rsvc.info("ServletContextResourceLoader: initialization starting.");
|
||||||
|
|
||||||
|
// the web framework was kind enough to slip this into the runtime
|
||||||
|
// instance when it started up
|
||||||
|
_sctx = (ServletContext)rsvc.getApplicationContext();
|
||||||
|
|
||||||
|
rsvc.info("ServletContextResourceLoader : initialization complete.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the input stream that can be used to load the named
|
||||||
|
* resource.
|
||||||
|
*
|
||||||
|
* @param path the path (relative to the webapp root) of resource to
|
||||||
|
* get.
|
||||||
|
*
|
||||||
|
* @return an input stream that can be used to read the resource.
|
||||||
|
*
|
||||||
|
* @exception ResourceNotFoundException if the resource was not found.
|
||||||
|
*/
|
||||||
|
public InputStream getResourceStream (String path)
|
||||||
|
throws ResourceNotFoundException
|
||||||
|
{
|
||||||
|
InputStream stream = _sctx.getResourceAsStream(path);
|
||||||
|
if (stream == null) {
|
||||||
|
String errmsg = "Unable to load resource via servlet context " +
|
||||||
|
"[path=" + path + "].";
|
||||||
|
throw new ResourceNotFoundException(errmsg);
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Things won't ever be modified when loaded from the servlet context
|
||||||
|
* because they came from the webapp .war file and if that is
|
||||||
|
* reloaded, everything will be thrown away and started afresh.
|
||||||
|
*/
|
||||||
|
public boolean isSourceModified (Resource resource)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Things won't ever be modified when loaded from the servlet context
|
||||||
|
* because they came from the webapp .war file and if that is
|
||||||
|
* reloaded, everything will be thrown away and started afresh. So we
|
||||||
|
* can punt here and return zero.
|
||||||
|
*/
|
||||||
|
public long getLastModified (Resource resource)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A reference to the servlet context through which we'll load
|
||||||
|
* things. */
|
||||||
|
protected ServletContext _sctx;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user