[weld-dev] Portable extension for injecting Seam beans
Gavin King
gavin.king at gmail.com
Fri Nov 20 03:10:46 EST 2009
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
More information about the weld-dev
mailing list