You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by jt...@apache.org on 2002/05/15 17:57:01 UTC
cvs commit: jakarta-turbine-fulcrum/src/services/org/apache/fulcrum/velocity ContextAdapter.java TurbineVelocityService.java
jtaylor 02/05/15 08:57:01
Modified: src/services/org/apache/fulcrum/velocity ContextAdapter.java
TurbineVelocityService.java
Log:
Changed ContextAdapter to extend AbstractContext rather than just implementing
Context. This allows us to avoid wrapping the context adapter in a
VelocityContext. Because VelocityContext does not pass puts to the context it is
wrapping, additions to the TemplateContext occuring inside velocity were not
being retained. They are now, so if you process two templates sequentially with
the same TemplateContext, changes to the context made by the first will be
visible to the second. As far as I can tell, this is closer to the turbine 2
behavior, and based on discussions had on IRC a few months ago, this is the way
it ought to work.
Revision Changes Path
1.4 +43 -16 jakarta-turbine-fulcrum/src/services/org/apache/fulcrum/velocity/ContextAdapter.java
Index: ContextAdapter.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-fulcrum/src/services/org/apache/fulcrum/velocity/ContextAdapter.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ContextAdapter.java 10 Aug 2001 11:46:24 -0000 1.3
+++ ContextAdapter.java 15 May 2002 15:57:01 -0000 1.4
@@ -57,18 +57,19 @@
import org.apache.fulcrum.template.TemplateContext;
import org.apache.velocity.context.Context;
+import org.apache.velocity.context.AbstractContext;
/**
- * An adapter for Fulcrum's {@link
- * org.apache.fulcrum.template.TemplateContext}. Allows for easy
- * processing of TemplateContext objects by Velocity.
+ * An adapter for Fulcrum's {@link TemplateContext}. Allows for easy processing
+ * of TemplateContext objects by Velocity. This class extends
+ * {@link AbstractContext}, so it supports event cartridge handling.
*
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
+ * @author <a href="mailto:james@jamestaylor.org">James Taylor</a>
* @see org.apache.fulcrum.template.TemplateContext
* @see org.apache.velocity.context.Context
*/
-public class ContextAdapter
- implements Context
+public class ContextAdapter extends AbstractContext
{
private TemplateContext context;
@@ -77,29 +78,55 @@
this.context = context;
}
- public Object put(String key, Object value)
+ /**
+ * @see AbstractContext#internalGet
+ */
+ public Object internalGet( String key )
{
- context.put(key, value);
- return null;
+ return context.get( key );
}
- public Object get(String key)
+ /**
+ * @see AbstractContext#internalPut
+ */
+ public Object internalPut( String key, Object value )
{
- return context.get(key);
+ context.put( key, value );
+
+ return null;
}
- public Object remove(Object key)
+ /**
+ * determines if there is a value for the
+ * given key
+ *
+ * @param key name of value to check
+ * @return true if non-null value in store
+ */
+ public boolean internalContainsKey(Object key)
{
- return context.remove(key);
+ return context.containsKey( key );
}
- public Object[] getKeys()
+ /**
+ * returns array of keys
+ *
+ * @return keys as []
+ */
+ public Object[] internalGetKeys()
{
- return null;
+ return context.getKeys();
}
- public boolean containsKey(Object key)
+ /**
+ * remove a key/value pair from the
+ * internal storage
+ *
+ * @param key name of value to remove
+ * @return value removed
+ */
+ public Object internalRemove(Object key)
{
- return false;
+ return context.remove( key );
}
}
1.17 +21 -8 jakarta-turbine-fulcrum/src/services/org/apache/fulcrum/velocity/TurbineVelocityService.java
Index: TurbineVelocityService.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-fulcrum/src/services/org/apache/fulcrum/velocity/TurbineVelocityService.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- TurbineVelocityService.java 16 Apr 2002 22:23:36 -0000 1.16
+++ TurbineVelocityService.java 15 May 2002 15:57:01 -0000 1.17
@@ -70,6 +70,7 @@
import org.apache.velocity.app.event.NullSetEventHandler;
import org.apache.velocity.app.event.MethodExceptionEventHandler;
import org.apache.velocity.context.Context;
+import org.apache.velocity.context.InternalEventContext;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.fulcrum.ServiceException;
import org.apache.fulcrum.InitializationException;
@@ -100,7 +101,7 @@
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
* @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
* @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
- * @version $Id: TurbineVelocityService.java,v 1.16 2002/04/16 22:23:36 jvanzyl Exp $
+ * @version $Id: TurbineVelocityService.java,v 1.17 2002/05/15 15:57:01 jtaylor Exp $
*/
public class TurbineVelocityService
extends BaseTemplateEngineService
@@ -279,27 +280,39 @@
{
try
{
- // need to convert to a VelocityContext because EventCartridges
- // need that. this sucks because it seems like unnecessary
- // object creation.
- VelocityContext velocityContext = new VelocityContext(context);
+ // If the context is not already an instance of
+ // InternalEventContext, wrap it in a VeclocityContext so that
+ // event cartridges will work. Unfortunately there is no interface
+ // that extends both Context and InternalEventContext, so this
+ // is not as clear as it could be.
+
+ Context eventContext;
+
+ if ( context instanceof InternalEventContext )
+ {
+ eventContext = context;
+ }
+ else
+ {
+ eventContext = new VelocityContext( context );
+ }
// Attach the EC to the context
EventCartridge ec = getEventCartridge();
if (ec != null && eventCartridgeEnabled)
{
- ec.attachToContext(velocityContext);
+ ec.attachToContext(eventContext);
}
if (encoding != null)
{
// Request scoped encoding first supported by Velocity 1.1.
Velocity.mergeTemplate(filename, encoding,
- velocityContext, writer);
+ eventContext, writer);
}
else
{
- Velocity.mergeTemplate(filename, velocityContext, writer);
+ Velocity.mergeTemplate(filename, eventContext, writer);
}
}
catch (Exception e)
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>