You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Jan Algermissen <ja...@nordsc.com> on 2013/11/07 13:39:53 UTC

Howto implement a non-trivial ResourceProvider

Hi,

I am complteley new to Sling and tasked with creating a ResourceProvider implementation. I have been digging thorugh the docs for a while but, honestly, would appreciate some help :-)

My ResourceProvider needs to contact an upstream HTTP server(s), so it needs to use an HTTP client, preferably the Apache HTTP Client. Performance and concurrency requirements are very high - I am not building a pet project. This raises a number of questions for me:

- Where do I best put the HTTP client instance and where do I configure it? It is likely a caching client and I plan to make it multi-threaded, too (use a connection pooling configuration for the Apache client)
  I am looking for the right spot of the Sling runtime here, to wire the client to.

- Or is there maybe a client instance in the Sling runtime that I can just obtain and use from within my ResourceResolver?

- Do I have to write a Factory for my Resolver? And how does a Sling application specify it wants to work with resource from my provider? IOW, How does the bootstrapping work?

- Do I also have to write a ResourceResolver and bootstrap that, too? Or does the runtime provide its own to my ResourcePorvider?

- Will instances of my ResourceProvider be used concurrently, or is a new instance created for every request to sling?

- How do I obtain a reference to my (global) HTTP client inside my ResourceProvider?

Excuse the big bunch of newbie questions, but I feel I need hands-on information directly as opposed digging around - especially the concurrency and bootstrapping aspects I'd really like to not mess up :-)

Jan


Re: Howto implement a non-trivial ResourceProvider

Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi Jan,

On Thu, Nov 7, 2013 at 1:39 PM, Jan Algermissen
<ja...@nordsc.com> wrote:
> ...I am complteley new to Sling and tasked with creating a ResourceProvider implementation...

That sounds cool ;-)

>
> My ResourceProvider needs to contact an upstream HTTP server(s), so it needs to use an HTTP client,
> preferably the Apache HTTP Client...

Sounds good, if needed you'll find numerous examples of using that in
our test code.

To cache the content that you'll download, you could write it to the
JCR repository, depending on its lifetime, total size and frequency of
modification. Then, decide to serve the content from that cache if
fresh enough, or download it if needed.

> - Where do I best put the HTTP client instance and where do I configure it?...

You install the httpclient OSGi, bundle and if it's not OSGi-aware in
terms of config (which IIRC is the case) you can write an OSGi service
that supplies you with configured instances, based on OSGi configs. If
you need just one httpclient instance you can keep it inside your
resource provider bundle.

> ...I am looking for the right spot of the Sling runtime here, to wire the client to....

Not sure what you mean by "wire to". The simplest ResourceProvider
example that we have is the planets resource provider used in our
tests, you should be able to use it as an example. I'd start with a
single bundle that contains the resource provider and HTTP acquisition
logic.

>
> - Or is there maybe a client instance in the Sling runtime that I can just obtain and use from within my ResourceResolver?...

The Sling launchpad contains
org.apache.geronimo.bundles.commons-httpclient but that's httpclient
3.x - a similar bundle is available for 4.x IIRC, and then you can
just instantiate the client as usual.

>
> - Do I have to write a Factory for my Resolver?...

No, see the planets example, it's very simple.

> And how does a Sling application specify it wants to work with resource from my provider?...

 IOW, How does the bootstrapping work?

> ...- Do I also have to write a ResourceResolver and bootstrap that, too?...

No, just the provider.

> ...Will instances of my ResourceProvider be used concurrently, or is a new instance created for every request to sling?...

If you set it up like PlanetsResourceProvider only one instance will
be created, and one PlanetResource instance for each resource.

>
> - How do I obtain a reference to my (global) HTTP client inside my ResourceProvider?...

If you need to share it, make it a service. Or just instantiate and
configure it in the provider.

> Excuse the big bunch of newbie questions...

No worries, it's a good opportunity to see how good our docs and examples are!

-Bertrand

[1] http://svn.apache.org/repos/asf/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/

Re: Howto implement a non-trivial ResourceProvider

Posted by Felix Meschberger <fm...@adobe.com>.
Hi

Apart from looking at existing providers such as the MongoDB, Filesystem, and BundleResource Providers in SVN as well as the Cassandra ResourceProvider mentioned by Robert, here are some generic answers:
Am 07.11.2013 um 13:39 schrieb Jan Algermissen <ja...@nordsc.com>:

> Hi,
> 
> I am complteley new to Sling and tasked with creating a ResourceProvider implementation. I have been digging thorugh the docs for a while but, honestly, would appreciate some help :-)
> 
> My ResourceProvider needs to contact an upstream HTTP server(s), so it needs to use an HTTP client, preferably the Apache HTTP Client. Performance and concurrency requirements are very high - I am not building a pet project. This raises a number of questions for me:
> 
> - Where do I best put the HTTP client instance and where do I configure it? It is likely a caching client and I plan to make it multi-threaded, too (use a connection pooling configuration for the Apache client)
>  I am looking for the right spot of the Sling runtime here, to wire the client to.

IIRC Sling does not come with Apache Http Components. But you can take the Http Components bundles and deploy them in Sling. This should give you access to the API.

It might be a good idea, though if your ResourceProvider would configure its own specialized Http Components configuration instance. I would think that this is possible with the Http Components bundles. Just make sure to cleanup any client and configuration instances you configure when the ResourceProvider is stopped.

> 
> - Or is there maybe a client instance in the Sling runtime that I can just obtain and use from within my ResourceResolver?
> 
> - Do I have to write a Factory for my Resolver? And how does a Sling application specify it wants to work with resource from my provider? IOW, How does the bootstrapping work?

Depends. In the simplest form you don’t need a ResourceProviderFactory. You just write a ResourceProvider and register it as a service. See the FileSystem ResourceProvider as a simple example.

The simple form has two disadvantages, though: It does not support different users and it has to be implemented thread-safely because the same instance is used for all ResourceResolver instances.

To support users (with optional authentication) or non-thread-safe ResourceProvider instances, you have to implement a ResourceProviderFactory. In this case the factory is called for each ResourceResolver that is being created by the ResourceResolverFactory.

> 
> - Do I also have to write a ResourceResolver and bootstrap that, too? Or does the runtime provide its own to my ResourcePorvider?

No, the existing ResourceResolverFactory will pick up your ResourceProvider and hook in into the ResourceResolvers created.

> 
> - Will instances of my ResourceProvider be used concurrently, or is a new instance created for every request to sling?

See above: Plain ResourceProvider services will be used concurrently. ResourceProvider instances created through a ResourceProviderFactory service are unique to each ResourceResolver and thus are not used concurrently.

> 
> - How do I obtain a reference to my (global) HTTP client inside my ResourceProvider?

It is up to your implementation on how you settup the HTTP client.

> 
> Excuse the big bunch of newbie questions, but I feel I need hands-on information directly as opposed digging around - especially the concurrency and bootstrapping aspects I'd really like to not mess up :-)

Hope this helps.

Regards
Felix

> 
> Jan
> 


Re: Howto implement a non-trivial ResourceProvider

Posted by Jan Algermissen <ja...@nordsc.com>.
On 07.11.2013, at 14:44, Jan Algermissen <ja...@nordsc.com> wrote:

> 
> On 07.11.2013, at 13:58, Robert Munteanu <ro...@lmn.ro> wrote:
> 
>> Hi Jan,
>> 
>> On Thu, Nov 7, 2013 at 2:39 PM, Jan Algermissen
>> <ja...@nordsc.com> wrote:
>>> Hi,
>>> 
>>> I am complteley new to Sling and tasked with creating a ResourceProvider implementation. I have been digging thorugh the docs for a while but, honestly, would appreciate some help :-)
>>> 
>>> My ResourceProvider needs to contact an upstream HTTP server(s), so it needs to use an HTTP client, preferably the Apache HTTP Client. Performance and concurrency requirements are very high - I am not building a pet project. This raises a number of questions for me:
>> 
>> Your questions should be addressed by the Cassandra
> 
> Cassandra? Yep, that should do it all. Many thanks! Brilliant.

Actually ... no, that doesn't really help. I am also not at all sure this code is intended to be production quality.

Hence, I am still seeking for ansers to my questions.

Thanks anyway.

Jan

> 
> Jan
> 
> 
>> ResourceProvider
>> implementation which was completed this year for the GSOC.
>> 
>> You can find the code attached at [1].
>> 
>> Robert
>> 
>> [1]: https://issues.apache.org/jira/browse/SLING-2798
>> 
>>> 
>>> - Where do I best put the HTTP client instance and where do I configure it? It is likely a caching client and I plan to make it multi-threaded, too (use a connection pooling configuration for the Apache client)
>>> I am looking for the right spot of the Sling runtime here, to wire the client to.
>>> 
>>> - Or is there maybe a client instance in the Sling runtime that I can just obtain and use from within my ResourceResolver?
>>> 
>>> - Do I have to write a Factory for my Resolver? And how does a Sling application specify it wants to work with resource from my provider? IOW, How does the bootstrapping work?
>>> 
>>> - Do I also have to write a ResourceResolver and bootstrap that, too? Or does the runtime provide its own to my ResourcePorvider?
>>> 
>>> - Will instances of my ResourceProvider be used concurrently, or is a new instance created for every request to sling?
>>> 
>>> - How do I obtain a reference to my (global) HTTP client inside my ResourceProvider?
>>> 
>>> Excuse the big bunch of newbie questions, but I feel I need hands-on information directly as opposed digging around - especially the concurrency and bootstrapping aspects I'd really like to not mess up :-)
>>> 
>>> Jan
>>> 
>> 
>> 
>> 
>> -- 
>> Sent from my (old) computer
> 


Re: Howto implement a non-trivial ResourceProvider

Posted by Jan Algermissen <ja...@nordsc.com>.
On 07.11.2013, at 13:58, Robert Munteanu <ro...@lmn.ro> wrote:

> Hi Jan,
> 
> On Thu, Nov 7, 2013 at 2:39 PM, Jan Algermissen
> <ja...@nordsc.com> wrote:
>> Hi,
>> 
>> I am complteley new to Sling and tasked with creating a ResourceProvider implementation. I have been digging thorugh the docs for a while but, honestly, would appreciate some help :-)
>> 
>> My ResourceProvider needs to contact an upstream HTTP server(s), so it needs to use an HTTP client, preferably the Apache HTTP Client. Performance and concurrency requirements are very high - I am not building a pet project. This raises a number of questions for me:
> 
> Your questions should be addressed by the Cassandra

Cassandra? Yep, that should do it all. Many thanks! Brilliant.

Jan


> ResourceProvider
> implementation which was completed this year for the GSOC.
> 
> You can find the code attached at [1].
> 
> Robert
> 
> [1]: https://issues.apache.org/jira/browse/SLING-2798
> 
>> 
>> - Where do I best put the HTTP client instance and where do I configure it? It is likely a caching client and I plan to make it multi-threaded, too (use a connection pooling configuration for the Apache client)
>>  I am looking for the right spot of the Sling runtime here, to wire the client to.
>> 
>> - Or is there maybe a client instance in the Sling runtime that I can just obtain and use from within my ResourceResolver?
>> 
>> - Do I have to write a Factory for my Resolver? And how does a Sling application specify it wants to work with resource from my provider? IOW, How does the bootstrapping work?
>> 
>> - Do I also have to write a ResourceResolver and bootstrap that, too? Or does the runtime provide its own to my ResourcePorvider?
>> 
>> - Will instances of my ResourceProvider be used concurrently, or is a new instance created for every request to sling?
>> 
>> - How do I obtain a reference to my (global) HTTP client inside my ResourceProvider?
>> 
>> Excuse the big bunch of newbie questions, but I feel I need hands-on information directly as opposed digging around - especially the concurrency and bootstrapping aspects I'd really like to not mess up :-)
>> 
>> Jan
>> 
> 
> 
> 
> -- 
> Sent from my (old) computer


Re: Howto implement a non-trivial ResourceProvider

Posted by Robert Munteanu <ro...@lmn.ro>.
Hi Jan,

On Thu, Nov 7, 2013 at 2:39 PM, Jan Algermissen
<ja...@nordsc.com> wrote:
> Hi,
>
> I am complteley new to Sling and tasked with creating a ResourceProvider implementation. I have been digging thorugh the docs for a while but, honestly, would appreciate some help :-)
>
> My ResourceProvider needs to contact an upstream HTTP server(s), so it needs to use an HTTP client, preferably the Apache HTTP Client. Performance and concurrency requirements are very high - I am not building a pet project. This raises a number of questions for me:

Your questions should be addressed by the Cassandra ResourceProvider
implementation which was completed this year for the GSOC.

You can find the code attached at [1].

Robert

[1]: https://issues.apache.org/jira/browse/SLING-2798

>
> - Where do I best put the HTTP client instance and where do I configure it? It is likely a caching client and I plan to make it multi-threaded, too (use a connection pooling configuration for the Apache client)
>   I am looking for the right spot of the Sling runtime here, to wire the client to.
>
> - Or is there maybe a client instance in the Sling runtime that I can just obtain and use from within my ResourceResolver?
>
> - Do I have to write a Factory for my Resolver? And how does a Sling application specify it wants to work with resource from my provider? IOW, How does the bootstrapping work?
>
> - Do I also have to write a ResourceResolver and bootstrap that, too? Or does the runtime provide its own to my ResourcePorvider?
>
> - Will instances of my ResourceProvider be used concurrently, or is a new instance created for every request to sling?
>
> - How do I obtain a reference to my (global) HTTP client inside my ResourceProvider?
>
> Excuse the big bunch of newbie questions, but I feel I need hands-on information directly as opposed digging around - especially the concurrency and bootstrapping aspects I'd really like to not mess up :-)
>
> Jan
>



-- 
Sent from my (old) computer