0 Replies Latest reply on Jan 19, 2012 12:13 PM by Eduardo Lopes

    Beans loaded through CDI extensions sometimes not available

    Eduardo  Lopes Newbie


      I'm trying to load some beans through a CDI extension, and such beans come from jar files. Sometimes they are available on JSF pages. But, if I reboot jboss (version 6.1) the beans are not available anymore, and I pretty sure that the extension load correctly the bean classes whenever the CDI container initializes. I wrote the CDI extension according to the example available at: http://in.relation.to/Bloggers/AnotherNicePortableExtension

      Can anyone tell what I'm missing here?

      Below, I list the code:


      List<Class<?>> getBeanClasses() throws IOException, ClassNotFoundException, 
      InstantiationException, IllegalAccessException {
                PluginLoader loader = new PluginLoader();
                PluginPathResolver resolver = new WebPathResolver();
                File file = new File(resolver.getPluginRepository().
                PluginBundle bundle = loader.load(file);
                loader.installView(file, resolver, bundle.getName());
                return bundle.getManagedBeans();
          void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager bm) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
               System.out.println("---------- loading the beans -------------");
               if(getBeanClasses() == null){
                    System.out.println("no classes to load.");
              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() {
                      public Class<?> getBeanClass() {
                          return c;
                      public Set<InjectionPoint> getInjectionPoints() {
                          return it.getInjectionPoints();
                      public String getName() {
                          //hardcoded beacause I'll load a known class 
                          //for testing the approach
                          return "myDateUtils";
                      public Set<Annotation> getQualifiers() {
                          Set<Annotation> qualifiers = new HashSet<Annotation>();
                          qualifiers.add( new AnnotationLiteral<Default>() {} );
                          qualifiers.add( new AnnotationLiteral<Any>() {} );
                          return qualifiers;
                      public Class<? extends Annotation> getScope() {
                          //return Dependent.class;
                           return SessionScoped.class;
                      public Set<Class<? extends Annotation>> getStereotypes() {
                          return Collections.emptySet();
                      public Set<Type> getTypes() {
                          Set<Type> types = new HashSet<Type>();
                          return types;
                      public boolean isAlternative() {
                          return false;
                      public boolean isNullable() {
                          return false;
                      public Object create(CreationalContext ctx) {
                          Object instance = it.produce(ctx);
                          it.inject(instance, ctx);
                          return instance;
                      public void destroy(Object instance, CreationalContext ctx) {
                  } );

      Class that should be dynamically load into CDI:

      public class DateUtils {
           public String getToday(){
               return the date formatted string..
           public void showMessage(){
               System.out.println("Class post construct method!")

      Simple facelets page using the bean:

      <html xmlns="http://www.w3.org/1999/xhtml"
                <h1>Adicionar diário</h1>
      <h:outputText value="new.xhtml" /><br/>               
      <h:outputText value="#{myDateUtils.today}"/>