On Tue, May 13, 2014 at 9:04 PM, Jeremie BOUSQUET
<jeremie.bousquet(a)gmail.com> wrote:
2014-05-13 19:10 GMT+02:00 Thomas Mortagne
<thomas.mortagne(a)xwiki.com>om>:
On Tue, May 13, 2014 at 6:29 PM, Jeremie
BOUSQUET
<jeremie.bousquet(a)gmail.com> wrote:
2014-05-13 17:53 GMT+02:00 Thomas Mortagne
<thomas.mortagne(a)xwiki.com>om>:
The ExecutionContext returned by
execution.getContext(); comes from a
ThreadLocal which means it's not a singleton associated to the main
wiki but a new ExecutionContext generated for each http request.
That is what I understood, but that means that for my component to
operate
on correct wiki (in correct context), it should
reassign his own
"context"
member for each request - and not do it once at
component initialization
time.
I'm not sure I understand, are you talking about storing the
XWikiContext in your component instance at init ?
Yes in fact it's what I did (the "this.context = " in my sample code). I
realize that I didn't think about it so much when I did it :)
The XWikiContext should never ever be stored, instead you should
always request the current one when you need it. Among other things
the XWikiContext is not thread safe at all and is supposed to always
be used in the Thread it's been created for.
Here I have to apologize because we already had this discussion here, and
you already suggested to use Provider<XWikiContext>, but I completely
forgot about it.
I suggest that the component tutorial could be updated around this context
stuff ?
http://platform.xwiki.org/xwiki/bin/view/DevGuide/WritingComponents#HTheXWi…
I would propose to replace:
"If you still need to access the old XWiki context, then you can get a
reference to it from the execution context, but you should not cast it to
an XWikiContext, which would pull the whole xwiki-core as a dependency, but
to a Map. You won't be able to access all the properties, like the current
user name or the URL factory, but you can access anything placed in the
internal map of the XWikiContext."
By:
"If you still need to access the old XWiki context, then you can get a
reference to it from an injected XWikiContext Provider, but you should not
cast it to an XWikiContext, which would pull the whole xwiki-core as a
dependency, but to a Map.
This is not going to be very easy since you have to know the
XWikiContext class to use that Provider ;)
You won't be able to access all the properties,
like the current user name or the URL factory, but you can access anything
placed in the internal map of the XWikiContext."
And to replace the sample code by:
"
/** Provides access to the XWiki context. Injected by the Component
Manager. */
@Inject
private Provider<XWikiContext> xcontextProvider;
[...]
private void workWithTheContext()
{
Map<Object, Object> xwikiContext = (Map<Object, Object>)
xcontextProvider.get();
// Do something with the XWiki context
}"
>
> By the way you should really use Provider<XWikiContext> component
instead.
I will, thanks. Does it have a different/better behaviour or is just the
preferred way ?
> _______________________________________________
> users mailing list
> users(a)xwiki.org
>
http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________
users mailing list
users(a)xwiki.org
http://lists.xwiki.org/mailman/listinfo/users
--
Thomas Mortagne
_______________________________________________
users mailing list
users(a)xwiki.org
http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________
users mailing list
users(a)xwiki.org
http://lists.xwiki.org/mailman/listinfo/users