Author: nbubna
Date: Wed Sep 17 23:24:13 2008
New Revision: 696572
URL: http://svn.apache.org/viewvc?rev=696572&view=rev
Log:
refactor to bridge the gap between ToolManager and VelocityView with a new
ViewToolManager class for those who need easier tool mgmt in webapps but don't
need VelocityView's template processing or advanced config conventions
Added:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java
(with props)
Modified:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ToolContext.java
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ToolManager.java
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ServletUtils.java
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityView.java
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityViewFilter.java
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ViewToolContext.java
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/jsp/VelocityViewTag.java
Modified:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ToolContext.java
URL:
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ToolContext.java?rev=696572&r1=696571&r2=696572&view=diff
==============================================================================
---
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ToolContext.java
(original)
+++
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ToolContext.java
Wed Sep 17 23:24:13 2008
@@ -46,61 +46,41 @@
public static final String LOG_KEY = "log";
public static final String CATCH_EXCEPTIONS_KEY = "catchExceptions";
- private List<Toolbox> toolboxes;
+ private List<Toolbox> toolboxes = new ArrayList<Toolbox>();
// this is meant solely for tool setup,
// values in here are not part of the Context
- private Map<String,Object> toolProps;
+ private Map<String,Object> toolProps = new HashMap<String,Object>(12);
// this is only for values added during use of this context
- private Map<String,Object> localContext;
+ private Map<String,Object> localContext = new HashMap<String,Object>();
- public ToolContext(VelocityEngine engine)
+ public ToolContext()
{
- this(null, null);
-
- // add the engine and log as common tool properties
- putToolProperty(ENGINE_KEY, engine);
- putToolProperty(LOG_KEY, engine.getLog());
-
- // tell interested tools not to catch exceptions whenever there's a
- // method exception event handler configured for the engine
- Object ehme =
- engine.getProperty(VelocityEngine.EVENTHANDLER_METHODEXCEPTION);
- if (ehme != null)
- {
- putToolProperty(CATCH_EXCEPTIONS_KEY, Boolean.FALSE);
- }
+ // add this as a common tool property
+ putToolProperty(CONTEXT_KEY, this);
}
- public ToolContext(Map<String,Object> toolProps)
+ /**
+ * Creates an instance that automatically has the specified
+ * VelocityEngine and related tool properties set.
+ */
+ public ToolContext(VelocityEngine engine)
{
- this(toolProps, null);
- }
+ this();
- public ToolContext(Toolbox toolbox)
- {
- this(null, toolbox);
+ putVelocityEngine(engine);
}
- public ToolContext(Map<String,Object> toolProps, Toolbox toolbox)
+ /**
+ * Creates an instance starting with the specified tool properties.
+ */
+ public ToolContext(Map<String,Object> toolProps)
{
- if (toolProps != null)
- {
- this.toolProps = toolProps;
- }
- else
- {
- this.toolProps = new HashMap<String,Object>(8);
- }
- // add this as a common tool property
- putToolProperty(CONTEXT_KEY, this);
+ this();
- toolboxes = new ArrayList<Toolbox>();
- if (toolbox != null)
+ if (toolProps != null)
{
- toolboxes.add(toolbox);
+ this.toolProps.putAll(toolProps);
}
-
- this.localContext = new HashMap<String,Object>();
}
public void addToolbox(Toolbox toolbox)
@@ -152,6 +132,29 @@
return this.toolProps;
}
+ /**
+ * Puts the specified VelocityEngine in the tool properties,
+ * as well as the Log for that engine. Last, if the specified
+ * engine has a MethodExceptionEventHandler configured, then
+ * this will automatically set [EMAIL PROTECTED] #CATCH_EXCEPTIONS_KEY}
+ * to false in the tool properties.
+ */
+ public void putVelocityEngine(VelocityEngine engine)
+ {
+ // add the engine and log as common tool properties
+ putToolProperty(ENGINE_KEY, engine);
+ putToolProperty(LOG_KEY, engine.getLog());
+
+ // tell interested tools not to catch exceptions whenever there's a
+ // method exception event handler configured for the engine
+ Object ehme =
+ engine.getProperty(VelocityEngine.EVENTHANDLER_METHODEXCEPTION);
+ if (ehme != null)
+ {
+ putToolProperty(CATCH_EXCEPTIONS_KEY, Boolean.FALSE);
+ }
+ }
+
public Object putToolProperty(String key, Object value)
{
return toolProps.put(key, value);
Modified:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ToolManager.java
URL:
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ToolManager.java?rev=696572&r1=696571&r2=696572&view=diff
==============================================================================
---
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ToolManager.java
(original)
+++
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/ToolManager.java
Wed Sep 17 23:24:13 2008
@@ -21,6 +21,7 @@
import java.util.Map;
import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.runtime.log.Log;
import org.apache.velocity.tools.Scope;
import org.apache.velocity.tools.config.ConfigurationUtils;
import org.apache.velocity.tools.config.FactoryConfiguration;
@@ -37,25 +38,38 @@
*/
public class ToolManager
{
- private VelocityEngine engine;
- private ToolboxFactory factory;
+ protected VelocityEngine velocity;
+ protected ToolboxFactory factory;
private Toolbox application;
/**
* Constructs an instance already configured to use the
- * [EMAIL PROTECTED] ConfigurationUtils#getAutoLoaded()()} configuration
+ * [EMAIL PROTECTED] ConfigurationUtils#getAutoLoaded()} configuration
* and any configuration specified via a "org.apache.velocity.tools"
* system property.
*/
public ToolManager()
{
- this(true);
+ this(true, true);
}
public ToolManager(boolean includeDefaults)
{
+ this(true, includeDefaults);
+ }
+
+ public ToolManager(boolean autoConfig, boolean includeDefaults)
+ {
this.factory = new ToolboxFactory();
+
+ if (autoConfig)
+ {
+ autoConfigure(includeDefaults);
+ }
+ }
+ public void autoConfigure(boolean includeDefaults)
+ {
FactoryConfiguration config =
ConfigurationUtils.getAutoLoaded(includeDefaults);
@@ -77,7 +91,7 @@
public void configure(String path)
{
- FactoryConfiguration config = ConfigurationUtils.find(path);
+ FactoryConfiguration config = findConfig(path);
if (config != null)
{
configure(config);
@@ -88,42 +102,95 @@
}
}
- public void setVelocityEngine(VelocityEngine engine)
+ protected FactoryConfiguration findConfig(String path)
{
- this.engine = engine;
+ return ConfigurationUtils.find(path);
}
- public ToolContext createContext()
+ /**
+ * Returns the underlying [EMAIL PROTECTED] ToolboxFactory} being used.
+ */
+ public ToolboxFactory getToolboxFactory()
+ {
+ return this.factory;
+ }
+
+ /**
+ * Sets the underlying ToolboxFactory being used.
+ * <b>If you use this, be sure that your ToolboxFactory
+ * is already properly configured.</b>
+ */
+ public void setToolboxFactory(ToolboxFactory factory)
{
- ToolContext context;
- if (this.engine != null)
+ if (this.factory != factory)
{
- context = new ToolContext(this.engine);
+ if (factory == null)
+ {
+ throw new NullPointerException("ToolboxFactory cannot be
null");
+ }
+ debug("ToolboxFactory instance was changed to %s", factory);
+ this.factory = factory;
}
- else
+ }
+
+ /**
+ * Sets the underlying VelocityEngine being used.
+ * <b>If you use this, be sure that your VelocityEngine
+ * is already properly configured and initialized.</b>
+ */
+ public void setVelocityEngine(VelocityEngine engine)
+ {
+ if (velocity != engine)
{
- context = new ToolContext((Map<String,Object>)null);
+ debug("VelocityEngine instance was changed to %s", engine);
+ this.velocity = engine;
}
- addToolboxes(context);
- return context;
}
- public ToolContext createContext(Map<String,Object> toolProps)
+ public VelocityEngine getVelocityEngine()
+ {
+ return this.velocity;
+ }
+
+ public Log getLog()
{
- ToolContext context;
- if (this.engine != null)
+ if (velocity == null)
{
- context = new ToolContext(this.engine);
- context.putToolProperties(toolProps);
+ return null;
}
- else
+ return velocity.getLog();
+ }
+
+ protected final void debug(String msg, Object... args)
+ {
+ Log log = getLog();
+ if (log != null && log.isDebugEnabled())
{
- context = new ToolContext(toolProps);
+ log.debug(String.format(msg, args));
}
- addToolboxes(context);
+ }
+
+ public ToolContext createContext()
+ {
+ return createContext(null);
+ }
+
+ public ToolContext createContext(Map<String,Object> toolProps)
+ {
+ ToolContext context = new ToolContext(toolProps);
+ prepareContext(context);
return context;
}
+ protected void prepareContext(ToolContext context)
+ {
+ if (this.velocity != null)
+ {
+ context.putVelocityEngine(this.velocity);
+ }
+ addToolboxes(context);
+ }
+
protected void addToolboxes(ToolContext context)
{
if (hasApplicationTools())
@@ -163,7 +230,7 @@
protected Toolbox getApplicationToolbox()
{
- if (this.application == null)
+ if (this.application == null && hasApplicationTools())
{
this.application = createToolbox(Scope.APPLICATION);
}
Modified:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ServletUtils.java
URL:
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ServletUtils.java?rev=696572&r1=696571&r2=696572&view=diff
==============================================================================
---
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ServletUtils.java
(original)
+++
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ServletUtils.java
Wed Sep 17 23:24:13 2008
@@ -51,6 +51,12 @@
"org.apache.velocity.tools.shared.config";
public static final String ALT_VELOCITY_VIEW_KEY =
"org.apache.velocity.tools.view.class";
+ /**
+ * Key used to access a live [EMAIL PROTECTED] FactoryConfiguration}
previously
+ * placed in the ServletContext attributes.
+ */
+ public static final String CONFIGURATION_KEY =
+ "org.apache.velocity.tools";
public static final ServletUtils INSTANCE = new ServletUtils();
@@ -320,6 +326,24 @@
return inputStream;
}
+ public static FactoryConfiguration getConfiguration(ServletContext
application)
+ {
+ Object obj = application.getAttribute(CONFIGURATION_KEY);
+ if (obj instanceof FactoryConfiguration)
+ {
+ FactoryConfiguration injected = (FactoryConfiguration)obj;
+ // make note of where we found this
+ String source = injected.getSource();
+ String addnote = " from
ServletContext.getAttribute("+CONFIGURATION_KEY+")";
+ if (!source.endsWith(addnote))
+ {
+ injected.setSource(source+addnote);
+ }
+ return injected;
+ }
+ return null;
+ }
+
public static FactoryConfiguration getConfiguration(String path,
ServletContext
application)
{
Modified:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityView.java
URL:
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityView.java?rev=696572&r1=696571&r2=696572&view=diff
==============================================================================
---
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityView.java
(original)
+++
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityView.java
Wed Sep 17 23:24:13 2008
@@ -87,7 +87,7 @@
*
* @version $Id: VelocityView.java 511959 2007-02-26 19:24:39Z nbubna $
*/
-public class VelocityView
+public class VelocityView extends ViewToolManager
{
/** The HTTP content type context key. */
public static final String CONTENT_TYPE_KEY = "default.contentType";
@@ -99,10 +99,6 @@
public static final String SERVLET_CONTEXT_KEY =
ServletContext.class.getName();
- public static final String DEFAULT_TOOLBOX_KEY = Toolbox.KEY;
-
- public static final String CREATE_SESSION_PROPERTY = "createSession";
-
/** The default content type for the response */
public static final String DEFAULT_CONTENT_TYPE = "text/html";
@@ -115,8 +111,7 @@
* or to access a live [EMAIL PROTECTED] FactoryConfiguration} previously
* placed in the ServletContext attributes.
*/
- public static final String TOOLS_KEY =
- "org.apache.velocity.tools";
+ public static final String TOOLS_KEY = ServletUtils.CONFIGURATION_KEY;
@Deprecated
public static final String DEPRECATED_TOOLS_KEY =
"org.apache.velocity.toolbox";
@@ -183,12 +178,7 @@
private static SimplePool writerPool = new SimplePool(40);
- private ToolboxFactory toolboxFactory = null;
- private VelocityEngine velocity = null;
- private ServletContext servletContext;
private String defaultContentType = DEFAULT_CONTENT_TYPE;
- private String toolboxKey = DEFAULT_TOOLBOX_KEY;
- private boolean createSession = true;
private boolean deprecationSupportMode = true;
public VelocityView(ServletConfig config)
@@ -208,108 +198,33 @@
public VelocityView(JeeConfig config)
{
- this(config, DEFAULT_TOOLBOX_KEY);
- }
-
- public VelocityView(JeeConfig config, String toolboxKey)
- {
- setToolboxKey(toolboxKey);
+ // suppress auto-config, as we have our own config lookup order here
+ super(config.getServletContext(), false, false);
init(config);
}
- /**
- * This method should be private until someone thinks of a good
- * reason to change toolbox keys arbitrarily. If it is opened up,
- * then we need to be sure any "application" toolbox
- * is copied or moved to be under the new key.
- */
- private final void setToolboxKey(String key)
- {
- if (key == null)
- {
- throw new NullPointerException("toolboxKey cannot be null");
- }
- if (!key.equals(toolboxKey))
- {
- this.toolboxKey = key;
- debug("Toolbox key was changed to %s", key);
- }
- }
-
- protected final String getToolboxKey()
- {
- return this.toolboxKey;
- }
-
@Deprecated
protected final void setDeprecationSupportMode(boolean support)
{
- this.deprecationSupportMode = support;
- debug("deprecationSupportMode is %s", (support ? "on" : "off"));
- }
-
- /**
- * Returns the underlying VelocityEngine being used.
- */
- public VelocityEngine getVelocityEngine()
- {
- return velocity;
+ if (deprecationSupportMode != support)
+ {
+ this.deprecationSupportMode = support;
+ debug("deprecationSupportMode is now %s", (support ? "on" :
"off"));
+ }
}
/**
- * Sets the underlying VelocityEngine being used.
- * <b>If you use this, be sure that your VelocityEngine
- * is already properly configured and initialized or
- * you had better call [EMAIL PROTECTED] #init(JeeConfig,VelocityEngine)}
- * after you call this method.</b>
+ * Overrides super class to ensure engine is not set to null.
*/
+ @Override
public void setVelocityEngine(VelocityEngine engine)
{
if (engine == null)
{
throw new NullPointerException("VelocityEngine cannot be null");
}
- debug("VelocityEngine instance was changed to %s", engine);
- this.velocity = engine;
- }
-
- public Log getLog()
- {
- return velocity.getLog();
- }
-
- private void debug(String msg, Object... args)
- {
- if (getLog().isDebugEnabled())
- {
- getLog().debug(String.format(msg, args));
- }
- }
-
- /**
- * Returns the underlying [EMAIL PROTECTED] ToolboxFactory} being used.
- */
- public ToolboxFactory getToolboxFactory()
- {
- return this.toolboxFactory;
- }
-
- /**
- * Sets the underlying ToolboxFactory being used.
- * <b>If you use this, be sure that your ToolboxFactory
- * is already properly configured and initialized or
- * you had better call [EMAIL PROTECTED] #init(JeeConfig,ToolboxFactory)}
- * after you call this method.</b>
- */
- public void setToolboxFactory(ToolboxFactory factory)
- {
- if (factory == null)
- {
- throw new NullPointerException("ToolboxFactory cannot be null");
- }
- debug("ToolboxFactory instance was changed to %s", factory);
- this.toolboxFactory = factory;
+ super.setVelocityEngine(engine);
}
/**
@@ -325,21 +240,10 @@
*/
public void setDefaultContentType(String type)
{
- this.defaultContentType = type;
- debug("Default Content-Type was changed to %s", type);
- }
-
- /**
- * Sets whether or not VelocityView should create a new HttpSession
- * when there are session scoped tools, but no session has been
- * created yet.
- */
- public void setCreateSession(boolean create)
- {
- if (create != this.createSession)
+ if (!defaultContentType.equals(type))
{
- debug("Create session setting was changed to %s", create);
- this.createSession = create;
+ this.defaultContentType = type;
+ debug("Default Content-Type was changed to %s", type);
}
}
@@ -374,29 +278,24 @@
*/
protected void init(JeeConfig config)
{
- this.servletContext = config.getServletContext();
-
- // create engine and factory if none are set yet
- if (velocity == null)
+ // create an engine if none is set yet
+ // (servletContext and factory should already be set by now
+ if (this.velocity == null)
{
this.velocity = new VelocityEngine();
}
- if (toolboxFactory == null)
- {
- this.toolboxFactory = new ToolboxFactory();
- }
- String depMode = findInitParameter(DEPRECATION_SUPPORT_MODE_KEY,
config);
+ String depMode =
config.findInitParameter(DEPRECATION_SUPPORT_MODE_KEY);
if (depMode != null && depMode.equalsIgnoreCase("false"))
{
setDeprecationSupportMode(false);
}
- // initialize the VelocityEngine
+ // configure and initialize the VelocityEngine
init(config, velocity);
- // initialize the ToolboxFactory
- init(config, toolboxFactory);
+ // configure the ToolboxFactory
+ configure(config, factory);
// set encoding & content-type
setEncoding(config);
@@ -441,40 +340,6 @@
}
}
-
- /**
- * Initializes the ToolboxFactory.
- *
- * @param config servlet configuation
- * @param factory the ToolboxFactory to be initialized for this
VelocityView
- */
- protected void init(final JeeConfig config, final ToolboxFactory factory)
- {
- configure(config, toolboxFactory);
-
- // check for a createSession setting
- Boolean bool =
- (Boolean)toolboxFactory.getGlobalProperty(CREATE_SESSION_PROPERTY);
- if (bool != null)
- {
- setCreateSession(bool);
- }
-
- // add any application toolbox to the application attributes
- Toolbox appTools = toolboxFactory.createToolbox(Scope.APPLICATION);
- if (appTools != null &&
- this.servletContext.getAttribute(this.toolboxKey) == null)
- {
- this.servletContext.setAttribute(this.toolboxKey, appTools);
- }
- }
-
- protected String findInitParameter(String key, JeeConfig config)
- {
- return config.findInitParameter(key);
- }
-
-
protected void configure(final JeeConfig config, final VelocityEngine
velocity)
{
// first get the default properties, and bail if we don't find them
@@ -558,7 +423,7 @@
// only load the default tools if they have explicitly said to
// or if they are not using an old toolbox and have said nothing
- String loadDefaults = findInitParameter(LOAD_DEFAULTS_KEY, config);
+ String loadDefaults = config.findInitParameter(LOAD_DEFAULTS_KEY);
if ((!hasOldToolbox && loadDefaults == null) ||
"true".equalsIgnoreCase(loadDefaults))
{
@@ -595,19 +460,15 @@
setConfig(factoryConfig, servletToolsPath, true);
// check for "injected" configuration in application attributes
- Object obj = servletContext.getAttribute(TOOLS_KEY);
- if (obj instanceof FactoryConfiguration)
+ FactoryConfiguration injected =
ServletUtils.getConfiguration(servletContext);
+ if (injected != null)
{
- debug("Adding configuration found in servletContext under key
'%s'", TOOLS_KEY);
- FactoryConfiguration injected = (FactoryConfiguration)obj;
- // make note of where we found this
- String source = injected.getSource();
- injected.setSource(source+" from
ServletContext.getAttribute("+TOOLS_KEY+")");
+ debug("Adding configuration instance in servletContext attributes
as '%s'", TOOLS_KEY);
factoryConfig.addConfiguration(injected);
}
// see if we should only keep valid tools, data, and properties
- String cleanConfig = findInitParameter(CLEAN_CONFIGURATION_KEY,
config);
+ String cleanConfig = config.findInitParameter(CLEAN_CONFIGURATION_KEY);
if ("true".equals(cleanConfig))
{
// remove invalid tools, data, and properties from the
configuration
@@ -617,8 +478,8 @@
}
// apply this configuration to the specified factory
- debug("Configuring toolboxFactory with: %s", factoryConfig);
- factory.configure(factoryConfig);
+ debug("Configuring factory with: %s", factoryConfig);
+ configure(factoryConfig);
}
/**
@@ -635,7 +496,7 @@
FactoryConfiguration toolbox = null;
// look for specified path under the deprecated toolbox key
- String oldPath = findInitParameter(DEPRECATED_TOOLS_KEY, config);
+ String oldPath = config.findInitParameter(DEPRECATED_TOOLS_KEY);
if (oldPath != null)
{
// ok, they said the toolbox.xml should be there
@@ -856,7 +717,7 @@
HttpServletResponse response) throws IOException
{
// then get a context
- Context context = getContext(request, response);
+ Context context = createContext(request, response);
// get the template
Template template = getTemplate(request, response);
@@ -871,7 +732,7 @@
throws IOException
{
// then get a context
- Context context = getContext(request);
+ Context context = createContext(request, null);
// get the template
Template template = getTemplate(request);
@@ -884,67 +745,6 @@
/**
- * Prepares the request scope toolbox, if one is configured for
- * the toolbox factory, and then prepares the session toolbox
- * if one is configured for the factory and has not yet been created
- * for the current session.
- */
- public void prepareToolboxes(HttpServletRequest request)
- {
- prepareToolbox(request);
-
- if (toolboxFactory.hasTools(Scope.SESSION))
- {
- //FIXME? does this honor createSession props set on the session
Toolbox?
- HttpSession session = request.getSession(this.createSession);
- if (session != null)
- {
- // allow only one thread per session at a time
- synchronized(getMutex(session))
- {
- if (session.getAttribute(this.toolboxKey) == null)
- {
- Toolbox sessTools =
- toolboxFactory.createToolbox(Scope.SESSION);
- session.setAttribute(this.toolboxKey, sessTools);
- }
- }
- }
- }
- }
-
- /**
- * Prepares the request scope toolbox, if one is configured for
- * the toolbox factory.
- */
- public void prepareToolboxes(ServletRequest request)
- {
- prepareToolbox(request);
- }
-
- private void prepareToolbox(ServletRequest request)
- {
- // only set a new toolbox if we need one
- if (toolboxFactory.hasTools(Scope.REQUEST)
- && request.getAttribute(this.toolboxKey) == null)
- {
- Toolbox reqTools = toolboxFactory.createToolbox(Scope.REQUEST);
- request.setAttribute(this.toolboxKey, reqTools);
- }
- }
-
-
- /**
- * Returns a mutex (lock object) unique to the specified session
- * to allow for reliable synchronization on the session.
- */
- protected Object getMutex(HttpSession session)
- {
- return ServletUtils.getMutex(session, "session.mutex", this);
- }
-
-
- /**
* <p>Creates and returns an initialized Velocity context.</p>
*
* A new context of class [EMAIL PROTECTED] ViewToolContext} is created and
@@ -953,50 +753,23 @@
* @param request servlet request from client
* @param response servlet reponse to client
*/
- protected ViewToolContext createContext(HttpServletRequest request,
- HttpServletResponse response)
+ @Override
+ public ViewToolContext createContext(HttpServletRequest request,
+ HttpServletResponse response)
{
ViewToolContext ctx;
if (this.deprecationSupportMode)
{
- ctx = new ChainedContext(getVelocityEngine(), request, response,
servletContext);
+ ctx = new ChainedContext(velocity, request, response,
servletContext);
}
else
{
- ctx = new ViewToolContext(getVelocityEngine(), request, response,
servletContext);
+ ctx = new ViewToolContext(velocity, request, response,
servletContext);
}
- prepareContext(ctx);
+ prepareContext(ctx, request);
return ctx;
}
- public void prepareContext(ViewToolContext context)
- {
- // if this view is storing toolboxes under a non-standard key,
- // then retrieve it's toolboxes here, since ViewToolContext won't
- // know where to find them
- if (!this.toolboxKey.equals(DEFAULT_TOOLBOX_KEY))
- {
- context.addToolboxesUnderKey(this.toolboxKey);
- }
- }
-
-
- public Context getContext(HttpServletRequest request)
- {
- return getContext(request, null);
- }
-
- public Context getContext(HttpServletRequest request,
- HttpServletResponse response)
- {
- // first make sure request and session toolboxes are added
- prepareToolboxes(request);
-
- // then create the context
- return createContext(request, response);
- }
-
-
/**
* <p>Gets the requested template.</p>
Modified:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityViewFilter.java
URL:
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityViewFilter.java?rev=696572&r1=696571&r2=696572&view=diff
==============================================================================
---
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityViewFilter.java
(original)
+++
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityViewFilter.java
Wed Sep 17 23:24:13 2008
@@ -160,9 +160,6 @@
FilterChain chain)
throws java.io.IOException, ServletException
{
- // prepare toolbox(es)
- getVelocityView().prepareToolboxes(request);
-
// can/should we create the context for the request?
if (contextKey != null && request instanceof HttpServletRequest)
{
@@ -170,6 +167,11 @@
(HttpServletResponse)response);
request.setAttribute(contextKey, context);
}
+ else
+ {
+ // just publish the toolboxes
+ getVelocityView().publishToolboxes(request);
+ }
// move down the chain
chain.doFilter(request, response);
Modified:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java
URL:
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java?rev=696572&r1=696571&r2=696572&view=diff
==============================================================================
---
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java
(original)
+++
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/VelocityViewServlet.java
Wed Sep 17 23:24:13 2008
@@ -247,7 +247,7 @@
protected Context createContext(HttpServletRequest request,
HttpServletResponse response)
{
- return getVelocityView().getContext(request, response);
+ return getVelocityView().createContext(request, response);
}
protected void fillContext(Context context, HttpServletRequest request)
Modified:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ViewToolContext.java
URL:
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ViewToolContext.java?rev=696572&r1=696571&r2=696572&view=diff
==============================================================================
---
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ViewToolContext.java
(original)
+++
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ViewToolContext.java
Wed Sep 17 23:24:13 2008
@@ -76,6 +76,7 @@
private final HttpServletResponse response;
private final ServletContext application;
private final VelocityEngine velocity;
+ private String toolboxKey = DEFAULT_TOOLBOX_KEY;
public ViewToolContext(VelocityEngine velocity,
HttpServletRequest request,
@@ -93,6 +94,11 @@
putToolProperties();
}
+ protected void setToolboxKey(String key)
+ {
+ this.toolboxKey = key;
+ }
+
protected void putToolProperties()
{
putToolProperty(REQUEST, getRequest());
@@ -112,16 +118,16 @@
// then we stop looking. adding boxes to
// the request/session/application attributes
// later will not work. once one is in, any
- // later additions must be direct via addToolbox()
- // or addToolboxes(String toolboxKey)
+ // later additions must be via addToolbox(Toolbox)
+ // or addToolboxesUnderKey(String toolboxKey)
if (super.getToolboxes().isEmpty())
{
- addToolboxesUnderKey(DEFAULT_TOOLBOX_KEY);
+ addToolboxesUnderKey(this.toolboxKey);
}
return super.getToolboxes();
}
- public void addToolboxesUnderKey(String toolboxKey)
+ protected void addToolboxesUnderKey(String toolboxKey)
{
Toolbox reqTools = (Toolbox)getRequest().getAttribute(toolboxKey);
if (reqTools != null)
Added:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java
URL:
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java?rev=696572&view=auto
==============================================================================
---
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java
(added)
+++
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java
Wed Sep 17 23:24:13 2008
@@ -0,0 +1,339 @@
+package org.apache.velocity.tools.view;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Map;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletRequest;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpServletResponse;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.tools.Scope;
+import org.apache.velocity.tools.Toolbox;
+import org.apache.velocity.tools.ToolboxFactory;
+import org.apache.velocity.tools.ToolContext;
+import org.apache.velocity.tools.ToolManager;
+import org.apache.velocity.tools.config.ConfigurationUtils;
+import org.apache.velocity.tools.config.FactoryConfiguration;
+import org.apache.velocity.tools.view.ServletUtils;
+import org.apache.velocity.tools.view.ViewContext;
+import org.apache.velocity.tools.view.ViewToolContext;
+
+/**
+ * Manages tools for web applications. This simplifies the process
+ * of getting a tool-populated Velocity context for merging with templates.
+ * It allows for both direct configuration by passing in a [EMAIL PROTECTED]
FactoryConfiguration}
+ * or having one in the ServletContext attributes under
+ * [EMAIL PROTECTED] ServletUtils#CONFIGURATION_KEY}, as well as configuration
+ * via a tools.xml or tools.properties file in
+ * either the classpath or the local file system.
+ *
+ * @author Nathan Bubna
+ * @version $Id: ToolManager.java 511959 2007-02-26 19:24:39Z nbubna $
+ */
+public class ViewToolManager extends ToolManager
+{
+ public static final String CREATE_SESSION_PROPERTY = "createSession";
+ public static final String PUBLISH_TOOLBOXES_PROPERTY = "publishToolboxes";
+ public static final String DEFAULT_TOOLBOX_KEY = Toolbox.KEY;
+
+ protected ServletContext servletContext;
+ private boolean createSession = true;
+ private boolean publishToolboxes = true;
+ private boolean appToolsPublished = false;
+ private String toolboxKey = DEFAULT_TOOLBOX_KEY;
+
+ /**
+ * Constructs an instance already configured to use the
+ * [EMAIL PROTECTED] ConfigurationUtils#getAutoLoaded()()} configuration
+ * and any configuration specified via a "org.apache.velocity.tools"
+ * system property.
+ */
+ public ViewToolManager(ServletContext app)
+ {
+ this(app, true, true);
+ }
+
+ public ViewToolManager(ServletContext app, boolean includeDefaults)
+ {
+ this(app, true, includeDefaults);
+ }
+
+ public ViewToolManager(ServletContext app,
+ boolean autoConfig, boolean includeDefaults)
+ {
+ super(autoConfig, includeDefaults);
+
+ if (app == null)
+ {
+ throw new NullPointerException("ServletContext is required");
+ }
+ this.servletContext = app;
+ }
+
+ @Override
+ public void autoConfigure(boolean includeDefaults)
+ {
+ super.autoConfigure(includeDefaults);
+
+ // check for a configuration in application attributes
+ FactoryConfiguration injected =
ServletUtils.getConfiguration(servletContext);
+ if (injected != null)
+ {
+ configure(injected);
+ }
+ }
+
+ /**
+ * Sets whether or not the creation of a new [EMAIL PROTECTED]
ViewToolContext}
+ * should make the various scoped [EMAIL PROTECTED] Toolbox} instances
available
+ * publically via the HttpServletRequest/HttpSession/ServletContext
+ * attributes or simply add the Toolbox instances directly to the
+ * context. <b>It is important to note that if this is set to false,
+ * session-scoped tools will NOT be stored in the session, but instead
+ * be recreated for each request.</b>
+ * @see #publishToolboxes
+ * @see #setToolboxKey
+ */
+ public void setPublishToolboxes(boolean publish)
+ {
+ if (publish != this.publishToolboxes)
+ {
+ debug("Publish toolboxes setting was changed to %s", publish);
+ this.publishToolboxes = publish;
+ }
+ }
+
+ public boolean getPublishToolboxes()
+ {
+ return this.publishToolboxes;
+ }
+
+ /**
+ * Sets a new attribute key to be used for publishing each [EMAIL
PROTECTED] Toolbox}.
+ * @see #setPublishToolboxes
+ * @see #publishToolboxes
+ */
+ public void setToolboxKey(String key)
+ {
+ if (key == null)
+ {
+ throw new NullPointerException("toolboxKey cannot be null");
+ }
+ if (!key.equals(toolboxKey))
+ {
+ this.toolboxKey = key;
+ unpublishApplicationTools();
+ debug("Toolbox key was changed to %s", key);
+ }
+ }
+
+ public String getToolboxKey()
+ {
+ return this.toolboxKey;
+ }
+
+ /**
+ * Sets whether or not a new HttpSession should be created
+ * when there are session scoped tools to be stored in the session,
+ * but no session has been created yet.
+ * @see #publishToolboxes
+ */
+ public void setCreateSession(boolean create)
+ {
+ if (create != this.createSession)
+ {
+ debug("Create session setting was changed to %s", create);
+ this.createSession = create;
+ }
+ }
+
+ public boolean getCreateSession()
+ {
+ return this.createSession;
+ }
+
+ /**
+ * Checks the internal [EMAIL PROTECTED] ToolboxFactory} for any changes to
+ * the createSession or publishToolboxes settings.
+ */
+ protected void updateGlobalProperties()
+ {
+ // check for a createSession setting
+ Boolean create =
+ (Boolean)this.factory.getGlobalProperty(CREATE_SESSION_PROPERTY);
+ if (create != null)
+ {
+ setCreateSession(create);
+ }
+
+ // check for a publishToolboxes setting
+ Boolean publish =
+
(Boolean)this.factory.getGlobalProperty(PUBLISH_TOOLBOXES_PROPERTY);
+ if (publish != null)
+ {
+ setPublishToolboxes(publish);
+ }
+ }
+
+ /**
+ * Removes any published [EMAIL PROTECTED] Scope#APPLICATION} Toolbox.
+ */
+ protected void unpublishApplicationTools()
+ {
+ if (appToolsPublished)
+ {
+ // clear the published application toolbox
+ servletContext.removeAttribute(this.toolboxKey);
+ appToolsPublished = false;
+ }
+ }
+
+ @Override
+ public void configure(FactoryConfiguration config)
+ {
+ super.configure(config);
+
+ // reset things as best we can
+ unpublishApplicationTools();
+ updateGlobalProperties();
+ }
+
+ @Override
+ protected FactoryConfiguration findConfig(String path)
+ {
+ return ServletUtils.getConfiguration(path, servletContext, false);
+ }
+
+ @Override
+ protected void addToolboxes(ToolContext context)
+ {
+ super.addToolboxes(context);
+ if (hasSessionTools())
+ {
+ context.addToolbox(getSessionToolbox());
+ }
+ }
+
+ @Override
+ public ToolContext createContext(Map<String,Object> toolProps)
+ {
+ ToolContext context = super.createContext(toolProps);
+ context.putToolProperty(ViewContext.SERVLET_CONTEXT_KEY,
servletContext);
+ debug("Non-ViewToolContext was requested from ViewToolManager.");
+ return context;
+ }
+
+ public ViewToolContext createContext(HttpServletRequest request,
+ HttpServletResponse response)
+ {
+ ViewToolContext context =
+ new ViewToolContext(getVelocityEngine(), request, response,
servletContext);
+ prepareContext(context, request);
+ return context;
+ }
+
+ public void prepareContext(ViewToolContext context, HttpServletRequest
request)
+ {
+ context.setToolboxKey(this.toolboxKey);
+ if (this.publishToolboxes)
+ {
+ // put the toolboxes where the ViewToolContext
+ // and others can find them
+ publishToolboxes(request);
+
+ VelocityEngine engine = getVelocityEngine();
+ if (engine != null)
+ {
+ context.putVelocityEngine(engine);
+ }
+ }
+ else
+ {
+ // super class takes care of engine
+ // and adds toolboxes directly
+ prepareContext(context);
+ }
+ }
+
+ protected boolean hasSessionTools()
+ {
+ return hasTools(Scope.SESSION);
+ }
+
+ protected Toolbox getSessionToolbox()
+ {
+ return createToolbox(Scope.SESSION);
+ }
+
+ /**
+ * Places the [EMAIL PROTECTED] Scope#REQUEST} [EMAIL PROTECTED] Toolbox}
(if any)
+ * into the [EMAIL PROTECTED] ServletRequest} attributes using
+ * [EMAIL PROTECTED] Toolbox#KEY} as the key.
+ */
+ public void publishToolboxes(ServletRequest request)
+ {
+ publishToolbox(request);
+ }
+
+ private void publishToolbox(ServletRequest request)
+ {
+ if (hasRequestTools() &&
+ request.getAttribute(this.toolboxKey) == null)
+ {
+ request.setAttribute(this.toolboxKey, getRequestToolbox());
+ }
+ }
+
+ /**
+ * Places the [EMAIL PROTECTED] Scope#REQUEST} [EMAIL PROTECTED] Toolbox}
(if any)
+ * into the [EMAIL PROTECTED] HttpServletRequest} attributes using
+ * [EMAIL PROTECTED] Toolbox#KEY} as the key, places the [EMAIL PROTECTED]
Scope#SESSION}
+ * Toolbox (if any) into the attributes of the [EMAIL PROTECTED]
HttpSession} (if any)
+ * then ensures that the [EMAIL PROTECTED] Scope#APPLICATION} Toolbox (if
any)
+ * has been placed in the [EMAIL PROTECTED] ServletContext} attributes.
+ */
+ public void publishToolboxes(HttpServletRequest request)
+ {
+ publishToolbox(request);
+
+ if (hasSessionTools())
+ {
+ HttpSession session = request.getSession(this.createSession);
+ if (session != null)
+ {
+ // allow only one thread per session
+ synchronized(ServletUtils.getMutex(session, "session.mutex",
this))
+ {
+ if (session.getAttribute(this.toolboxKey) == null)
+ {
+ session.setAttribute(this.toolboxKey,
getSessionToolbox());
+ }
+ }
+ }
+ }
+ if (!appToolsPublished && hasApplicationTools())
+ {
+ servletContext.setAttribute(this.toolboxKey,
getApplicationToolbox());
+ }
+ }
+
+}
Propchange:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java
------------------------------------------------------------------------------
svn:executable = *
Propchange:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java
------------------------------------------------------------------------------
svn:keywords = Revision
Propchange:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ViewToolManager.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/jsp/VelocityViewTag.java
URL:
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/jsp/VelocityViewTag.java?rev=696572&r1=696571&r2=696572&view=diff
==============================================================================
---
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/jsp/VelocityViewTag.java
(original)
+++
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/jsp/VelocityViewTag.java
Wed Sep 17 23:24:13 2008
@@ -194,8 +194,7 @@
ViewToolContext context =
new JspToolContext(view.getVelocityEngine(), this.pageContext);
-
view.prepareToolboxes((HttpServletRequest)this.pageContext.getRequest());
- view.prepareContext(context);
+ view.prepareContext(context,
(HttpServletRequest)this.pageContext.getRequest());
setVelocityView(view);
setViewToolContext(context);