You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@myfaces.apache.org by Mario Ivankovits <ma...@ops.co.at> on 2008/04/03 11:33:53 UTC
nasty problem in server side state saving
... with "serialize in state" disabled.
I've create a small test case which shows that the attributes map is
just copied over into the state. Which means that each and every
Component shares exactly the same map. Any change to this map will be
reflected in ALL saved states.
Correct would be to clone the map, but this must not work, at least a
shallow copy of the map should be saved (when "serialize in
state=false") to avoid this problem.
What do you think?
public void testSaveInSessionWithoutSerialize() throws Exception
{
// create a fake viewRoot
UIViewRoot root = new UIViewRoot();
root.getAttributes().put("key", "value");
// simulate server-side-state-saving without serialization
Object state = root.saveState(facesContext);
// restore view
UIViewRoot root1 = new UIViewRoot();
root1.restoreState(facesContext, state);
// restore view .. next request
UIViewRoot root2 = new UIViewRoot();
root2.restoreState(facesContext, state);
// chaange attribute in root1
root1.getAttributes().put("key", "borken");
// and see it changed in root2 too
assertEquals("value", root2.getAttributes().get("key"));
// and see it changed everywhere
assertEquals("value", root.getAttributes().get("key"));
}
Ciao,
Mario
Re: nasty problem in server side state saving
Posted by Mario Ivankovits <ma...@ops.co.at>.
Hi!
> It's because the "wrong" constructor in api's _ComponentAttributesMap
> class, it's assigning the map directly:
So we do agree that this needs to be fixed? Then I'll do so ...
Ciao,
Mario
Re: nasty problem in server side state saving
Posted by Sochor Zdeněk <zd...@ataco.cz>.
Hi,
simon.kitching@chello.at napsal(a):
> Sochor Zdeněk schrieb:
>
>> Hi,
>>
>> Mario Ivankovits napsal(a):
>>
>>> ... with "serialize in state" disabled.
>>>
>>> I've create a small test case which shows that the attributes map is
>>> just copied over into the state. Which means that each and every
>>> Component shares exactly the same map. Any change to this map will be
>>> reflected in ALL saved states.
>>>
>>>
>>>
>> It's because the "wrong" constructor in api's _ComponentAttributesMap
>> class, it's assigning the map directly:
>>
>> 1.1 trunk:
>>
>> _ComponentAttributesMap(UIComponent component, Map attributes)
>> {
>> _component = component;
>> _attributes = attributes;
>> }
>>
>> should be
>> _ComponentAttributesMap(UIComponent component, Map attributes)
>> {
>> _component = component;
>> _attributes = new HashMap();
>> _attributes.putAll(attributes);
>> }
>>
>> the same in 1.2 trunk:
>>
>> _ComponentAttributesMap(UIComponent component, Map<Object, Object>
>> attributes)
>> {
>> _component = component;
>> _attributes = attributes;
>> }
>>
>> should be:
>>
>> _ComponentAttributesMap(UIComponent component, Map<Object, Object>
>> attributes)
>> {
>> _component = component;
>> _attributes = new HashMap<Object, Object>();
>> _attributes.putAll(attributes);
>> }
>>
>
> But as Mario says..
>
>>> Correct would be to clone the map, but this must not work, at least a
>>> shallow copy of the map should be saved (when "serialize in
>>> state=false") to avoid this problem.
>>>
>
> Your solution fixes one more level, but not every possibility. It's
> certainly better, but not complete.
>
> If the attributes map has a mutable object in it (eg a StringBuffer, or
> something more complex) then the problem remains; a change to the object
> state via one UIViewRoot will cause it to change in the other.
>
>
I'm aware of it (but i didn't state that possibility). I was thinking
about user's view - they don't use serializable classes by default :(
Regards,
Zdenek
> Using java.io.serialize stuff forces a real "deep" clone which solves
> this issue. But it does then require that every object put into a
> component's attributes is serializable.
>
> Regards,
> Simon
>
>
>
Re: nasty problem in server side state saving
Posted by "simon.kitching@chello.at" <si...@chello.at>.
Sochor Zdeněk schrieb:
> Hi,
>
> Mario Ivankovits napsal(a):
>> ... with "serialize in state" disabled.
>>
>> I've create a small test case which shows that the attributes map is
>> just copied over into the state. Which means that each and every
>> Component shares exactly the same map. Any change to this map will be
>> reflected in ALL saved states.
>>
>>
> It's because the "wrong" constructor in api's _ComponentAttributesMap
> class, it's assigning the map directly:
>
> 1.1 trunk:
>
> _ComponentAttributesMap(UIComponent component, Map attributes)
> {
> _component = component;
> _attributes = attributes;
> }
>
> should be
> _ComponentAttributesMap(UIComponent component, Map attributes)
> {
> _component = component;
> _attributes = new HashMap();
> _attributes.putAll(attributes);
> }
>
> the same in 1.2 trunk:
>
> _ComponentAttributesMap(UIComponent component, Map<Object, Object>
> attributes)
> {
> _component = component;
> _attributes = attributes;
> }
>
> should be:
>
> _ComponentAttributesMap(UIComponent component, Map<Object, Object>
> attributes)
> {
> _component = component;
> _attributes = new HashMap<Object, Object>();
> _attributes.putAll(attributes);
> }
But as Mario says..
>
>> Correct would be to clone the map, but this must not work, at least a
>> shallow copy of the map should be saved (when "serialize in
>> state=false") to avoid this problem.
Your solution fixes one more level, but not every possibility. It's
certainly better, but not complete.
If the attributes map has a mutable object in it (eg a StringBuffer, or
something more complex) then the problem remains; a change to the object
state via one UIViewRoot will cause it to change in the other.
Using java.io.serialize stuff forces a real "deep" clone which solves
this issue. But it does then require that every object put into a
component's attributes is serializable.
Regards,
Simon
Re: nasty problem in server side state saving
Posted by Sochor Zdeněk <zd...@ataco.cz>.
Hi,
Mario Ivankovits napsal(a):
> ... with "serialize in state" disabled.
>
> I've create a small test case which shows that the attributes map is
> just copied over into the state. Which means that each and every
> Component shares exactly the same map. Any change to this map will be
> reflected in ALL saved states.
>
>
It's because the "wrong" constructor in api's _ComponentAttributesMap
class, it's assigning the map directly:
1.1 trunk:
_ComponentAttributesMap(UIComponent component, Map attributes)
{
_component = component;
_attributes = attributes;
}
should be
_ComponentAttributesMap(UIComponent component, Map attributes)
{
_component = component;
_attributes = new HashMap();
_attributes.putAll(attributes);
}
the same in 1.2 trunk:
_ComponentAttributesMap(UIComponent component, Map<Object, Object> attributes)
{
_component = component;
_attributes = attributes;
}
should be:
_ComponentAttributesMap(UIComponent component, Map<Object, Object> attributes)
{
_component = component;
_attributes = new HashMap<Object, Object>();
_attributes.putAll(attributes);
}
Regards,
Zdenek
> Correct would be to clone the map, but this must not work, at least a
> shallow copy of the map should be saved (when "serialize in
> state=false") to avoid this problem.
>
> What do you think?
>
>
> public void testSaveInSessionWithoutSerialize() throws Exception
> {
> // create a fake viewRoot
> UIViewRoot root = new UIViewRoot();
> root.getAttributes().put("key", "value");
>
> // simulate server-side-state-saving without serialization
> Object state = root.saveState(facesContext);
>
> // restore view
> UIViewRoot root1 = new UIViewRoot();
> root1.restoreState(facesContext, state);
>
> // restore view .. next request
> UIViewRoot root2 = new UIViewRoot();
> root2.restoreState(facesContext, state);
>
> // chaange attribute in root1
> root1.getAttributes().put("key", "borken");
>
> // and see it changed in root2 too
> assertEquals("value", root2.getAttributes().get("key"));
> // and see it changed everywhere
> assertEquals("value", root.getAttributes().get("key"));
> }
>
>
>
> Ciao,
> Mario
>
>
>