velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nathan Bubna" <nbu...@gmail.com>
Subject Re: how to configure Velocity entirely programmatically?
Date Thu, 29 Mar 2007 16:47:07 GMT
On 3/29/07, Nathan Bubna <nbubna@gmail.com> wrote:
> oops, meant to send this to the list too...
>
> ---------- Forwarded message ----------
> From: Nathan Bubna <nbubna@gmail.com>
> Date: Mar 29, 2007 9:16 AM
> Subject: Re: how to configure Velocity entirely programmatically?
> To: dd@daviddonohue.com
>
> On 3/29/07, David Donohue <dd@daviddonohue.com> wrote:
> > Nathan and friends,
> > I followed Nathan's fine, detailed instructions on configuring my Velocity
> > Tools 1.3 servlet programmatically.  I am running into difficulty with the
> > toolbox.  Nathan described this
> >
> > ServletContext application = config.getServletContext();
> > toolboxManager =
> > ServletToolboxManager.getInstance(application,
> > "path/to/empty/toolbox.xml");
> >
> > I find that absolute path does not work here.  I have also tried a variety
> > of relative paths.  I am wondering if configuring the toolbox only works
> > with relative path, which is relative to the servlet's root file path?
>
> yeah, unfortunately, ServletToolboxManager.getInstance() calls
> servletContext.getResourceAsStream(path) to get the toolbox file, so
> the file must be in the servlet context's purview.
>
> > The
> > trouble is my servlet is set up (in Jetty 6.1.1) to not really have such a
> > root file path.  I am using classpathloader, and loading all my .vm files
> > from the classpath.
>
> yeah, loading a toolbox from the classpath or absolute path are things
> i've put in 2.0 but not in 1.3.   perhaps i'll add support for this in
> 1.4 too.
>
> > I was able to configure the velocity.properties using an absolute file path,
> > as in
> >
> >  public ExtendedProperties loadConfiguration(ServletConfig config) {
> >         try {
> >             props = new
> > ExtendedProperties("c:/absolute/path/to/velocity.properties");
> >
> > So am I left with writing my own version of ToolboxManager?
>
> it seems that way. :(  though this can be as simple as copying the
> source of ServletToolboxManager into your own code and simply changing
> the constructor from private to public.   no other change needed, and
> then you can just do
>
> toolboxManager = new ServletToolboxManager(servletContext);
>
> or if you'd rather keep it in your velocity-tools jar, you can make
> the same small alteration to the source code for 1.3 and rebuild it
> yourself.   depending on your setup, that may be a bit easier.
>
> i'll be sure to have some remedy for this in VelocityTools 1.4, and
> not just in 2.0.


i've changed the constructor from private to protected.  this will be
in the next release.


> > Thanks again for any direction!!
> >
> > David
> >
> > On 3/27/07, Nathan Bubna <nbubna@gmail.com> wrote:
> > > Ok, to get a ServletToolboxManager, you need an empty toolbox file at
> > > a minimum.   There's no way around this with the released versions.
> > > To do otherwise you would have to alter and re-compile VelocityTools
> > > yourself.  I'll assume for now that that's not ok with you.  If it is,
> > > then let me know, as it opens a world of other possibilities.
> > >
> > > Anyway, in your initToolbox(ServletConfig config) method, you will
> > > want to start with something like this:
> > >
> > > ServletContext application = config.getServletContext();
> > > toolboxManager = ServletToolboxManager.getInstance
> > (application,
> > > "path/to/empty/toolbox.xml");
> > >
> > > toolboxManager is a protected variable in VelocityViewServlet, so you
> > > need only set it, not declare it.
> > >
> > > now that you have a ServletToolboxManager, you need to begin to add
> > > tools to it.  This is pretty simple.  You create either a
> > > ServletToolInfo (or DataInfo) object for each tool and call
> > >
> > > toolboxManager.addTool(info);  // or addData(data)
> > >
> > > these info classes are pretty straightforward.  you create a valid one
> > like so:
> > >
> > > // required stuff
> > > ServletToolInfo info = new ServletToolInfo();
> > > info.setKey("date");
> > > info.setClassname(DateTool.class.getName());
> > > info.setScope("application");
> > > // optional stuff
> > > info.setParameter("format", "yyyy-M-d H:m:s");
> > >
> > > or
> > >
> > > DataInfo data = new DataInfo();
> > > data.setKey("version");
> > > data.setType("number");
> > > // note that values always begin as strings for these
> > > data.setValue("1.2");
> > >
> > > add all the tools you want, and then you are good to go.  there
> > > shouldn't be anything else to do.
> > >
> > > p.s. if you want to keep tabs on Tools 2.0 dev, feel free to subscribe
> > > to the dev@velocity list and or checkout the source so far from:
> > >
> > http://svn.apache.org/repos/asf/velocity/tools/branches/2.x/
> > >
> > > On 3/27/07, David Donohue < dd@daviddonohue.com> wrote:
> > > > Thanks, Nathan for your fine reply!  I had looked at the toolbox API and
> > had
> > > > similar impression but hoped I was missing something.
> > > >
> > > > I need to limit the scope of which tools are available when, so I
> > probably
> > > > need ServletToolboxManager.
> > > >
> > > > I wish to forego web.xml as well.  I am using Jetty 6.1.1 and I am
> > pretty
> > > > sure this can support fully programmatic configuration of a
> > WebAppContext
> > > > (not exactly sure how yet).
> > > >
> > > > I look forward to your further explanation, and to Tools 2.0 as well!
> > > > David
> > > >
> > > >
> > > > On 3/27/07, Nathan Bubna < nbubna@gmail.com> wrote:
> > > > >
> > > > > On 3/27/07, David Donohue <dd@daviddonohue.com> wrote:
> > > > > > Sorry if this has been addressed but I have searched and cannot
find
> > the
> > > > > > complete answer.
> > > > >
> > > > > Unfortunately, i don't think there is any tutorial or example code
out
> > > > > there for what you ask.      Not much effort has been put into
> > > > > supporting this (though i'm looking to change that in VelocityTools
> > > > > 2.0).
> > > > >
> > > > > > Can anyone kindly point me to a tutorial or example code showing
how
> > I
> > > > > would
> > > > > > completely configure Velocity programmatically?
> > > > > > Specifically, I wish to configure a VelocityViewServlet with
a
> > toolbox,
> > > > > but
> > > > > > without using any of these configuration files:
> > > > > > web.xml
> > > > > > toolbox.xml
> > > > > > velocity.properties
> > > > >
> > > > > This is doable, but with limits.  I can try to point you in the right
> > > > > direction and help you out where you get stuck.  Here's a start:
> > > > >
> > > > > First, you need to create your own subclass of VelocityViewServlet.
> > > > > There's really no way around that.  In that subclass, there are two
> > > > > methods you will especially want to override:
> > > > >
> > > > > loadConfiguration(ServletConfig)
> > > > >
> > > > > and
> > > > >
> > > > > initToolbox(ServletConfig)
> > > > >
> > > > > These are your best opportunities to configure the VelocityEngine
and
> > > > > ToolboxManager.
> > > > >
> > > > > Basically what you want to do in loadConfiguration() is return a
new
> > > > > ExtendedProperties
> > > > > (
> > > > >
> > http://jakarta.apache.org/commons/collections/api/org/apache/commons/collections/ExtendedProperties.html
> > > > > )
> > > > > object upon which you have set your velocity properties
> > > > > programmatically.  It's pretty simple, just create an instance, set
> > > > > your props using addProperty(String,Object), then return it.   Don't
> > > > > worry about calling super.loadConfiguration(), it doesn't matter
if
> > > > > you don't have a velocity.properties file defined.
> > > > >
> > > > > In initToolbox(), things are a little trickier.  Support for
> > > > > programmatically creating a toolbox is particularly poor, and creating
> > > > > a ServletToolboxManager without at least an empty toolbox xml file
is
> > > > > sadly impossible as things stand.  So, you face a choice, you must
> > > > > either create an empty toolbox.xml such as:
> > > > >
> > > > > <toolbox>
> > > > > </toolbox>
> > > > >
> > > > > So that you may create a ServletToolboxManager and programmatically
> > > > > configure the tools for it, or else you must forgo the extra servlet
> > > > > support for the ServletToolboxManager and simply create an
> > > > > XMLToolboxManager, which offers no ability to put tools in varying
> > > > > scopes and may also limit which tools will work.  Make that decision
> > > > > and then i can help you from there.  I don't have time to fully
> > > > > describe both paths.
> > > > >
> > > > > Oh, and know that there are improvements in this area on the horizon.
> > > > > We're working on VelocityTools 2.0, including full, easy support
for
> > > > > programmatically creating toolboxes.
> > > > >
> > > > > Oh, and finally, the web.xml is not something specific to the
> > > > > VelocityViewServlet.  That is used by the servlet engine (e.g. Tomcat)
> > > > > to initialize the servlet.  I don't know if you can skip on that,
it
> > > > > may depend on the nature of your webapp and the servlet engine used.
> > > > >
> > > > > > Thanks for any direction!
> > > > > > David
> > > > > >
> > > > >
> > > >
> > >
> >
> >
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Mime
View raw message