Thanks Logan, Alen - appreciate the explanations.
Alen's solution is closer to what I was looking for, but I do see the
point Logan made in his suggestion of using a Map keyed with Key. I
can now apply a @ThreadLocalScoped annotation to any Entity... \o/
Cheers,
Rahul
PS: Google forums should provide some sorta code formatting :-)
On Oct 1, 3:36 am, Alen Vrečko <[EMAIL PROTECTED]> wrote:
> I might not see the point but why do we need to store the providers,
> map?
>
> Somebody correct me if I am mistaken in any case will learn something
> new.
>
> As I see it, scope is basically just one provider that wraps around
> original "no scope" provider.
>
> No_Scope_Provider <-> Scoped_Provider <-> new instance
>
> So when you call bind(...).in(MY_SCOPE), Guice will call the scope
> method (just once) on your Scope and use the returned provider instead
> of the default (new instance each time).
>
> In your case, I think you essentially return the get(){ return
> unscoped.get())} meaning ThreadLocalScope is actually NO_SCOPE.
>
> Haven't really worked with ThreadLocal but how about this:
>
> public <T> Provider<T> scope(Key<T> key, final Provider<T> unscoped)
> {
>
> return new Provider<T>() {
> private final ThreadLocal<T> threadData = new
> ThreadLocal<T>();
>
> public T get() {
>
> T instance = threadData.get();
>
> if (instance == null) {
> instance = unscoped.get();
> threadData.set(instance);
> }
>
> return instance;
> }
> };
>
> }
>
> Cheers,
> Alen
>
> On Sep 30, 6:30 am, Rahul <[EMAIL PROTECTED]> wrote:
>
> > Something is amiss with the ThreadLocal scope implementation below.
> > Can someone please explain what am I missing? I am seeing the Scope
> > applied only the first time I request the instance of a Entity
> > decorated entity.
>
> > Thanks in advance,
> > Rahul
>
> > -------------- code snippet below -------------
>
> > public class ThreadLocalScope implements Scope {
>
> > public static final ThreadLocal<Provider<?>> threadlocal = new
> > ThreadLocal<Provider<?>>();
>
> > @Override
> > public <T> Provider<T> scope(Key<T> key, final Provider<T> unscoped)
> > {
> > System.out.println("Scoping " + key.toString());
> > Provider<T> retVal = null;
> > if (null != threadlocal.get()) {
> > retVal = (Provider<T>) threadlocal.get();
> > } else {
> > retVal = new Provider<T>() {
>
> > @Override
> > public T get() {
> > return unscoped.get();
> > }
>
> > };
> > threadlocal.set(retVal);
> > }
> > return retVal;
> > }
>
> > }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---