You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by he...@apache.org on 2003/07/15 11:07:32 UTC

cvs commit: jakarta-turbine-2/src/java/org/apache/turbine/services/velocity TurbineVelocityService.java VelocityService.java

henning     2003/07/15 02:07:32

  Modified:    conf     TurbineResources.properties
               conf/master TurbineResources.master
               xdocs    changes.xml
               xdocs/services velocity-service.xml
               src/java/org/apache/turbine/services/velocity
                        TurbineVelocityService.java VelocityService.java
  Log:
  I'm officially fed up with the obscure Velocity error logging and
  non-existent stack traces if a tool throws an exception. This is an
  attempt to fix this by adding an EventCartridge to the TurbineVelocityService
  and reporting the actual stack traces in the log file. This behaviour is
  controllable by a property. Even added some docs.
  
  This wants some testing, so I defaulted it to on.
  
  Revision  Changes    Path
  1.46      +5 -1      jakarta-turbine-2/conf/TurbineResources.properties
  
  Index: TurbineResources.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/conf/TurbineResources.properties,v
  retrieving revision 1.45
  retrieving revision 1.46
  diff -u -r1.45 -r1.46
  --- TurbineResources.properties	10 Jul 2003 12:33:24 -0000	1.45
  +++ TurbineResources.properties	15 Jul 2003 09:07:32 -0000	1.46
  @@ -510,6 +510,10 @@
   services.VelocityService.default.error.screen = VelocityErrorScreen
   services.VelocityService.default.layout.template = Default.vm
   
  +#
  +# Set this to true to catch Velocity Errors and display them in the log file
  +services.VelocityService.catch.errors = true
  +
   services.VelocityService.runtime.log=/logs/velocity.log
   #services.VelocityService.input.encoding=UTF-8
   services.VelocityService.velocimacro.library = GlobalMacros.vm
  
  
  
  1.45      +5 -1      jakarta-turbine-2/conf/master/TurbineResources.master
  
  Index: TurbineResources.master
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/conf/master/TurbineResources.master,v
  retrieving revision 1.44
  retrieving revision 1.45
  diff -u -r1.44 -r1.45
  --- TurbineResources.master	10 Jul 2003 12:33:24 -0000	1.44
  +++ TurbineResources.master	15 Jul 2003 09:07:32 -0000	1.45
  @@ -510,6 +510,10 @@
   services.VelocityService.default.error.screen = VelocityErrorScreen
   services.VelocityService.default.layout.template = Default.vm
   
  +#
  +# Set this to true to catch Velocity Errors and display them in the log file
  +services.VelocityService.catch.errors = true
  +
   services.VelocityService.runtime.log=/logs/velocity.log
   #services.VelocityService.input.encoding=UTF-8
   services.VelocityService.velocimacro.library = GlobalMacros.vm
  
  
  
  1.57      +6 -0      jakarta-turbine-2/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/xdocs/changes.xml,v
  retrieving revision 1.56
  retrieving revision 1.57
  diff -u -r1.56 -r1.57
  --- changes.xml	15 Jul 2003 08:55:53 -0000	1.56
  +++ changes.xml	15 Jul 2003 09:07:32 -0000	1.57
  @@ -423,6 +423,12 @@
         is  useful if you have multiple event fields in a form and only one should be selected 
         (e.g. by a client side java script application).
       </li>
  +    <li>
  +      VelocityService now uses the EventCartridge Code to catch and log invocation errors from templates.
  +      This is very useful when debugging pull tools, because you get the actual exception in the log and
  +      not only the velocity internal stack trace. You can switch this behaviour off by setting 
  +      "services.VelocityService.catch.errors" to off or false.
  +    </li>
     </ul>
   </p>
   </subsection>
  
  
  
  1.2       +14 -3     jakarta-turbine-2/xdocs/services/velocity-service.xml
  
  Index: velocity-service.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/xdocs/services/velocity-service.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- velocity-service.xml	16 Aug 2001 05:10:45 -0000	1.1
  +++ velocity-service.xml	15 Jul 2003 09:07:32 -0000	1.2
  @@ -52,6 +52,10 @@
   services.VelocityService.default.error.screen = VelocityErrorScreen
   services.VelocityService.default.layout.template = /Default.vm
   
  +#
  +# Set this to true to catch Velocity Errors and display them in the log file
  +services.VelocityService.catch.errors = true
  +
   services.VelocityService.runtime.log=/logs/velocity.log
   #services.VelocityService.input.encoding=UTF-8
   services.VelocityService.velocimacro.library = GlobalMacros.vm
  @@ -73,10 +77,17 @@
   
   <section name="Usage">
   
  -<source><![CDATA[
  -
  -]]></source>
  +</section>
   
  +<section name="Properties">
  +<ul>
  +  <li>"services.VelocityService.catch.errors" controls the reporting of Velocity errors to the application (and to to the user).
  +      If this is set to "true" (which is the default), errors are caught by Turbine and reported in the turbine.log file at 
  +      error level. Velocity will get the String "[Turbine caught an Error here. Look into the turbine.log for further information]"
  +      as return value (this might cause further errors down the road if you e.g. expect a numerical value or an object as the result
  +      of a method), so if you see this message in your screen, follow the instructions.
  +  </li>
  +</ul>
   </section>
   
   </body>
  
  
  
  1.24      +57 -8     jakarta-turbine-2/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java
  
  Index: TurbineVelocityService.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- TurbineVelocityService.java	15 Jul 2003 08:26:16 -0000	1.23
  +++ TurbineVelocityService.java	15 Jul 2003 09:07:32 -0000	1.24
  @@ -83,6 +83,8 @@
   
   import org.apache.velocity.VelocityContext;
   import org.apache.velocity.app.Velocity;
  +import org.apache.velocity.app.event.EventCartridge;
  +import org.apache.velocity.app.event.MethodExceptionEventHandler;
   import org.apache.velocity.context.Context;
   import org.apache.velocity.runtime.log.SimpleLog4JLogSystem;
   
  @@ -114,7 +116,8 @@
    */
   public class TurbineVelocityService
           extends BaseTemplateEngineService
  -        implements VelocityService
  +        implements VelocityService,
  +                   MethodExceptionEventHandler
   {
       /** The generic resource loader path property in velocity.*/
       private static final String RESOURCE_LOADER_PATH = ".resource.loader.path";
  @@ -146,6 +149,9 @@
        */
       private boolean refreshToolsPerRequest = false;
   
  +    /** Shall we catch Velocity Errors and report them in the log file? */
  +    private boolean catchErrors = true;
  +
       /**
        * Load all configured components and initialize them. This is
        * a zero parameter variant which queries the Turbine Servlet
  @@ -180,7 +186,7 @@
               // an empty toolBoxContext
               if (globalContext == null)
               {
  -                globalContext = new VelocityContext();
  +                globalContext = getNewContext();
               }
   
               // Register with the template service.
  @@ -224,6 +230,7 @@
       public Context getContext()
       {
           Context ctx = new VelocityContext(globalContext);
  +        addEventCartridge(ctx);
   
           //
           // We have only one context per request (that is the nature
  @@ -245,10 +252,50 @@
       public Context getNewContext()
       {
           Context ctx = new VelocityContext();
  +        addEventCartridge(ctx);
           return ctx;
       }
   
       /**
  +     * Creates a new Event Cartridge object and attaches it
  +     * to the passed Velocity Context
  +     *
  +     * @param ctx The context
  +     */
  +    private void addEventCartridge(Context ctx)
  +    {
  +        EventCartridge ec = new EventCartridge();
  +        ec.addEventHandler(this);
  +        ec.attachToContext(ctx);
  +    }
  +
  +    /**
  +     * MethodException Event Cartridge handler
  +     * for Velocity.
  +     *
  +     * It logs an execption thrown by the velocity processing
  +     * on error level into the log file
  +     *
  +     * @param clazz The class that threw the exception
  +     * @param method The Method name that threw the exception
  +     * @param e The exception that would've been thrown
  +     * @return A valid value to be used as Return value
  +     * @throws Exception We threw the exception further up
  +     */
  +    public Object methodException(Class clazz, String method, Exception e)
  +            throws Exception
  +    {
  +        log.error("Class " + clazz.getName() + "::" + method + " threw Exception", e);
  +
  +        if (!catchErrors)
  +        {
  +            throw e;
  +        }
  +
  +        return "[Turbine caught an Error here. Look into the turbine.log for further information]";
  +    }
  +
  +    /**
        * Create a Context from the RunData object.  Adds a pointer to
        * the RunData object to the VelocityContext so that RunData
        * is available in the templates.
  @@ -511,14 +558,16 @@
           throws Exception
       {
           // Get the configuration for this service.
  -        Configuration configuration = getConfiguration();
  +        Configuration conf = getConfiguration();
   
  -        configuration.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
  +        catchErrors = conf.getBoolean(CATCH_ERRORS_KEY, CATCH_ERRORS_DEFAULT);
  +        
  +        conf.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
                   SimpleLog4JLogSystem.class.getName());
  -        configuration.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM
  +        conf.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM
                   + ".log4j.category", "velocity");
  -
  -        Velocity.setExtendedProperties(createVelocityProperties(configuration));
  +        
  +        Velocity.setExtendedProperties(createVelocityProperties(conf));
           Velocity.init();
       }
   
  
  
  
  1.11      +7 -1      jakarta-turbine-2/src/java/org/apache/turbine/services/velocity/VelocityService.java
  
  Index: VelocityService.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/velocity/VelocityService.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- VelocityService.java	15 Jul 2003 08:26:16 -0000	1.10
  +++ VelocityService.java	15 Jul 2003 09:07:32 -0000	1.11
  @@ -87,6 +87,12 @@
       /** The Key for storing the RunData Object in the Context */
       String RUNDATA_KEY = "data";
   
  +    /** Shall we catch Velocity Errors and report them? */
  +    String CATCH_ERRORS_KEY = "catch.errors";
  +
  +    /** Default: Yes */
  +    boolean CATCH_ERRORS_DEFAULT = true;
  +
       /**
        * Process the request and fill in the template with the values
        * you set in the Context.
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org