You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by Dominique Pfister <dp...@adobe.com> on 2014/05/28 11:25:05 UTC

Context ClassLoader in Servlet.init()

Hi,

I’m creating a Sling Servlet that is derived from an abstract base class:
in the base class’s init() method, some factory class is loaded via
Thread.getCurrentThread().getContextClassLoader(). I was surprised that the
loader returned is the standard AppLoader, while I would have expected the
bundle’s class loader. Is this by design?

Thanks
Dominique

Re: Context ClassLoader in Servlet.init()

Posted by Dominique Pfister <dp...@adobe.com>.
Hi Bertrand,

I see! Thank you for pointing to the workaround: that’s exactly what I’m
doing now as I’m unable to change the base class which resides in a library.

Cheers
Dominique


On Wed, May 28, 2014 at 11:44 AM, Bertrand Delacretaz <
bdelacretaz@apache.org> wrote:

> Hi Dominique,
>
> On Wed, May 28, 2014 at 11:25 AM, Dominique Pfister <dp...@adobe.com>
> wrote:
> > ...I’m creating a Sling Servlet that is derived from an abstract base
> class:
> > in the base class’s init() method, some factory class is loaded via
> > Thread.getCurrentThread().getContextClassLoader(). I was surprised that
> the
> > loader returned is the standard AppLoader, while I would have expected
> the
> > bundle’s class loader...
>
> In general you shouldn't use the thread context class loader (TCCL) in
> OSGi components. C.getClassLoader() returns the bundle class loader of
> class C that's provided by a bundle.
>
> If you're stuck with existing code that does use the TCCL, you'll need
> to set it yourself as done for example in [1]
>
> -Bertrand
>
> [1]
> https://svn.apache.org/repos/asf/sling/trunk/bundles/jcr/base/src/main/java/org/apache/sling/jcr/base/util/RepositoryAccessor.java
>

Re: Context ClassLoader in Servlet.init()

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

On Wed, May 28, 2014 at 11:25 AM, Dominique Pfister <dp...@adobe.com> wrote:
> ...I’m creating a Sling Servlet that is derived from an abstract base class:
> in the base class’s init() method, some factory class is loaded via
> Thread.getCurrentThread().getContextClassLoader(). I was surprised that the
> loader returned is the standard AppLoader, while I would have expected the
> bundle’s class loader...

In general you shouldn't use the thread context class loader (TCCL) in
OSGi components. C.getClassLoader() returns the bundle class loader of
class C that's provided by a bundle.

If you're stuck with existing code that does use the TCCL, you'll need
to set it yourself as done for example in [1]

-Bertrand

[1] https://svn.apache.org/repos/asf/sling/trunk/bundles/jcr/base/src/main/java/org/apache/sling/jcr/base/util/RepositoryAccessor.java