I think that's exactly when scopes are applied I think what you want to do
is always return a Provider which in turn wraps your ThreadLocal.
public static final ThreadLocal<?> threadlocal = new
ThreadLocal<?>();
public <T> Provider<T> scope(Key<T> key, final Provider<T> unscoped) {
return new Provider<T> {
public T get() {
if (null != threadlocal.get()) {
return threadlocal.get();
} else {
// ...
}
}
}
}
Incidentally, your ThreadLocal may not be robust enough-- you can't have
more than one Object in your threadlocal scope the way it's set up there.
You probably want a Map keyed on Key.
Check out the Scopes page on the wiki:
http://code.google.com/p/google-guice/wiki/Scopes
On Tue, Sep 30, 2008 at 12: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
-~----------~----~----~----~------~----~------~--~---