[weld-dev] Portable extension for injecting Seam beans

Dan Allen dan.j.allen at gmail.com
Fri Nov 20 04:17:07 EST 2009


Norman, I figure you should give this a try since you'll be focusing on the
initial Seam 2 integration.

-Dan

On Fri, Nov 20, 2009 at 9:10 AM, Gavin King <gavin.king at gmail.com> wrote:

> Folks, would someone please try out the following portable extension
> for me. It should be enough to let you typesafe-inject a Seam2
> component using Weld, e.g.
>
>   @Inject @Qualifier MySeamComponent msc;
>
> It's not enough to allow the use of typesafe injection in the seam
> component itself, however.
>
> Please test this for me, thanks!
>
> ++++
> package org.seamframework;
>
> import java.util.Collections;
> import java.util.Set;
>
> import javax.enterprise.context.RequestScoped;
> import javax.enterprise.context.spi.CreationalContext;
> import javax.enterprise.event.Observes;
> import javax.enterprise.inject.spi.AnnotatedType;
> import javax.enterprise.inject.spi.BeforeBeanDiscovery;
> import javax.enterprise.inject.spi.Extension;
> import javax.enterprise.inject.spi.InjectionPoint;
> import javax.enterprise.inject.spi.InjectionTarget;
> import javax.enterprise.inject.spi.ProcessInjectionTarget;
> import javax.enterprise.util.AnnotationLiteral;
>
> import org.jboss.seam.Component;
> import org.jboss.seam.annotations.Name;
>
> public class SeamComponentExtension implements Extension {
>
>        void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd) {
>                bbd.addStereotype(Name.class, new
> AnnotationLiteral<RequestScoped>()
> {}); //TODO: put it in a @StatelessScope!
>        }
>
>        <X> void processInjectionTarget(@Observes final
> ProcessInjectionTarget<X> pit) {
>                final AnnotatedType<X> at = pit.getAnnotatedType();
>                if ( at.isAnnotationPresent(Name.class) ) {
>                        if (
> !pit.getInjectionTarget().getInjectionPoints().isEmpty() ) {
>                                throw new RuntimeException("CDI injection
> points not supported for
> Seam2 components");
>                        }
>                        InjectionTarget<X> it = new InjectionTarget<X>() {
>                                @Override
>                                public void inject(X instance,
> CreationalContext<X> ctx) {
>                                        //TODO figure out a way to inject
> into the Seam component
>                                }
>                                @Override
>                                public void postConstruct(X instance) {}
>                                @Override
>                                public void preDestroy(X instance) {}
>                                @Override
>                                public void dispose(X instance) {}
>                                @Override
>                                public Set<InjectionPoint>
> getInjectionPoints() {
>                                        return Collections.EMPTY_SET;
>                                }
>                                @Override
>                                public X produce(CreationalContext<X> ctx) {
>                                        //TODO: it would be better to
> override
> Component.instantiateJavaBean() to call
>                                        //      it.produce(), in order to
> get injection, etc, but then we
> would have
>                                        //      to figure out a way to
> replace the Seam Component object
>                                        return (X) Component.getInstance(
> at.getJavaClass(), true );
>                                }
>                        };
>                        pit.setInjectionTarget(it);
>                }
>        }
>
> }
>
>
> --
> Gavin King
> gavin.king at gmail.com
> http://in.relation.to/Bloggers/Gavin
> http://hibernate.org
> http://seamframework.org
> _______________________________________________
> weld-dev mailing list
> weld-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/weld-dev
>



-- 
Dan Allen
Senior Software Engineer, Red Hat | Author of Seam in Action
Registered Linux User #231597

http://mojavelinux.com
http://mojavelinux.com/seaminaction
http://www.google.com/profiles/dan.j.allen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/weld-dev/attachments/20091120/4ade5dfe/attachment-0001.html 


More information about the weld-dev mailing list