Could this example be more generic? For instance, context could be read
from a flat properties file.

Also, concerning ScriptEngine.eval(String): the String argument should
be the content of the script itself, not the filename of the script.


  Claude

On Sat, 4 Aug 2012 01:03:56 +0530
Dishara Wijewardana <ddwijeward...@gmail.com> wrote:

> On Wed, Aug 1, 2012 at 11:13 AM, Dishara Wijewardana <
> ddwijeward...@gmail.com> wrote:
> 
> > Hi Claude
> >
> > I think now I was able to get the code what you expected. And I just
> > basically added the template merging stuff to script engine code and
> > the flow went successfully ;-).
> > Here is how to deal with tools with the JSR 223 API.
> >
> Hi Claude,
> Can you verify this code whether it meets your expectations ?
> 
> 
> >          ScriptEngineManager manager = new ScriptEngineManager();
> >          manager.registerEngineName("velocity", new
> > VelocityScriptEngineFactory());
> >          engine = manager.getEngineByName("velocity");
> >
> >         ScriptContext context = engine.getContext();
> >
> >         Properties properties = new Properties();
> >         properties.put("resource.loader", "class");
> >         properties.put("class.resource.loader.description",
> > "Template Class Loader");
> >         properties.put("class.resource.loader.class",
> >                 "org.apache.velocity.runtime.resource.loader.
> > ClasspathResourceLoader");
> >
> >         CustomEvent event = new CustomEvent("MyEvent");
> >         context.getBindings(ScriptContext.ENGINE_SCOPE).put("event",
> > event);
> >         context.setAttribute(VelocityScriptEngine.VELOCITY_PROPERTIES,
> > properties, ScriptContext.ENGINE_SCOPE);
> >
> >         Writer writer = new StringWriter();
> >         context.setWriter(writer);
> >         engine.eval("eventtool.vm");
> >         System.out.println("####### Tools output
> > #########\n"+writer);
> >
> >
> > On Wed, Aug 1, 2012 at 12:34 AM, Dishara Wijewardana <
> > ddwijeward...@gmail.com> wrote:
> >
> >>
> >>
> >> On Mon, Jul 30, 2012 at 12:40 AM, Claude Brisson
> >> <cla...@renegat.net>wrote:
> >>
> >>> On Sun, 29 Jul 2012 23:57:22 +0530
> >>> Dishara Wijewardana <ddwijeward...@gmail.com> wrote:
> >>>
> >>> > On Sun, Jul 29, 2012 at 10:20 PM, Claude Brisson
> >>> > <cla...@renegat.net> wrote:
> >>> >
> >>> > > Pardon me if I'm all wrong with that, but I really thought
> >>> > > that in the context of the JSR 223 API, templates themselves
> >>> > > were considered as scripts.
> >>> > >
> >>> > > This API is a wrapper around a scripting engine, that
> >>> > > evaluates scripts, which in our case are Velocity templates.
> >>> >
> >>> > My question is how do we manage following methods . We need to
> >>> > pass a template instance or a wrapper of it in to JSR 223
> >>> > ScriptEngine . How to achieve this without having extensive
> >>> > methods in ScriptEngine API?
> >>> >
> >>> > template.merge(context, writer);
> >>> > ve.getTemplate(vmTemplateFile);
> >>>
> >>> The Velocity context corresponds to the JSR 223 Bindings.
> >>> Populating it should not be a problem.
> >>>
> >>> The evaluation itself should gather the getTemplate() step and the
> >>> merge() step, or use one of the VelocityEngine.evaluate() method,
> >>> this is already what you did in VelocityScriptEngine.eval()
> >>> method.
> >>
> >>
> >> I think now I understood what you really meant. Will provide the
> >> client code ASAP and also I think I have to change the
> >> engine.eval() method in JSR223 API.
> >>
> >>
> >>
> >>> So I
> >>> really don't understand why you would need other methods. But
> >>> maybe I'm still missing something...
> >>>
> >>> Oh, by the way, I still saw some method comments like the
> >>> following one in your code:
> >>>     /**
> >>>      * Added creation inside sync block to avoid creating two
> >>> factories from a engine by two parallel threads at the same time.
> >>>      * Also the additional null check out from sync block is to
> >>> avoid every  thread to get blocked inside it even there is an
> >>> already created factory. */
> >>>
> >>> so I recall you that such comments may be useful when reading the
> >>> code, but they should be inside the method, not as a javadoc
> >>> comment. Javadoc users don't care about such details. Could you
> >>> please do something about that?
> >>
> >>
> >> I thought I cleared all. Sorry for the inconvenience caused. Will
> >> clean them all and update.
> >>
> > Done.
> >
> >>
> >>
> >>>
> >>>
> >>>   Claude
> >>>
> >>>
> >>> >
> >>> > >
> >>> > >   Claude
> >>> > >
> >>> > > On Sun, 29 Jul 2012 00:52:08 +0530
> >>> > > Dishara Wijewardana <ddwijeward...@gmail.com> wrote:
> >>> > >
> >>> > > > On Sat, Jul 28, 2012 at 12:53 PM, Claude Brisson
> >>> > > > <cla...@renegat.net> wrote:
> >>> > > >
> >>> > > > > On Sat, 28 Jul 2012 11:47:35 +0530
> >>> > > > > Dishara Wijewardana <ddwijeward...@gmail.com> wrote:
> >>> > > > >
> >>> > > > > > On Fri, Jul 27, 2012 at 2:20 PM, Claude Brisson
> >>> > > > > > <cla...@renegat.net> wrote:
> >>> > > > > >
> >>> > > > > > > Well, ok, but it's still not what I asked for: I
> >>> > > > > > > meant a command line tool that would, of course, use
> >>> > > > > > > your JSR 223 API classes.
> >>> > > > > > >
> >>> > > > > > HI Claude,
> >>> > > > > > Sorry for the inconvenience  I made. So what you meant
> >>> > > > > > is do the above similar kind of thing (a client code)
> >>> > > > > > which would not directly goes through the velocity API
> >>> > > > > > but through JSR223 API ? If so in that case we would
> >>> > > > > > need to add additional methods to the JSR API ? IS that
> >>> > > > > > OK ? Is it what you expected ?
> >>> > > > >
> >>> > > > > Well, the API is meant to be used, and a command line
> >>> > > > > wrapper seems to me one of the simplest things we could
> >>> > > > > do as an example.
> >>> > > > >
> >>> > > > > Why would you need additional methods to the JSR 223 API?
> >>> > > > > Which methods?
> >>> > > > >
> >>> > > > JSR223 API doesn't have a concept like templates which is in
> >>> > > > velocity. Hence it does not have getTemplate() method.
> >>> > > > ( because I have simply to execute a similar example as
> >>> > > > given through jsr 223 API, need a getTemplate() )
> >>> > > >
> >>> > > > >
> >>> > > > >
> >>> > > > >   Claude
> >>> > > > >
> >>> > > > > >
> >>> > > > > > >   Claude
> >>> > > > > > >
> >>> > > > > > > On Fri, 27 Jul 2012 00:00:31 +0530
> >>> > > > > > > Dishara Wijewardana <ddwijeward...@gmail.com> wrote:
> >>> > > > > > >
> >>> > > > > > > > Hi Claude,
> >>> > > > > > > > I was finally able to got a simple code
> >>> > > > > > > > working :-) .
> >>> > > > > > > >
> >>> > > > > > > > public class Main {
> >>> > > > > > > >
> >>> > > > > > > >     public static void main(String[] args) throws
> >>> > > > > > > > Exception { VelocityContext context = new
> >>> > > > > > > > VelocityContext(); Writer writer = new
> >>> > > > > > > > StringWriter();
> >>> > > > > > > >
> >>> > > > > > > >         CustomEvent event = new
> >>> > > > > > > > CustomEvent("subash"); context.put("event", event);
> >>> > > > > > > >         Template template =
> >>> > > > > > > > createTemplate("eventtool.vm");
> >>> > > > > > > >
> >>> > > > > > > >         template.merge(context, writer);
> >>> > > > > > > >         System.out.println(writer);
> >>> > > > > > > >         writer.close();
> >>> > > > > > > >     }
> >>> > > > > > > >
> >>> > > > > > > >     private static Template createTemplate(String
> >>> > > > > > > > vmTemplateFile) throws Exception {
> >>> > > > > > > >         VelocityEngine ve = new VelocityEngine();
> >>> > > > > > > >         Properties properties = new Properties();
> >>> > > > > > > >         properties.put("resource.loader", "class");
> >>> > > > > > > >
> >>> > > properties.put("class.resource.loader.description","Template
> >>> > > > > > > > Class Loader");
> >>> > > > > > > >         properties.put("class.resource.loader.class",
> >>> > > > > > > >
> >>> > > > > > > >
> >>> > > > >
> >>> "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
> >>> > > > > > > >         ve.init(properties);
> >>> > > > > > > >         return ve.getTemplate(vmTemplateFile);
> >>> > > > > > > >     }
> >>> > > > > > > >
> >>> > > > > > > >
> >>> > > > > > >
> >>> > > > >
> >>> > >
> >>>  
> >>> ---------------------------------------------------------------------------------------------------------------------------------
> >>> > > > > > > >
> >>> > > > > > > > And here is the template and the out put.
> >>> > > > > > > >
> >>> > > > > > > > *eventtool.vm*
> >>> > > > > > > >
> >>> > > > > > > > $event;
> >>> > > > > > > >
> >>> > > > > > > > Event Created by $event.getName()
> >>> > > > > > > > Event Created on $event.getDate()
> >>> > > > > > > >
> >>> > > > > > > >
> >>> > > > > > > > *Output*
> >>> > > > > > > > *
> >>> > > > > > > > *
> >>> > > > > > > > This is a test event template: created bysubash on
> >>> > > > > > > > Thu Jul 26 23:57:25 IST 2012;
> >>> > > > > > > >
> >>> > > > > > > > Event Created by subash
> >>> > > > > > > > Event Created on Thu Jul 26 23:57:25 IST 2012
> >>> > > > > > > >
> >>> > > > > > > >
> >>> > > > > > > >
> >>> > > > > > > > On Sun, Jul 22, 2012 at 4:54 PM, Claude Brisson
> >>> > > > > > > > <cla...@renegat.net> wrote:
> >>> > > > > > > >
> >>> > > > > > > > > > Did you mean Velocity maven plugin tool[1] as
> >>> > > > > > > > > > the command line tool ? If not can you please
> >>> > > > > > > > > > direct me to a link to find the example to
> >>> > > > > > > > > > follow. Because in the documentation page there
> >>> > > > > > > > > > are links to XML tool, View tool, JSP tool and
> >>> > > > > > > > > > etc. And couldn't find "command line tool"
> >>> > > > > > > > > > thing . Correct me if I am looking for
> >>> > > > > > > > > > irrelevant.
> >>> > > > > > > > > >
> >>> > > > > > > > > > [1] -
> >>> > > > > > > > > >
> >>> > > > > > > > >
> >>> > > > > > >
> >>> > > > >
> >>> > >
> >>> http://velocity.apache.org/tools/devel/maven-velocity-tools-plugin/index.html
> >>> > > > > > > > >
> >>> > > > > > > > > No, when I say "command line tool", I only mean a
> >>> > > > > > > > > plain old Java object with a "main(String
> >>> > > > > > > > > args[])" method, that you can call from the
> >>> > > > > > > > > command line...
> >>> > > > > > > > >
> >>> > > > > > > > >
> >>> > > > > > > > >   Claude
> >>> > > > > > > > >
> >>> > > > > > > >
> >>> > > > > > > >
> >>> > > > > > > >
> >>> > > > > > >
> >>> > > > > > >
> >>> > > > > >
> >>> > > > > >
> >>> > > > >
> >>> > > > >
> >>> > > >
> >>> > > >
> >>> > >
> >>> > >
> >>> >
> >>> >
> >>>
> >>>
> >>
> >>
> >> --
> >> Thanks
> >> /Dishara
> >>
> >>
> >
> >
> > --
> > Thanks
> > /Dishara
> >
> >
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@velocity.apache.org
For additional commands, e-mail: dev-h...@velocity.apache.org

Reply via email to