You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Anthony Eden <me...@anthonyeden.com> on 2001/06/22 16:22:47 UTC

Extend VelocityContext

I would like to extend the VelocityContext class and override the put (and
remove) methods to provide locking behavior so that some reserved keys
cannot be put into the context.  My question is how do I notify callers of
the methods that the key and value pair cannot be placed into the context?
One possibility is to use the returned Object from the put() method as the
success identifier by returning the inserted value if the put succeeds or
null if it fails, but this is not how the Context contract is really defined
(it is supposed to return the previous value or null at least that is my
impression.)  Another possibility is to fail silently, but that doesn't seem
like a very nice thing to do.  :-)

Any thoughts?

Sincerely,
Anthony Eden


RE: Extend VelocityContext

Posted by Anthony Eden <ae...@signaturedomains.com>.
Do you think that inheritance is less "secure" than wrapping the object,
especially if the inheriting class is marked final?  If I wrap the context,
should the wrapper be final?

I am curious as to what you think are the benefits of wrapping in this case?
There is no requirement that I have to extend it since the wrapper can just
implement the Context interface, but I am curious as to what the benefits
are.  One benefit of extending is that I only need to implement the put()
and remove() methods, but since the Context interface is so simple that
benefit is really minimal.

Sincerely,
Anthony Eden

> -----Original Message-----
> From: Nick Bauman [mailto:nick@cortexity.com]
> Sent: Friday, June 22, 2001 2:09 PM
> To: velocity-user@jakarta.apache.org
> Subject: Re: Extend VelocityContext
>
>
> Something sounds strange about your design.
>
> If you are dynamically putting arbitrary objects into your context at
> runtime and checking them, a better choice would be to put the logic for
> checking whether the instance is "allowed" by simply wrapping the call to
> put(), instead of extending VelocityContext. Point is inheritance is not
> more "secure".
>
> If you feel you _must_ extend it, you should be using a Visitor instead of
> straight-up inheritance anyway. This is how to get
> inheritance-like behavior
> for a class marked final, too. Comes with a lot of baggage, though.
>
> On Fri, Jun 22, 2001 at 10:22:47AM -0400, Anthony Eden wrote:
>
> >> I would like to extend the VelocityContext class and override the put
> >> (and remove) methods to provide locking behavior so that some reserved
> >> keys cannot be put into the context.  My question is how do I notify
> >> callers of the methods that the key and value pair cannot be placed
> >> into the context? One possibility is to use the returned Object from
> >> the put() method as the success identifier by returning the inserted
> >> value if the put succeeds or null if it fails, but this is not how the
> >> Context contract is really defined (it is supposed to return the
> >> previous value or null at least that is my impression.)  Another
> >> possibility is to fail silently, but that doesn't seem like a very
> >> nice thing to do.  :-)
> >>
> >> Any thoughts?
> >>
> >> Sincerely,
> >> Anthony Eden
> >
> > How about throwing an IllegalArgumentException or a custom exception?
> >
> > Daniel
> >
> > --
> > Daniel Rogers             | Email: iidan@ccwf.cc.utexas.edu
> > Web Development Person    | Please, no spam. I have some from last
> > year.
>
>
> --
> Nick Bauman
> Software Engineer
> 3600 Dupont
> Minneapolis, MN
> 55412
> Mobile Phone: (612) 810-7406
> Home Phone: (612) 522-0165
>


Re: Extend VelocityContext

Posted by Nick Bauman <ni...@cortexity.com>.
Something sounds strange about your design.

If you are dynamically putting arbitrary objects into your context at
runtime and checking them, a better choice would be to put the logic for
checking whether the instance is "allowed" by simply wrapping the call to
put(), instead of extending VelocityContext. Point is inheritance is not
more "secure".

If you feel you _must_ extend it, you should be using a Visitor instead of
straight-up inheritance anyway. This is how to get inheritance-like behavior
for a class marked final, too. Comes with a lot of baggage, though.

On Fri, Jun 22, 2001 at 10:22:47AM -0400, Anthony Eden wrote:

>> I would like to extend the VelocityContext class and override the put
>> (and remove) methods to provide locking behavior so that some reserved
>> keys cannot be put into the context.  My question is how do I notify
>> callers of the methods that the key and value pair cannot be placed
>> into the context? One possibility is to use the returned Object from
>> the put() method as the success identifier by returning the inserted
>> value if the put succeeds or null if it fails, but this is not how the
>> Context contract is really defined (it is supposed to return the
>> previous value or null at least that is my impression.)  Another
>> possibility is to fail silently, but that doesn't seem like a very
>> nice thing to do.  :-)
>> 
>> Any thoughts?
>> 
>> Sincerely,
>> Anthony Eden
> 
> How about throwing an IllegalArgumentException or a custom exception?
> 
> Daniel
> 
> --
> Daniel Rogers             | Email: iidan@ccwf.cc.utexas.edu
> Web Development Person    | Please, no spam. I have some from last
> year.


-- 
Nick Bauman
Software Engineer
3600 Dupont
Minneapolis, MN
55412
Mobile Phone: (612) 810-7406
Home Phone: (612) 522-0165


RE: Extend VelocityContext

Posted by Anthony Eden <ae...@signaturedomains.com>.
I suppose that the key here is that the exception will have to be a
RuntimeException which makes sense since the context is only built at
runtime.

Thanks for the suggestion.

Sincerely,
Anthony Eden

-----Original Message-----
From: Daniel Rogers [mailto:iidan@ccwf.cc.utexas.edu]
Sent: Friday, June 22, 2001 2:16 PM
To: velocity
Subject: Re: Extend VelocityContext


On Fri, Jun 22, 2001 at 10:22:47AM -0400, Anthony Eden wrote:
> I would like to extend the VelocityContext class and override the put (and
> remove) methods to provide locking behavior so that some reserved keys
> cannot be put into the context.  My question is how do I notify callers of
> the methods that the key and value pair cannot be placed into the context?
> One possibility is to use the returned Object from the put() method as the
> success identifier by returning the inserted value if the put succeeds or
> null if it fails, but this is not how the Context contract is really
defined
> (it is supposed to return the previous value or null at least that is my
> impression.)  Another possibility is to fail silently, but that doesn't
seem
> like a very nice thing to do.  :-)
>
> Any thoughts?
>
> Sincerely,
> Anthony Eden

How about throwing an IllegalArgumentException or a custom exception?

Daniel

--
Daniel Rogers             | Email: iidan@ccwf.cc.utexas.edu
Web Development Person    | Please, no spam. I have some from last year.


Re: Extend VelocityContext

Posted by Daniel Rogers <ii...@ccwf.cc.utexas.edu>.
On Fri, Jun 22, 2001 at 10:22:47AM -0400, Anthony Eden wrote:
> I would like to extend the VelocityContext class and override the put (and
> remove) methods to provide locking behavior so that some reserved keys
> cannot be put into the context.  My question is how do I notify callers of
> the methods that the key and value pair cannot be placed into the context?
> One possibility is to use the returned Object from the put() method as the
> success identifier by returning the inserted value if the put succeeds or
> null if it fails, but this is not how the Context contract is really defined
> (it is supposed to return the previous value or null at least that is my
> impression.)  Another possibility is to fail silently, but that doesn't seem
> like a very nice thing to do.  :-)
> 
> Any thoughts?
> 
> Sincerely,
> Anthony Eden

How about throwing an IllegalArgumentException or a custom exception?

Daniel

--
Daniel Rogers             | Email: iidan@ccwf.cc.utexas.edu
Web Development Person    | Please, no spam. I have some from last year.


Re: Extend VelocityContext

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Anthony Eden wrote:
> 
> I would like to extend the VelocityContext class and override the put (and
> remove) methods to provide locking behavior so that some reserved keys
> cannot be put into the context.  My question is how do I notify callers of
> the methods that the key and value pair cannot be placed into the context?
> One possibility is to use the returned Object from the put() method as the
> success identifier by returning the inserted value if the put succeeds or
> null if it fails, but this is not how the Context contract is really defined
> (it is supposed to return the previous value or null at least that is my
> impression.)  Another possibility is to fail silently, but that doesn't seem
> like a very nice thing to do.  :-)
> 
> Any thoughts?

It should be very straightforward - the whole context system was
designed for this.

You can either extend VelocityContext, or just extend AbstractContext. 
I would probably try to extend AbstractContex, and thow an exception
when someone tried to tuck in something illegal.

Of course, that would not be handled well in the template - rendering
will stop - since a #set() is effectively a put(), and there is no
EventHandler for that.

My guess is that you would then catch your exceptio during the merge(),
so you would know what happened.

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!