You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by rhild <rh...@yahoo.com> on 2009/02/23 20:04:51 UTC

tools 1.4=>2.0 upgrade, configuration question

With 1.4 I could override the loadConfiguration method of VelocityViewServlet
to modify/add properties of the velocity configuration at runtime.  From a
previous post last year it seems the way to do this now is:

http://www.nabble.com/Tools-2.0---VelocityLayoutServlet---loadConfiguration-td18268452.html

When I try this approach the MyVelocityView class I created is not used. I
am setting up the init params for the servlet as indicated.

Question: Does the link above still represent the best way to accomplish
what used to be done by overriding loadConfiguration() in
VelocityViewServlet?  If not, what would you recommend.

thanks,

-Ray


-- 
View this message in context: http://www.nabble.com/tools-1.4%3D%3E2.0-upgrade%2C-configuration-question-tp22167805p22167805.html
Sent from the Velocity - User mailing list archive at Nabble.com.


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


Re: tools 1.4=>2.0 upgrade, configuration question

Posted by rhild <rh...@yahoo.com>.
Thanks for the quick and accurate help Nathan.  Everything is working fine
with 1.6.1 and I'm sticking with extending VelocityView.

-Ray


Nathan Bubna wrote:
> 
> On Wed, Feb 25, 2009 at 11:25 AM, rhild <rh...@yahoo.com> wrote:
>>
>> I was using beta 2.  I just updated to beta 3 and all is well now. I
>> don't
>> know why I didn't notice sooner I was one version behind.
>>
>> While I have your attention a few more questions.  I see that beta 3
>> includes velocity 1.5.  Is there any reason to think the beta 3 would
>> have
>> any problems with the latest 1.6.1?
> 
> None at all.  It should work great with 1.6.
> 
>> Since I am already extending VelocityViewServlet it would be cleaner to
>> override int() there to configure the engine rather than extend
>> VelocityView, if it will work the way I want. I want velocity.properties
>> to
>> be used and then add to or modify from there. If I understand the code
>> you
>> gave overriding init() it is throwing out the original config and
>> starting
>> from scratch, which is not what I want.  Am I understanding that code
>> correctly? If so, is there an easy way to modify the config versus
>> starting
>> from scratch in init().  If the answer is no, that's OK.  What I have now
>> works.
> 
> The answer is no; if you drop a new VelocityEngine into the
> VelocityView, then all configuration done on the previous engine is
> for naught.  This is why i recommend having your own VelocityView
> subclass.    However, if you really prefer to just have the
> VelocityViewServlet subclass, it's not terribly complicated to
> duplicate that effort yourself.  You can copy the code that does it
> out of the VelocityView class.  This is open source, after all. :)
> 
> 

-- 
View this message in context: http://www.nabble.com/tools-1.4%3D%3E2.0-upgrade%2C-configuration-question-tp22167805p22214498.html
Sent from the Velocity - User mailing list archive at Nabble.com.


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


Re: tools 1.4=>2.0 upgrade, configuration question

Posted by Nathan Bubna <nb...@gmail.com>.
On Wed, Feb 25, 2009 at 11:25 AM, rhild <rh...@yahoo.com> wrote:
>
> I was using beta 2.  I just updated to beta 3 and all is well now. I don't
> know why I didn't notice sooner I was one version behind.
>
> While I have your attention a few more questions.  I see that beta 3
> includes velocity 1.5.  Is there any reason to think the beta 3 would have
> any problems with the latest 1.6.1?

None at all.  It should work great with 1.6.

> Since I am already extending VelocityViewServlet it would be cleaner to
> override int() there to configure the engine rather than extend
> VelocityView, if it will work the way I want. I want velocity.properties to
> be used and then add to or modify from there. If I understand the code you
> gave overriding init() it is throwing out the original config and starting
> from scratch, which is not what I want.  Am I understanding that code
> correctly? If so, is there an easy way to modify the config versus starting
> from scratch in init().  If the answer is no, that's OK.  What I have now
> works.

The answer is no; if you drop a new VelocityEngine into the
VelocityView, then all configuration done on the previous engine is
for naught.  This is why i recommend having your own VelocityView
subclass.    However, if you really prefer to just have the
VelocityViewServlet subclass, it's not terribly complicated to
duplicate that effort yourself.  You can copy the code that does it
out of the VelocityView class.  This is open source, after all. :)

> thanks,
>
> -Ray
>
>
> Nathan Bubna wrote:
>>
>> Odd. Which VelocityViewServlet does your VelocityServlet extend?  It
>> should be extending
>> org.apache.velocity.tools.view.VelocityViewServlet.
>>
>> Also, which version of VelocityTools 2 are you using?
>>
>>
>
> --
> View this message in context: http://www.nabble.com/tools-1.4%3D%3E2.0-upgrade%2C-configuration-question-tp22167805p22209874.html
> Sent from the Velocity - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

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


Re: tools 1.4=>2.0 upgrade, configuration question

Posted by rhild <rh...@yahoo.com>.
I was using beta 2.  I just updated to beta 3 and all is well now. I don't
know why I didn't notice sooner I was one version behind.

While I have your attention a few more questions.  I see that beta 3
includes velocity 1.5.  Is there any reason to think the beta 3 would have
any problems with the latest 1.6.1?

Since I am already extending VelocityViewServlet it would be cleaner to
override int() there to configure the engine rather than extend
VelocityView, if it will work the way I want. I want velocity.properties to
be used and then add to or modify from there. If I understand the code you
gave overriding init() it is throwing out the original config and starting
from scratch, which is not what I want.  Am I understanding that code
correctly? If so, is there an easy way to modify the config versus starting
from scratch in init().  If the answer is no, that's OK.  What I have now
works.  

thanks,

-Ray


Nathan Bubna wrote:
> 
> Odd. Which VelocityViewServlet does your VelocityServlet extend?  It
> should be extending
> org.apache.velocity.tools.view.VelocityViewServlet.
> 
> Also, which version of VelocityTools 2 are you using?
> 
> 

-- 
View this message in context: http://www.nabble.com/tools-1.4%3D%3E2.0-upgrade%2C-configuration-question-tp22167805p22209874.html
Sent from the Velocity - User mailing list archive at Nabble.com.


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


Re: tools 1.4=>2.0 upgrade, configuration question

Posted by Nathan Bubna <nb...@gmail.com>.
Odd. Which VelocityViewServlet does your VelocityServlet extend?  It
should be extending
org.apache.velocity.tools.view.VelocityViewServlet.

Also, which version of VelocityTools 2 are you using?

On Wed, Feb 25, 2009 at 9:01 AM, rhild <rh...@yahoo.com> wrote:
>
> Here is the code:
>
> package atr.servlet;
> import org.apache.log4j.Logger;
> import org.apache.velocity.tools.view.VelocityView;
> import org.apache.velocity.tools.view.JeeConfig;
> import org.apache.velocity.app.VelocityEngine;
>
> import atr.utils.SystemProperties;
>
> public class ConfigurableVelocityView extends VelocityView {
>        static Logger logger =
> Logger.getLogger(ConfigurableVelocityView.class.getName());
>
>        public ConfigurableVelocityView(JeeConfig config) {
>                super(config);
>        }
>
>        protected void configure(JeeConfig config, VelocityEngine engine) {
>                super.configure(config, engine);
>                logger.info("Using ConfigurableVelocityView");
>                String[] props = { "webapp.resource.loader.cache",
> "webapp.resource.loader.modificationCheckInterval",
>                                "velocimacro.library.autoreload" };
>                for (String prop : props) {
>                        String value = SystemProperties.INSTANCE.getProperty("prop");
>                        if (value != null && value.length() > 0) {
>                                engine.setProperty(prop, value);
>                                logger.info("setting custom Velocity property: name=" + prop + ",
> value=" + value);
>                        }
>                }
>
>        }
> }
>
> here is the relevant piece of web.xml:
>
>        <servlet>
>                <servlet-name>velocity</servlet-name>
>                <servlet-class>atr.servlet.VelocityServlet</servlet-class>
>                <init-param>
>                        <param-name>org.apache.velocity.tools.view.class</param-name>
>                        <param-value>atr.servlet.ConfigurableVelocityView</param-value>
>                </init-param>
>        </servlet>
>
> atr.servlet.VelocityServlet extends VelocityViewServlet.  I still use a
> velocity.properties file.  I just want to selectively override some
> properties based on  the environment I'm in.  For instance, I want to leave
> the properties file as it would be for a production environment, with
> template caching on and macro autoreloading off, etc.  But in a dev
> environment I'd override those.
>
> When I try the code as shown I get no exceptions thrown or additional log
> mesages from Velocity.  There is no indication that my code above runs as
> the log messages don't appear and the behavior of velocity does not change,
> i.e. templates are still cached.
>
> --
> View this message in context: http://www.nabble.com/tools-1.4%3D%3E2.0-upgrade%2C-configuration-question-tp22167805p22207069.html
> Sent from the Velocity - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

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


Re: tools 1.4=>2.0 upgrade, configuration question

Posted by rhild <rh...@yahoo.com>.
Here is the code:

package atr.servlet;
import org.apache.log4j.Logger;
import org.apache.velocity.tools.view.VelocityView;
import org.apache.velocity.tools.view.JeeConfig;
import org.apache.velocity.app.VelocityEngine;

import atr.utils.SystemProperties;

public class ConfigurableVelocityView extends VelocityView {
	static Logger logger =
Logger.getLogger(ConfigurableVelocityView.class.getName());

	public ConfigurableVelocityView(JeeConfig config) {
		super(config);
	}

	protected void configure(JeeConfig config, VelocityEngine engine) {
		super.configure(config, engine);
		logger.info("Using ConfigurableVelocityView");
		String[] props = { "webapp.resource.loader.cache",
"webapp.resource.loader.modificationCheckInterval",
				"velocimacro.library.autoreload" };
		for (String prop : props) {
			String value = SystemProperties.INSTANCE.getProperty("prop");
			if (value != null && value.length() > 0) {
				engine.setProperty(prop, value);
				logger.info("setting custom Velocity property: name=" + prop + ",
value=" + value);
			}
		}

	}
}

here is the relevant piece of web.xml:

	<servlet>
		<servlet-name>velocity</servlet-name>
		<servlet-class>atr.servlet.VelocityServlet</servlet-class>
		<init-param>
			<param-name>org.apache.velocity.tools.view.class</param-name>
			<param-value>atr.servlet.ConfigurableVelocityView</param-value>
		</init-param>
	</servlet>

atr.servlet.VelocityServlet extends VelocityViewServlet.  I still use a
velocity.properties file.  I just want to selectively override some
properties based on  the environment I'm in.  For instance, I want to leave
the properties file as it would be for a production environment, with
template caching on and macro autoreloading off, etc.  But in a dev
environment I'd override those.

When I try the code as shown I get no exceptions thrown or additional log
mesages from Velocity.  There is no indication that my code above runs as
the log messages don't appear and the behavior of velocity does not change,
i.e. templates are still cached.

-- 
View this message in context: http://www.nabble.com/tools-1.4%3D%3E2.0-upgrade%2C-configuration-question-tp22167805p22207069.html
Sent from the Velocity - User mailing list archive at Nabble.com.


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


Re: tools 1.4=>2.0 upgrade, configuration question

Posted by Nathan Bubna <nb...@gmail.com>.
You could also just try extending the VelocityViewServlet, and
creating/configuring a VelocityEngine on your own in init() and doing
something like:

public void init(ServletConfig config) throws ServletException
{
    super.init(config);

    VelocityEngine myWay = new VelocityEngine();
    // configure it your way here...

    getVelocityView().setVelocityEngine(myWay);
}

You might lose a few log messages from the engine being replaced, but
that might not be a big deal to you.

On Wed, Feb 25, 2009 at 6:52 AM, Nathan Bubna <nb...@gmail.com> wrote:
> Yes, if you need to programmatically manipulate the VelocityEngine
> properties, rather than set them via a velocity.properties file, this
> is still the way to go.
>
> You are sure you specified the org.apache.velocity.tools.view.class
> property in your init-params?  Can you share your code and config,
> perhaps i can spot a mistake that is not obvious to you.   Did you get
> any exceptions or log messages?  Make sure your VelocityView subclass
> has a constructor that accepts a JeeConfig object.
>
> On Mon, Feb 23, 2009 at 11:04 AM, rhild <rh...@yahoo.com> wrote:
>>
>> With 1.4 I could override the loadConfiguration method of VelocityViewServlet
>> to modify/add properties of the velocity configuration at runtime.  From a
>> previous post last year it seems the way to do this now is:
>>
>> http://www.nabble.com/Tools-2.0---VelocityLayoutServlet---loadConfiguration-td18268452.html
>>
>> When I try this approach the MyVelocityView class I created is not used. I
>> am setting up the init params for the servlet as indicated.
>>
>> Question: Does the link above still represent the best way to accomplish
>> what used to be done by overriding loadConfiguration() in
>> VelocityViewServlet?  If not, what would you recommend.
>>
>> thanks,
>>
>> -Ray
>>
>>
>> --
>> View this message in context: http://www.nabble.com/tools-1.4%3D%3E2.0-upgrade%2C-configuration-question-tp22167805p22167805.html
>> Sent from the Velocity - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
>>
>

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


Re: tools 1.4=>2.0 upgrade, configuration question

Posted by Nathan Bubna <nb...@gmail.com>.
Yes, if you need to programmatically manipulate the VelocityEngine
properties, rather than set them via a velocity.properties file, this
is still the way to go.

You are sure you specified the org.apache.velocity.tools.view.class
property in your init-params?  Can you share your code and config,
perhaps i can spot a mistake that is not obvious to you.   Did you get
any exceptions or log messages?  Make sure your VelocityView subclass
has a constructor that accepts a JeeConfig object.

On Mon, Feb 23, 2009 at 11:04 AM, rhild <rh...@yahoo.com> wrote:
>
> With 1.4 I could override the loadConfiguration method of VelocityViewServlet
> to modify/add properties of the velocity configuration at runtime.  From a
> previous post last year it seems the way to do this now is:
>
> http://www.nabble.com/Tools-2.0---VelocityLayoutServlet---loadConfiguration-td18268452.html
>
> When I try this approach the MyVelocityView class I created is not used. I
> am setting up the init params for the servlet as indicated.
>
> Question: Does the link above still represent the best way to accomplish
> what used to be done by overriding loadConfiguration() in
> VelocityViewServlet?  If not, what would you recommend.
>
> thanks,
>
> -Ray
>
>
> --
> View this message in context: http://www.nabble.com/tools-1.4%3D%3E2.0-upgrade%2C-configuration-question-tp22167805p22167805.html
> Sent from the Velocity - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

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