1

My Question - Does anybody know how to get an instance of the Deployment interface in Weld? Ideally inside the beforeBeanDiscovery event?

I'm trying to implement a plug-in system in my application where the jar is loaded by my war app. I'm using JBoss AS 7.1 for that.

I have tried to include the JAR in the deployment process but it seems not possible unless the jar is in the WEB-INF/lib folder.

So my next move is trying to make CDI (Weld) to discover the beans inside an external JAR.

It seems CDI has no direct support to include new jars to be discovered, but Weld has a Deployment interface and a method called loadDeploymentArchive (http://docs.jboss.org/weld/javadoc/2.0/weld-spi/org/jboss/weld/bootstrap/spi/Deployment.html#loadBeanDeploymentArchive(java.lang.Class))

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
Ricardo Memoria
  • 454
  • 1
  • 3
  • 14

2 Answers2

0

That class is for integration with the application server. CDI is meant to have everything loaded at boot time for the application. To do what you're looking for you'd have to startup a new instance of Weld and pass everything along and do the bootstrapping yourself. Honestly I have no idea the implications of doing this in your application, but I can tell you that it would cause major headaches for you.

LightGuard
  • 5,298
  • 19
  • 19
0

Actually I found the answer in this article from Gavin King at http://relation.to/12981.lace

Basically I have to create an extension and map afterBeanDiscovery event like this:

public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager bm) {
    for ( final Class c: getBeanClasses() ) {

        //use this to read annotations of the class
        AnnotatedType at = bm.createAnnotatedType(c);

        //use this to create the class and inject dependencies
        final InjectionTarget it = bm.createInjectionTarget(at);

        abd.addBean( new Bean() {
            @Override
            public Class<?> getBeanClass() {
                return c;
            }

            @Override
            public Set<InjectionPoint> getInjectionPoints() {
                return it.getInjectionPoints();
            }

            @Override
            public String getName() {
                String s = c.getSimpleName();
                s = Character.toLowerCase( s.charAt(0) ) + s.substring(1);
                return s;
            }

            @Override
            public Set<Annotation> getQualifiers() {
                Set<Annotation> qualifiers = new HashSet<Annotation>();
                qualifiers.add( new AnnotationLiteral<Default>() {} );
                qualifiers.add( new AnnotationLiteral<Any>() {} );
                return qualifiers;
            }

            @Override
            public Class<? extends Annotation> getScope() {
                return Dependent.class;
            }

            @Override
            public Set<Class<? extends Annotation>> getStereotypes() {
                return Collections.emptySet();
            }

            @Override
            public Set<Type> getTypes() {
                Set<Type> types = new HashSet<Type>();
                types.add(c);
                types.add(Object.class);
                return types;
            }

            @Override
            public boolean isAlternative() {
                return false;
            }

            @Override
            public boolean isNullable() {
                return false;
            }

            @Override
            public Object create(CreationalContext ctx) {
                Object instance = it.produce(ctx);
                it.inject(instance, ctx);
                it.postConstruct(instance);
                return instance;
            }

            @Override
            public void destroy(Object instance, CreationalContext ctx) {
                it.preDestroy(instance);
                it.dispose(instance);
                ctx.release();
            }

        } );
    }
}

I also have to scan my jar for bean classes, but it's not a big deal. Once I find the classes, I include then in the getBeanClasses() list.

Now I'm searching for a solution on how to include JPA entities in Runtime.

But by now this issue is solved.

Ricardo Memoria
  • 454
  • 1
  • 3
  • 14