jon         01/11/28 16:35:58

  Modified:    src/services/java/org/apache/fulcrum/velocity
                        TurbineVelocityService.java
  Log:
  Added support for configuration and initilization of Velocity
  EventCartridges. It is actually a pretty minor patch which is nice and
  configuration is quite easy.
  
  -jon
  
  Revision  Changes    Path
  1.8       +81 -5     
jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/velocity/TurbineVelocityService.java
  
  Index: TurbineVelocityService.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/velocity/TurbineVelocityService.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TurbineVelocityService.java       2001/08/16 05:41:42     1.7
  +++ TurbineVelocityService.java       2001/11/29 00:35:58     1.8
  @@ -56,6 +56,7 @@
   
   import java.util.Vector;
   import java.util.Iterator;
  +import java.util.Enumeration;
   import java.io.IOException;
   import java.io.OutputStream;
   import java.io.OutputStreamWriter;
  @@ -63,6 +64,10 @@
   import org.apache.velocity.Template;
   import org.apache.velocity.VelocityContext;
   import org.apache.velocity.app.Velocity;
  +import org.apache.velocity.app.event.EventCartridge;
  +import org.apache.velocity.app.event.ReferenceInsertionEventHandler;
  +import org.apache.velocity.app.event.NullSetEventHandler;
  +import org.apache.velocity.app.event.MethodExceptionEventHandler;
   import org.apache.velocity.context.Context;
   import org.apache.velocity.exception.MethodInvocationException;
   import org.apache.fulcrum.ServiceException;
  @@ -91,7 +96,8 @@
    * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Sean Legassick</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Daniel Rall</a>
  - * @version $Id: TurbineVelocityService.java,v 1.7 2001/08/16 05:41:42 jon Exp $
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Jon S. Stevens</a>
  + * @version $Id: TurbineVelocityService.java,v 1.8 2001/11/29 00:35:58 jon Exp $
    */
   public class TurbineVelocityService
       extends BaseTemplateEngineService
  @@ -119,6 +125,11 @@
       private static final String ABSOLUTE_PREFIX = "file://";
   
       /**
  +     * The EventCartridge that is used against all contexts
  +     */
  +    private static EventCartridge eventCartridge;
  +    
  +    /**
        * Performs early initialization of this Turbine service.
        */
       public void init()
  @@ -127,6 +138,7 @@
           try
           {
               initVelocity();
  +            initEventCartridges();
   
               // Register with the template service.
               registerConfiguration("vm");
  @@ -227,6 +239,14 @@
       }
   
       /**
  +     * Returns the populated event cartridge
  +     */
  +    public EventCartridge getEventCartridge()
  +    {
  +        return eventCartridge;
  +    }
  +
  +    /**
        * Processes the request and fill in the template with the values
        * you set in the the supplied Context. Applies the specified
        * character and template encodings.
  @@ -256,19 +276,26 @@
               charset = DEFAULT_CHAR_SET;
           }
   
  -        OutputStreamWriter writer = null;
  +        // need to convert to a VelocityContext because EventCartridges
  +        // need that. this sucks because it seems like unnecessary
  +        // object creation.
  +        VelocityContext vc = new VelocityContext(context);        
   
  +        // Attach the EC to the context
  +        getEventCartridge().attachToContext(vc);
  +
  +        OutputStreamWriter writer = null;
           try
           {
               writer = new OutputStreamWriter(output, charset);
               if (encoding != null)
               {
                   // Request scoped encoding first supported by Velocity 1.1.
  -                Velocity.mergeTemplate(filename, encoding, context, writer);
  +                Velocity.mergeTemplate(filename, encoding, vc, writer);
               }
               else
               {
  -                Velocity.mergeTemplate(filename, context, writer);
  +                Velocity.mergeTemplate(filename, vc, writer);
               }
           }
           catch (Exception e)
  @@ -288,7 +315,6 @@
               {
               }
           }
  -
           return charset;
       }
   
  @@ -314,6 +340,56 @@
           }
   
           throw new ServiceException(err, e);
  +    }
  +
  +    /**
  +     * This method is responsible for initializing the various Velocity
  +     * EventCartridges. You just add a configuration like this:
  +     * <code>
  +     * services.VelocityService.eventCartridge.classes = 
org.tigris.scarab.util.ReferenceInsertionFilter
  +     * </code>
  +     * and list out (comma separated) the list of EC's that you want to 
  +     * initialize.
  +     */
  +    private void initEventCartridges()
  +        throws InitializationException
  +    {
  +        eventCartridge = new EventCartridge();
  +
  +        Vector ecconfig = getConfiguration()
  +            .getVector("eventCartridge.classes");
  +        Object obj = null;
  +        String className = null;
  +        for (Enumeration e = ecconfig.elements() ; e.hasMoreElements() ;)
  +        {
  +            className = (String) e.nextElement();
  +            try
  +            {
  +                obj = Class.forName(className).newInstance();
  +
  +                if (obj instanceof ReferenceInsertionEventHandler)
  +                {
  +                    getEventCartridge()
  +                        .addEventHandler((ReferenceInsertionEventHandler)obj);
  +                }
  +                else if (obj instanceof NullSetEventHandler)
  +                {
  +                    getEventCartridge()
  +                        .addEventHandler((NullSetEventHandler)obj);
  +                }
  +                else if (obj instanceof MethodExceptionEventHandler)
  +                {
  +                    getEventCartridge()
  +                        .addEventHandler((MethodExceptionEventHandler)obj);
  +                }
  +            }
  +            catch (Exception h)
  +            {
  +                throw new InitializationException(
  +                    "Could not initialize EventCartridge: " + 
  +                    className, h);
  +            }
  +        }
       }
   
       /**
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to