[xwiki-users] Components, context and subwikis
Hello, I have some java components (@Singleton) that depend on old core. It is bad, and I'm trying to move away from it, but I'll keep at least one component like this - my own bridge. I have this in initialize() of these components: ExecutionContext context = execution.getContext(); this.context = (XWikiContext) context.getProperty("xwikicontext"); this.xwiki = this.context.getWiki(); I realize that this worked well in the context of a mono-wiki, but in a subwiki I wonder if it causes issues. Here, ExecutionContext relates to the request (user action) at initialize() time, so I suppose it's a sort of default execution context, related to main wiki, right ? So for example when xwiki.getDocument() is called, it looks after a document in the main wiki - not in the (sub)wiki from which the component method was called, right ? Should I just call initialize() at the beginning of every method that needs a correct context, in order to get a "fresh" execution context related to correct wiki ? Or should I pass the context from the script service, adding it as parameter to all needed methods in my component ? Thanks, Jeremie
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. By the way you should really use Provider<XWikiContext> component instead.
2014-05-13 17:53 GMT+02:00 Thomas Mortagne <[email protected]>:
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.
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 [email protected] http://lists.xwiki.org/mailman/listinfo/users
On Tue, May 13, 2014 at 6:29 PM, Jeremie BOUSQUET <[email protected]> wrote:
2014-05-13 17:53 GMT+02:00 Thomas Mortagne <[email protected]>:
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 ? 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.
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 [email protected] http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne
2014-05-13 19:10 GMT+02:00 Thomas Mortagne <[email protected]>:
On Tue, May 13, 2014 at 6:29 PM, Jeremie BOUSQUET <[email protected]> wrote:
2014-05-13 17:53 GMT+02:00 Thomas Mortagne <[email protected]>:
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#HTheXWik... 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. 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 [email protected] http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
On Tue, May 13, 2014 at 9:04 PM, Jeremie BOUSQUET <[email protected]> wrote:
2014-05-13 19:10 GMT+02:00 Thomas Mortagne <[email protected]>:
On Tue, May 13, 2014 at 6:29 PM, Jeremie BOUSQUET <[email protected]> wrote:
2014-05-13 17:53 GMT+02:00 Thomas Mortagne <[email protected]>:
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#HTheXWik...
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 [email protected] http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne
participants (2)
-
Jeremie BOUSQUET -
Thomas Mortagne