The simplest way to do that is with a ThreadLocal:
http://download.oracle.com/javase/6/docs/api/java/lang/ThreadLocal.html

So you need some class that is available to all your iPojo classes.  I would
put this in a separate "API" bundle, the package of which is imported by
your other bundles.

I like to keep the ThreadLocal API from being exposed by the context class
itself by doing something like this:

public class MyContext {

    private static ThreadLocal<MyContext> threadLocal = new
ThreadLocal<MyContext>() {

        protected MyContext initialValue() {
            return new MyContext();
        }

    };

     public static MyContext getContext() {
            return threadLocal.get();
     }

}

Then from any of your classes that have imported the above you can just
call:

MyContext ctx = MyContext.getContext() ;

When the thread dies the instance of MyContext is then available for garbage
collection.

There may be a more specific OSGi or iPojo friendly way of doing it (though
I've used this method in the past with no problems).  I'm sure there are
also other discussions on the use of ThreadLocal in general too, but I hope
that helps.

Cheers,
Chris



On 25 November 2010 09:41, Bigard Olivier <[email protected]> wrote:

> Hi,
>
>
>
> I'm trying to find a way to implement following need with Felix and
> iPojo.
>
>
>
> Imagine you have an iPojo instance of Bundle A calling an OSGi Service
> provided by an iPojo instance of Bundle B.
>
> Imagine this Bundle B iPojo instance is calling an OSGi Service provided
> by an iPojo instance of Bundle C.
>
>
>
> class A {
>
> @Requires
>
> B b;
>
>
>
> public doSomething() {
>
>                                ...
>
>                                b.doSomethingInB();
>
> }
>
> }
>
>
>
> class B {
>
> @Requires
>
> C c;
>
>
>
> public doSomethingInB() {
>
>                                ...
>
>                                c.doSomethingInC();
>
> }
>
> }
>
>
>
> class C {
>
> public doSomethingInC() {
>
>                                ...
>
> }
>
> }
>
>
>
> What I want is to be able to send some "contextual" information from
> method "doSomething()" in class A to method "doSomethingInC()" in class
> C without being intrusive in the method signature.
>
> For example, this contextual information could be created by class A
> with the username.
>
> This context could be enriched by class B by adding the user email for
> example.
>
> And finally the class C could get the username and user email from the
> contextual information without adding this data as parameter of its
> method.
>
>
>
> Do you have an idea how I can do that with iPojo?
>
>
>
> Thanks
>
> Olivier
>
>

Reply via email to