You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by trad-ex <tr...@inter7.jp> on 2006/04/20 11:17:54 UTC

Can velocity load a properties file ?

Hi All,

I tried to load property resource bundle file, but failed to get an 
instance of FileInputStream.
My code is below:

#set( $fStreamClass = $util.class.forName("java.io.FileInputStream") )
#set( $iStreamClass = $util.class.forName("java.io.InputStream") )
#set( $stringClass = $util.class.forName("java.lang.String") )
#set( $bundleClass 
     = $util.class.forName("java.util.PropertyResourceBundle") )
#set( $userNameFilePath = $util.getPortalFile($userNameFile) )
#set( $resStream 
     = $fStreamClass.getConstructor( [$stringClass] )
       .newInstance( ["$userNameFile"] ) )
#set( $resBundle 
     = $bundleClass.getConstructor( [$iStreamClass] )
     .newInstance( [$resStream] ) )

$util object is provided by our application to inject functionality into 
velocity context.
I succeeded to get $fStreamClass & $iStreamClass & $bundleClass
But I failed to get $resStream & $resBundle.

My questions are:
1. Can I load the specific property file in Velocity context ?
2. What's wrong with my velocity script ?

Best Regards, and thanks in advance.
trad-ex

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


Re: Can velocity load a properties file ?

Posted by Will Glass-Husain <wg...@forio.com>.
Ugh.  Using reflection in Velocity to create a new class is a pretty messy
kludge.  Would it be possible just to create a method in $util that loads
this, e.g. $util.getPropertyResourceBundle?

WILL



On 4/20/06, trad-ex <tr...@inter7.jp> wrote:
>
> Hi All,
>
> I tried to load property resource bundle file, but failed to get an
> instance of FileInputStream.
> My code is below:
>
> #set( $fStreamClass = $util.class.forName("java.io.FileInputStream") )
> #set( $iStreamClass = $util.class.forName("java.io.InputStream") )
> #set( $stringClass = $util.class.forName("java.lang.String") )
> #set( $bundleClass
>      = $util.class.forName("java.util.PropertyResourceBundle") )
> #set( $userNameFilePath = $util.getPortalFile($userNameFile) )
> #set( $resStream
>      = $fStreamClass.getConstructor( [$stringClass] )
>        .newInstance( ["$userNameFile"] ) )
> #set( $resBundle
>      = $bundleClass.getConstructor( [$iStreamClass] )
>      .newInstance( [$resStream] ) )
>
> $util object is provided by our application to inject functionality into
> velocity context.
> I succeeded to get $fStreamClass & $iStreamClass & $bundleClass
> But I failed to get $resStream & $resBundle.
>
> My questions are:
> 1. Can I load the specific property file in Velocity context ?
> 2. What's wrong with my velocity script ?
>
> Best Regards, and thanks in advance.
> trad-ex
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>


--
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

Re: Can velocity load a properties file ?

Posted by Glenn Holmer <gh...@weycogroup.com>.
On Friday 21 April 2006 09:38, Will Glass-Husain wrote:
> Velocity works well as a markup language where text is directly
> inserted in a template via properties or method calls.  Such
> templates are easy to use and easy to maintain.  If you need a
> scripting or programming language there are much better alternatives.

Very well said.

-- 
____________________________________________________________
Glenn Holmer                          gholmer@weycogroup.com
Software Engineer                        phone: 414-908-1809
Weyco Group, Inc.                          fax: 414-908-1601

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


Re: Can velocity load a properties file ?

Posted by trad-ex <tr...@inter7.jp>.
Hi Will,

Thanks a lot for your commentary.
I will try to persuade our development team to implement some 
functionality in helper object in order not to use reflections code.

In fact, I hacked (because I don't have priviledge of committing java 
source) the helper object ($util) to add a few line of java source and 
succeeded. It was very simple!

Thanks .


>Hi Trad-Ex,
>
>Far be it from me to dictate how to develop your app.  But I tend to avoid
>reflection in Velocity for the following reasons:
>
>--> It's a complex syntax, easy to mess up, hard to read, hard to debug.
>
>--> There's a much simpler alternative (putting objects and methods directly
>in the context).
>
>--> This is actively dangerous for cases in which third parties submit
>templates as they have full acess to files and other resources on your
>machine
>
>Velocity works well as a markup language where text is directly inserted in
>a template via properties or method calls.  Such templates are easy to use
>and easy to maintain.  If you need a scripting or programming language there
>are much better alternatives.
>
>best,
>WILL
>
>On 4/20/06, trad-ex <tr...@inter7.jp> wrote:
>>
>> Hi Barbara,
>> Hi Will,
>>
>> Thanks a lot for your response.
>> As Will says, #set can make any class objects.
>> In fact, I often make instances of ArrayList, and use them.
>> But I think whether there are tools like org.apache.velocity.tools.
>> struts.MessageTool or not.
>>
>> I will follow Will's advice to implement methods like $util.
>> getPropertyResourceBundle( path ) in $util.
>>
>> BTW, why do you propse to limit to use reflection ?
>> It's a powerful way to provide such availability into velocity realm, I
>> guess. Would you mind asking you for reasons ?
>>
>> Best Regards,
>> trad-ex
>>
>>
>> >Are you sure?  I think #set can be used for any class.
>> >
>> >I've proposed to limit this to prohibit reflection by default (such as
>> what
>> >trad-ex is doing), but we haven't applied the patch yet.
>> >
>> >WILL
>> >
>> >On 4/20/06, Barbara Baughman <ba...@utdallas.edu> wrote:
>> >>
>> >> You cannot use the #set directive to designate just any kind of JAVA
>> >> object.  See the User Guide for allowable references.  I believe the
>> >> #set directive you have will automatically call the toString method of
>> >> the class in order to set the variable to a valid reference, but you
>> >> can check by printing the value of the set variable.
>> >>
>> >> Barbara Baughman
>> >> X2157
>> >>
>> >> On Thu, 20 Apr 2006, trad-ex wrote:
>> >>
>> >> > Hi All,
>> >> >
>> >> > I tried to load property resource bundle file, but failed to get an
>> >> > instance of FileInputStream.
>> >> > My code is below:
>> >> >
>> >> > #set( $fStreamClass = $util.class.forName("java.io.FileInputStream")
>> )
>> >> > #set( $iStreamClass = $util.class.forName("java.io.InputStream") )
>> >> > #set( $stringClass = $util.class.forName("java.lang.String") )
>> >> > #set( $bundleClass
>> >> >      = $util.class.forName("java.util.PropertyResourceBundle") )
>> >> > #set( $userNameFilePath = $util.getPortalFile($userNameFile) )
>> >> > #set( $resStream
>> >> >      = $fStreamClass.getConstructor( [$stringClass] )
>> >> >        .newInstance( ["$userNameFile"] ) )
>> >> > #set( $resBundle
>> >> >      = $bundleClass.getConstructor( [$iStreamClass] )
>> >> >      .newInstance( [$resStream] ) )
>> >> >
>> >> > $util object is provided by our application to inject functionality
>> into
>> >> > velocity context.
>> >> > I succeeded to get $fStreamClass & $iStreamClass & $bundleClass
>> >> > But I failed to get $resStream & $resBundle.
>> >> >
>> >> > My questions are:
>> >> > 1. Can I load the specific property file in Velocity context ?
>> >> > 2. What's wrong with my velocity script ?
>> >> >
>> >> > Best Regards, and thanks in advance.
>> >> > trad-ex
>> >> >
>> >> > ---------------------------------------------------------------------
>> >> > To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>> >> > For additional commands, e-mail:
>> velocity-user-help@jakarta.apache.org
>> >> >
>> >> >
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>> >> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>> >>
>> >>
>> >
>> >
>> >--
>> >Forio Business Simulations
>> >
>> >Will Glass-Husain
>> >wglass@forio.com
>> >www.forio.com
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>>
>>
>
>
>--
>Forio Business Simulations
>
>Will Glass-Husain
>wglass@forio.com
>www.forio.com

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


Re: Can velocity load a properties file ?

Posted by Will Glass-Husain <wg...@forio.com>.
Hi Trad-Ex,

Far be it from me to dictate how to develop your app.  But I tend to avoid
reflection in Velocity for the following reasons:

--> It's a complex syntax, easy to mess up, hard to read, hard to debug.

--> There's a much simpler alternative (putting objects and methods directly
in the context).

--> This is actively dangerous for cases in which third parties submit
templates as they have full acess to files and other resources on your
machine

Velocity works well as a markup language where text is directly inserted in
a template via properties or method calls.  Such templates are easy to use
and easy to maintain.  If you need a scripting or programming language there
are much better alternatives.

best,
WILL

On 4/20/06, trad-ex <tr...@inter7.jp> wrote:
>
> Hi Barbara,
> Hi Will,
>
> Thanks a lot for your response.
> As Will says, #set can make any class objects.
> In fact, I often make instances of ArrayList, and use them.
> But I think whether there are tools like org.apache.velocity.tools.
> struts.MessageTool or not.
>
> I will follow Will's advice to implement methods like $util.
> getPropertyResourceBundle( path ) in $util.
>
> BTW, why do you propse to limit to use reflection ?
> It's a powerful way to provide such availability into velocity realm, I
> guess. Would you mind asking you for reasons ?
>
> Best Regards,
> trad-ex
>
>
> >Are you sure?  I think #set can be used for any class.
> >
> >I've proposed to limit this to prohibit reflection by default (such as
> what
> >trad-ex is doing), but we haven't applied the patch yet.
> >
> >WILL
> >
> >On 4/20/06, Barbara Baughman <ba...@utdallas.edu> wrote:
> >>
> >> You cannot use the #set directive to designate just any kind of JAVA
> >> object.  See the User Guide for allowable references.  I believe the
> >> #set directive you have will automatically call the toString method of
> >> the class in order to set the variable to a valid reference, but you
> >> can check by printing the value of the set variable.
> >>
> >> Barbara Baughman
> >> X2157
> >>
> >> On Thu, 20 Apr 2006, trad-ex wrote:
> >>
> >> > Hi All,
> >> >
> >> > I tried to load property resource bundle file, but failed to get an
> >> > instance of FileInputStream.
> >> > My code is below:
> >> >
> >> > #set( $fStreamClass = $util.class.forName("java.io.FileInputStream")
> )
> >> > #set( $iStreamClass = $util.class.forName("java.io.InputStream") )
> >> > #set( $stringClass = $util.class.forName("java.lang.String") )
> >> > #set( $bundleClass
> >> >      = $util.class.forName("java.util.PropertyResourceBundle") )
> >> > #set( $userNameFilePath = $util.getPortalFile($userNameFile) )
> >> > #set( $resStream
> >> >      = $fStreamClass.getConstructor( [$stringClass] )
> >> >        .newInstance( ["$userNameFile"] ) )
> >> > #set( $resBundle
> >> >      = $bundleClass.getConstructor( [$iStreamClass] )
> >> >      .newInstance( [$resStream] ) )
> >> >
> >> > $util object is provided by our application to inject functionality
> into
> >> > velocity context.
> >> > I succeeded to get $fStreamClass & $iStreamClass & $bundleClass
> >> > But I failed to get $resStream & $resBundle.
> >> >
> >> > My questions are:
> >> > 1. Can I load the specific property file in Velocity context ?
> >> > 2. What's wrong with my velocity script ?
> >> >
> >> > Best Regards, and thanks in advance.
> >> > trad-ex
> >> >
> >> > ---------------------------------------------------------------------
> >> > To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> >> > For additional commands, e-mail:
> velocity-user-help@jakarta.apache.org
> >> >
> >> >
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> >> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> >>
> >>
> >
> >
> >--
> >Forio Business Simulations
> >
> >Will Glass-Husain
> >wglass@forio.com
> >www.forio.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>


--
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

Re: Can velocity load a properties file ?

Posted by trad-ex <tr...@inter7.jp>.
Hi Barbara,
Hi Will,

Thanks a lot for your response.
As Will says, #set can make any class objects.
In fact, I often make instances of ArrayList, and use them.
But I think whether there are tools like org.apache.velocity.tools.
struts.MessageTool or not.

I will follow Will's advice to implement methods like $util.
getPropertyResourceBundle( path ) in $util.

BTW, why do you propse to limit to use reflection ?
It's a powerful way to provide such availability into velocity realm, I 
guess. Would you mind asking you for reasons ?

Best Regards,
trad-ex


>Are you sure?  I think #set can be used for any class.
>
>I've proposed to limit this to prohibit reflection by default (such as what
>trad-ex is doing), but we haven't applied the patch yet.
>
>WILL
>
>On 4/20/06, Barbara Baughman <ba...@utdallas.edu> wrote:
>>
>> You cannot use the #set directive to designate just any kind of JAVA
>> object.  See the User Guide for allowable references.  I believe the
>> #set directive you have will automatically call the toString method of
>> the class in order to set the variable to a valid reference, but you
>> can check by printing the value of the set variable.
>>
>> Barbara Baughman
>> X2157
>>
>> On Thu, 20 Apr 2006, trad-ex wrote:
>>
>> > Hi All,
>> >
>> > I tried to load property resource bundle file, but failed to get an
>> > instance of FileInputStream.
>> > My code is below:
>> >
>> > #set( $fStreamClass = $util.class.forName("java.io.FileInputStream") )
>> > #set( $iStreamClass = $util.class.forName("java.io.InputStream") )
>> > #set( $stringClass = $util.class.forName("java.lang.String") )
>> > #set( $bundleClass
>> >      = $util.class.forName("java.util.PropertyResourceBundle") )
>> > #set( $userNameFilePath = $util.getPortalFile($userNameFile) )
>> > #set( $resStream
>> >      = $fStreamClass.getConstructor( [$stringClass] )
>> >        .newInstance( ["$userNameFile"] ) )
>> > #set( $resBundle
>> >      = $bundleClass.getConstructor( [$iStreamClass] )
>> >      .newInstance( [$resStream] ) )
>> >
>> > $util object is provided by our application to inject functionality into
>> > velocity context.
>> > I succeeded to get $fStreamClass & $iStreamClass & $bundleClass
>> > But I failed to get $resStream & $resBundle.
>> >
>> > My questions are:
>> > 1. Can I load the specific property file in Velocity context ?
>> > 2. What's wrong with my velocity script ?
>> >
>> > Best Regards, and thanks in advance.
>> > trad-ex
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>> > For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>> >
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>>
>>
>
>
>--
>Forio Business Simulations
>
>Will Glass-Husain
>wglass@forio.com
>www.forio.com

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


Re: Can velocity load a properties file ?

Posted by Will Glass-Husain <wg...@forio.com>.
Are you sure?  I think #set can be used for any class.

I've proposed to limit this to prohibit reflection by default (such as what
trad-ex is doing), but we haven't applied the patch yet.

WILL

On 4/20/06, Barbara Baughman <ba...@utdallas.edu> wrote:
>
> You cannot use the #set directive to designate just any kind of JAVA
> object.  See the User Guide for allowable references.  I believe the
> #set directive you have will automatically call the toString method of
> the class in order to set the variable to a valid reference, but you
> can check by printing the value of the set variable.
>
> Barbara Baughman
> X2157
>
> On Thu, 20 Apr 2006, trad-ex wrote:
>
> > Hi All,
> >
> > I tried to load property resource bundle file, but failed to get an
> > instance of FileInputStream.
> > My code is below:
> >
> > #set( $fStreamClass = $util.class.forName("java.io.FileInputStream") )
> > #set( $iStreamClass = $util.class.forName("java.io.InputStream") )
> > #set( $stringClass = $util.class.forName("java.lang.String") )
> > #set( $bundleClass
> >      = $util.class.forName("java.util.PropertyResourceBundle") )
> > #set( $userNameFilePath = $util.getPortalFile($userNameFile) )
> > #set( $resStream
> >      = $fStreamClass.getConstructor( [$stringClass] )
> >        .newInstance( ["$userNameFile"] ) )
> > #set( $resBundle
> >      = $bundleClass.getConstructor( [$iStreamClass] )
> >      .newInstance( [$resStream] ) )
> >
> > $util object is provided by our application to inject functionality into
> > velocity context.
> > I succeeded to get $fStreamClass & $iStreamClass & $bundleClass
> > But I failed to get $resStream & $resBundle.
> >
> > My questions are:
> > 1. Can I load the specific property file in Velocity context ?
> > 2. What's wrong with my velocity script ?
> >
> > Best Regards, and thanks in advance.
> > trad-ex
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>


--
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

Re: Can velocity load a properties file ?

Posted by Barbara Baughman <ba...@utdallas.edu>.
You cannot use the #set directive to designate just any kind of JAVA
object.  See the User Guide for allowable references.  I believe the
#set directive you have will automatically call the toString method of
the class in order to set the variable to a valid reference, but you
can check by printing the value of the set variable.

Barbara Baughman
X2157

On Thu, 20 Apr 2006, trad-ex wrote:

> Hi All,
>
> I tried to load property resource bundle file, but failed to get an
> instance of FileInputStream.
> My code is below:
>
> #set( $fStreamClass = $util.class.forName("java.io.FileInputStream") )
> #set( $iStreamClass = $util.class.forName("java.io.InputStream") )
> #set( $stringClass = $util.class.forName("java.lang.String") )
> #set( $bundleClass
>      = $util.class.forName("java.util.PropertyResourceBundle") )
> #set( $userNameFilePath = $util.getPortalFile($userNameFile) )
> #set( $resStream
>      = $fStreamClass.getConstructor( [$stringClass] )
>        .newInstance( ["$userNameFile"] ) )
> #set( $resBundle
>      = $bundleClass.getConstructor( [$iStreamClass] )
>      .newInstance( [$resStream] ) )
>
> $util object is provided by our application to inject functionality into
> velocity context.
> I succeeded to get $fStreamClass & $iStreamClass & $bundleClass
> But I failed to get $resStream & $resBundle.
>
> My questions are:
> 1. Can I load the specific property file in Velocity context ?
> 2. What's wrong with my velocity script ?
>
> Best Regards, and thanks in advance.
> trad-ex
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>

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