You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Davide <ge...@gmail.com> on 2012/05/14 12:48:44 UTC

currentResource in OSGi

Hi All,

probably a stupid and easy question.

Normally to retrieve the currentResource in OSGi bundles I pass in the
SlingHttpServletRequest.

Is there a way to have it using the @Reference? Maybe with the
ResourceResolver?

Thank you
Regards
Davide


Re: currentResource in OSGi

Posted by Sarwar Bhuiyan <sa...@gmail.com>.
I agree with Betrand about just a few more characters to type, either the
method takes in the resource or request.
ThreadLocals would be cool but I'm not sure of the impacts and
complications arising in OSGi.


Sarwar

On Mon, May 14, 2012 at 3:07 PM, Bertrand Delacretaz <bdelacretaz@apache.org
> wrote:

> On Mon, May 14, 2012 at 2:55 PM, Chetan Mehrotra
> <ch...@gmail.com> wrote:
> > ...It would be helpful if we can expose the current
> SlingHttpServletRequest or
> > ResourceResolver via a threadlocal...
>
> aarghh ;-)
>
> > ..This would simplify code where we need
> > pass the request as method parameters . This can be done via a simple
> > FIlter which publishes the request into a thread local and have a new API
> > class which makes this request accessible....
>
> This would work, but I would be against including such a filter in the
> Sling codebase.
>
> Passing the request to a method clearly indicates that you expect that
> method to deal with it. It's a few more characters to type, but it
> makes things clear.
>
> IMHO, it's too easy to create a mess with threadlocals, which are just
> another kind of global variables.
>
> -Bertrand
>

Re: currentResource in OSGi

Posted by Sarwar Bhuiyan <sa...@gmail.com>.
Those are good examples.  So if it's made robust and part of sling, maybe.
 But I wouldn't suggest it to client side application developers building
website components.  Keep it simple and "stupid".

Sarwar

On Mon, May 14, 2012 at 5:33 PM, Chetan Mehrotra
<ch...@gmail.com>wrote:

> > aarghh ;-)
> I knew it is coming .... :)
>
> I understand that threadlocals are dangerous but they do have there uses in
> infrastructure components like in Security context , Transaction context
> propagation.
>
> Couple of examples where I feel they help
>
> 1. I had to implement a Client SDK for a remote server in Sling. The
> usecase involved flow like Http Request -> Sling Servlet -> OSGi Service
> Proxy -> Outpbound RPC call over Web Service. In such a scenario I had pass
> in the invoking user identity. For this I need to access the
> ResourceResolver associated with current thread and extract the user
> identity from the associated JCR session
>
> 2. Using RequestprogressTracker as poor man profiler - I like the
> RequestProgressTracker feature of Sling and would like to use it to capture
> time taken in some of the lower layers of my system. For example in
> scenario mentioned earlier I would like to keep a log of the time taken for
> the remote RPC call however that layer does not have access to request
> instance
>
> Probably the usecases I have dealt with might not be generic but having the
> current request's ResourceResolver available via ThreadLocal helps in write
> some infrastructure components!!
>
> Chetan Mehrotra
>
>
> On Mon, May 14, 2012 at 7:37 PM, Bertrand Delacretaz <
> bdelacretaz@apache.org
> > wrote:
>
> > On Mon, May 14, 2012 at 2:55 PM, Chetan Mehrotra
> > <ch...@gmail.com> wrote:
> > > ...It would be helpful if we can expose the current
> > SlingHttpServletRequest or
> > > ResourceResolver via a threadlocal...
> >
> > aarghh ;-)
> >
> > > ..This would simplify code where we need
> > > pass the request as method parameters . This can be done via a simple
> > > FIlter which publishes the request into a thread local and have a new
> API
> > > class which makes this request accessible....
> >
> > This would work, but I would be against including such a filter in the
> > Sling codebase.
> >
> > Passing the request to a method clearly indicates that you expect that
> > method to deal with it. It's a few more characters to type, but it
> > makes things clear.
> >
> > IMHO, it's too easy to create a mess with threadlocals, which are just
> > another kind of global variables.
> >
> > -Bertrand
> >
>

Re: currentResource in OSGi

Posted by Chetan Mehrotra <ch...@gmail.com>.
> aarghh ;-)
I knew it is coming .... :)

I understand that threadlocals are dangerous but they do have there uses in
infrastructure components like in Security context , Transaction context
propagation.

Couple of examples where I feel they help

1. I had to implement a Client SDK for a remote server in Sling. The
usecase involved flow like Http Request -> Sling Servlet -> OSGi Service
Proxy -> Outpbound RPC call over Web Service. In such a scenario I had pass
in the invoking user identity. For this I need to access the
ResourceResolver associated with current thread and extract the user
identity from the associated JCR session

2. Using RequestprogressTracker as poor man profiler - I like the
RequestProgressTracker feature of Sling and would like to use it to capture
time taken in some of the lower layers of my system. For example in
scenario mentioned earlier I would like to keep a log of the time taken for
the remote RPC call however that layer does not have access to request
instance

Probably the usecases I have dealt with might not be generic but having the
current request's ResourceResolver available via ThreadLocal helps in write
some infrastructure components!!

Chetan Mehrotra


On Mon, May 14, 2012 at 7:37 PM, Bertrand Delacretaz <bdelacretaz@apache.org
> wrote:

> On Mon, May 14, 2012 at 2:55 PM, Chetan Mehrotra
> <ch...@gmail.com> wrote:
> > ...It would be helpful if we can expose the current
> SlingHttpServletRequest or
> > ResourceResolver via a threadlocal...
>
> aarghh ;-)
>
> > ..This would simplify code where we need
> > pass the request as method parameters . This can be done via a simple
> > FIlter which publishes the request into a thread local and have a new API
> > class which makes this request accessible....
>
> This would work, but I would be against including such a filter in the
> Sling codebase.
>
> Passing the request to a method clearly indicates that you expect that
> method to deal with it. It's a few more characters to type, but it
> makes things clear.
>
> IMHO, it's too easy to create a mess with threadlocals, which are just
> another kind of global variables.
>
> -Bertrand
>

Re: currentResource in OSGi

Posted by Bertrand Delacretaz <bd...@apache.org>.
On Mon, May 14, 2012 at 2:55 PM, Chetan Mehrotra
<ch...@gmail.com> wrote:
> ...It would be helpful if we can expose the current SlingHttpServletRequest or
> ResourceResolver via a threadlocal...

aarghh ;-)

> ..This would simplify code where we need
> pass the request as method parameters . This can be done via a simple
> FIlter which publishes the request into a thread local and have a new API
> class which makes this request accessible....

This would work, but I would be against including such a filter in the
Sling codebase.

Passing the request to a method clearly indicates that you expect that
method to deal with it. It's a few more characters to type, but it
makes things clear.

IMHO, it's too easy to create a mess with threadlocals, which are just
another kind of global variables.

-Bertrand

Re: currentResource in OSGi

Posted by Chetan Mehrotra <ch...@gmail.com>.
It would be helpful if we can expose the current SlingHttpServletRequest or
ResourceResolver via a threadlocal. This would simplify code where we need
pass the request as method parameters . This can be done via a simple
FIlter which publishes the request into a thread local and have a new API
class which makes this request accessible.

Thoughts?

Chetan Mehrotra


On Mon, May 14, 2012 at 4:56 PM, Sarwar Bhuiyan <sa...@gmail.com>wrote:

> the resource will be different for every instance so annotation is not a
> good candidate to pass this anyway.  annotations would be used to wire up
> services when the services start on osgi bundle installation or update.
>
> Sarwar
>
> On Mon, May 14, 2012 at 12:00 PM, Davide <ge...@gmail.com> wrote:
>
> > On 14/05/2012 11:55, Sarwar Bhuiyan wrote:
> > > it's just called resource, not currentResource in JSPs.  In servlets,
> you
> > > get the resource from request.getResource or
> > > resourceResolver.getResource(path).  Not sure what you're asking in
> terms
> > > of @Reference.  Are you talking about when you are not in a servlet?
>  If
> > > you're using some java service or helper class, why not pass the
> resource
> > > object in the parameter?
> >
> > Yes, I'm in a java class. Normally I pass in the SlingHttpServletRequest
> > and it works. I was just wondering about any annotation we could use.
> >
> > thanks
> > Davide
> >
> >
>

Re: currentResource in OSGi

Posted by Sarwar Bhuiyan <sa...@gmail.com>.
the resource will be different for every instance so annotation is not a
good candidate to pass this anyway.  annotations would be used to wire up
services when the services start on osgi bundle installation or update.

Sarwar

On Mon, May 14, 2012 at 12:00 PM, Davide <ge...@gmail.com> wrote:

> On 14/05/2012 11:55, Sarwar Bhuiyan wrote:
> > it's just called resource, not currentResource in JSPs.  In servlets, you
> > get the resource from request.getResource or
> > resourceResolver.getResource(path).  Not sure what you're asking in terms
> > of @Reference.  Are you talking about when you are not in a servlet?  If
> > you're using some java service or helper class, why not pass the resource
> > object in the parameter?
>
> Yes, I'm in a java class. Normally I pass in the SlingHttpServletRequest
> and it works. I was just wondering about any annotation we could use.
>
> thanks
> Davide
>
>

Re: currentResource in OSGi

Posted by Davide <ge...@gmail.com>.
On 14/05/2012 11:55, Sarwar Bhuiyan wrote:
> it's just called resource, not currentResource in JSPs.  In servlets, you
> get the resource from request.getResource or
> resourceResolver.getResource(path).  Not sure what you're asking in terms
> of @Reference.  Are you talking about when you are not in a servlet?  If
> you're using some java service or helper class, why not pass the resource
> object in the parameter?

Yes, I'm in a java class. Normally I pass in the SlingHttpServletRequest
and it works. I was just wondering about any annotation we could use.

thanks
Davide


Re: currentResource in OSGi

Posted by Sarwar Bhuiyan <sa...@gmail.com>.
it's just called resource, not currentResource in JSPs.  In servlets, you
get the resource from request.getResource or
resourceResolver.getResource(path).  Not sure what you're asking in terms
of @Reference.  Are you talking about when you are not in a servlet?  If
you're using some java service or helper class, why not pass the resource
object in the parameter?


Sarwar

On Mon, May 14, 2012 at 11:48 AM, Davide <ge...@gmail.com> wrote:

> Hi All,
>
> probably a stupid and easy question.
>
> Normally to retrieve the currentResource in OSGi bundles I pass in the
> SlingHttpServletRequest.
>
> Is there a way to have it using the @Reference? Maybe with the
> ResourceResolver?
>
> Thank you
> Regards
> Davide
>
>