;)

package de.kamalook.guice;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;

import com.google.inject.AbstractModule;
import com.google.inject.Binder;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.Scope;
import com.google.inject.TypeLiteral;
import com.google.inject.binder.AnnotatedBindingBuilder;
import com.google.inject.binder.LinkedBindingBuilder;
import com.google.inject.binder.ScopedBindingBuilder;
import com.google.inject.matcher.Matcher;

/**
 * An extension of AbstractModule that provides support for member
injection of instances constructed at bind-time, in
 * particular [EMAIL PROTECTED] MethodInterceptor}s and [EMAIL PROTECTED] 
Scope}s.
 */
public abstract class ExtendedModule extends AbstractModule
{
        private class WrappedAnnotatedBindingBuilder<T> implements
AnnotatedBindingBuilder<T>
        {
                private LinkedBindingBuilder<T> wrapped;

                public WrappedAnnotatedBindingBuilder(LinkedBindingBuilder<T>
original)
                {
                        wrapped = original;
                }

                public LinkedBindingBuilder<T> annotatedWith(Annotation 
annotation)
                {
                        ((AnnotatedBindingBuilder<T>) 
wrapped).annotatedWith(annotation);
                        return this;
                }

                public LinkedBindingBuilder<T> annotatedWith(Class<? extends
Annotation> annotationType)
                {
                        ((AnnotatedBindingBuilder<T>)
wrapped).annotatedWith(annotationType);
                        return this;
                }

                public void asEagerSingleton()
                {
                        wrapped.asEagerSingleton();
                }

                public void in(Class<? extends Annotation> scopeAnnotation)
                {
                        wrapped.in(scopeAnnotation);
                }

                public void in(Scope scope)
                {
                        requestInjection(scope);
                        wrapped.in(scope);
                }

                public ScopedBindingBuilder to(Class<? extends T> 
implementation)
                {
                        wrapped.to(implementation);
                        return this;
                }

                public ScopedBindingBuilder to(Key<? extends T> targetKey)
                {
                        wrapped.to(targetKey);
                        return this;
                }

                public ScopedBindingBuilder to(TypeLiteral<? extends T>
implementation)
                {
                        wrapped.to(implementation);
                        return this;
                }

                public void toInstance(T instance)
                {
                        wrapped.toInstance(instance);
                }

                public ScopedBindingBuilder toProvider(Class<? extends 
Provider<?
extends T>> providerType)
                {
                        wrapped.toProvider(providerType);
                        return this;
                }

                public ScopedBindingBuilder toProvider(Key<? extends Provider<?
extends T>> providerKey)
                {
                        wrapped.toProvider(providerKey);
                        return this;
                }

                public ScopedBindingBuilder toProvider(Provider<? extends T>
provider)
                {
                        wrapped.toProvider(provider);
                        return this;
                }
        }

        private static <T> Object[] copyArray(T[] array)
        {
                Object[] objects = new Object[array.length];
                for (int i = 0; i < array.length; i++)
                {
                        objects[i] = array[i];
                }
                return objects;
        }

        /**
         * [EMAIL PROTECTED]
         */
        @Override
        protected <T> AnnotatedBindingBuilder<T> bind(Class<T> clazz)
        {
                return new 
WrappedAnnotatedBindingBuilder<T>(binder().bind(clazz));
        }

        /**
         * [EMAIL PROTECTED]
         */
        @Override
        protected <T> LinkedBindingBuilder<T> bind(Key<T> key)
        {
                return new 
WrappedAnnotatedBindingBuilder<T>(binder().bind(key));
        }

        /**
         * [EMAIL PROTECTED]
         */
        @Override
        protected <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T>
typeLiteral)
        {
                return new
WrappedAnnotatedBindingBuilder<T>(binder().bind(typeLiteral));
        }

        @Override
        protected Binder binder()
        {
                return super.binder().skipSources(ExtendedModule.class);
        }

        /**
         * Overridden version of bindInterceptor that, in addition to the
standard behavior, arranges for field and method
         * injection of each MethodInterceptor in [EMAIL PROTECTED] 
interceptors}.
         */
        @Override
        public void bindInterceptor(Matcher<? super Class<?>> classMatcher,
Matcher<? super Method> methodMatcher,
                MethodInterceptor... interceptors)
        {
                requestInjection(copyArray(interceptors));
                super.bindInterceptor(classMatcher, methodMatcher, 
interceptors);
        }

        /**
         * Overridden version of bindScope that, in addition to the standard
behavior, arranges for field and method
         * injection of [EMAIL PROTECTED] scope}.
         */
        @Override
        protected void bindScope(Class<? extends Annotation> scopeAnnotation,
Scope scope)
        {
                requestInjection(scope);
                super.bindScope(scopeAnnotation, scope);
        }
}



On Sep 13, 10:21 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> On Sep 12, 5:00 pm, Leigh Klotz <[EMAIL PROTECTED]> wrote:
>
> > Does anybody have suggestions about how I can let my LOCALE Scope
> > object use the Guice modules to obtain a Locale Provider?
>
> Your scopes shouldn't be static. There's a new
> method in recent snapshots called requestInjection()
> on AbstractModule. Use this method to get your
> scope instance injected in the same place that
> you call bindScope().
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to