5 Replies Latest reply on Jun 12, 2006 11:29 AM by shane.bryzak

    Using Velocity or XSLT Templates in Seam

    dhinojosa

      I am transporting over a web framework action over to a session bean to use in Seam. The webframework action transformed either a Velocity template or a XSLT into an email that I can send out. Now that my service is on the session bean instead of a web framework action, how do I get a velocity template or XSLT template into the session bean for transformation. Before as a web action I used to do something like this:

      Properties properties = new Properties();
      String realPath = servletContext.getRealPath("WEB-INF");
      properties.put("file.resource.loader.path", realPath);
      Velocity.init(properties);

      This would retrieve all the velocity templates from the WEB-INF directory. Now there is no dependency of the WEB-INF or servletContext. Does anyone have any strategy for something like this?

        • 1. Re: Using Velocity or XSLT Templates in Seam
          shane.bryzak

          Create your own response resource loader by extending org.apache.velocity.runtime.resource.loader.ResourceLoader, and override the getResourceStream() method. Then set the Velocity engine "service.resource.loader.class" property to point to your new resource loader class. Then you can use getClass().getResourceAsStream(<resource name goes here>) to load your templates.

          • 2. Re: Using Velocity or XSLT Templates in Seam
            dhinojosa

            and what shall I put in getResourceAsStream() ?

            • 3. Re: Using Velocity or XSLT Templates in Seam
              shane.bryzak

              The path to the file containing your Velocity template. So for example, if you're storing your templates in the WEB-INF directory and they have a .template extension, your getResourceStream() method would look like this:

              public InputStream getResourceStream(String source)
               throws ResourceNotFoundException
              {
               return getClass().getResourceAsStream(
               String.format("/WEB-INF/%s.template", source));
              }
              


              • 4. Re: Using Velocity or XSLT Templates in Seam
                dhinojosa

                Thanks for your help, but I don't think I can or should rely on /WEB-INF/. That way I can reuse those same beans that have non-web-based clients. I think the ultimate best solution could be a JMX setup so that we can get a velocity context injected into a stateless or stateful bean.


                IOW, something like this

                @Stateful
                public class MySessionBean {

                @Resource(name="apache/env/VelocityContext")
                private VelocityContext velocityContext;

                }

                • 5. Re: Using Velocity or XSLT Templates in Seam
                  shane.bryzak

                  The template resources could come from anywhere; a directory in your application archive, db table, etc - your resource loader just has to load them from somewhere. Personally I'd use an application-scoped bean to wrap the velocity context, annotate it with @Startup and define a @Create method that initialises the velocity context and possibly loads and caches your templates.

                  If you want to expose it as an MBean then you'd have to write it as a JBoss service and provide a management interface, but I don't really see any advantage to doing this unless you have special requirements.