1 2 Previous Next 23 Replies Latest reply on Oct 5, 2012 1:08 PM by cpineda

    BeanManager not accessible via JNDI

    marx3

      There is no possibility to access BeanManager via JNDI. I tried it on the newest JBoss 7.1.1. Is it a bug or a feature?

       

       

          String name = "java:comp/" + BeanManager.class.getSimpleName();
         
              InitialContext ic = new InitialContext();
              beanManager = (BeanManager) ic.lookup(name);

       


      javax.naming.NameNotFoundException: java:comp/BeanManager
        • 1. Re: BeanManager not accessible via JNDI
          lightguard

          That should work, sounds like a bug in AS7

          • 2. Re: BeanManager not accessible via JNDI
            marx3

            I've partially found a problem - BeanManager in JNDI is available only for EE components. I'm trying to use DI in Quartz job. If I get stateless component from JNDI, and it use @EJB for injection - everything works. But if stateless uses @Inject - all injected fields are null. So I'm trying to use BeanManager for getting components (hopefully it will manage @Inject for me) , but I can't find BeanManager in JNDI. I even tried to get BeanManager from stateles, it works, but if I try to use it - it fails with exception: "java.lang.IllegalStateException: JBAS011048: Failed to construct component instance".

             

            So I think that using injection in Quartz job isn't really possible, and I have to rewrite all @Inject=>@EJB

            • 3. Re: BeanManager not accessible via JNDI
              lightguard

              Currently you'd need to save off the BeanManager in an extension and get it that way. According to Pete, this situation will be fixed in CDI 1.1

              • 4. Re: BeanManager not accessible via JNDI
                radzish

                Could you please provide more information on how can I do that?

                • 5. Re: BeanManager not accessible via JNDI
                  lightguard
                  • 6. Re: BeanManager not accessible via JNDI
                    mark_1

                    Hi,

                     

                     

                    I faced the same problem, but I resolved it in a different way.

                    I defined a class extending the org.quartz.simpl.PropertySettingJobFactory, which is responsible to create the quartz job, and I injected in it the beanManager.

                    Then I've overridden the newJob method, in order to control the instantiation of the jobs, making use of the beanManager. This is a code snippet of my class:



                    @Inject

                    private BeanManager beanManager;

                     

                     


                    @Override

                    public Job newJob(TriggerFiredBundle bundle, Scheduler Scheduler)



                    throws SchedulerException {


                    Job job = super.newJob(bundle, Scheduler);


                    Class<? extends Job> clazz=job.getClass();


                    System.out.println("CDI FACTORY___");


                    if (beanManager != null) {





                    System.out.println("BEANMANAGER");



                    CreationalContext<Job> ctx = beanManager





                    .createCreationalContext(null);



                    @SuppressWarnings("unchecked")



                    AnnotatedType<Job> type =(AnnotatedType<Job>) beanManager





                    .createAnnotatedType(clazz);



                    InjectionTarget<Job> it = beanManager





                    .createInjectionTarget(type);



                    it.inject(job, ctx);


                    }

                     

                     



                    return job;

                    }

                     

                     

                     

                     

                     

                     

                     

                     

                    I hope it is useful

                    bye

                    • 7. Re: BeanManager not accessible via JNDI
                      cpineda

                      I found and implemented this code in my POJO:

                       

                      public BeanManager getBeanManager()

                           {

                               try{

                                   InitialContext initialContext = new InitialContext();

                                   return (BeanManager) initialContext.lookup("java:comp/BeanManager");

                               }catch (NamingException e) {

                                   log.error("Couldn't get BeanManager through JNDI");

                                   return null;

                               }

                           }

                        @SuppressWarnings("unchecked")

                        public UsuarioBean getFacade()

                        {

                        BeanManager bm = getBeanManager();

                       

                        if (bm!=null) {

                        Bean<UsuarioBean> bean = (Bean<UsuarioBean>) bm.getBeans(UsuarioBean.class).iterator().next();

                        CreationalContext<UsuarioBean> ctx = bm.createCreationalContext(bean);

                        UsuarioBean dao = (UsuarioBean) bm.getReference(bean, UsuarioBean.class, ctx); // this could be inlined, but intentionally left this way

                        return dao;

                        }

                        return null;

                       

                        }

                       

                       

                      BeanManager also be possible with Idetity Seam 3?

                       

                      For example:

                       

                      getIdentity public Identity ()

                      {

                      try {

                      InitialContext initialContext = new InitialContext ();

                      return (Identity) initialContext.lookup ("java: comp / Identity");

                      } catch (NamingException e) {

                      log.error ("Could not get BeanManager through JNDI");

                      return null;

                      }

                      }

                      • 8. Re: BeanManager not accessible via JNDI
                        lightguard

                        I highly doubt it. You could get it from the BeanManager though.

                        • 9. Re: BeanManager not accessible via JNDI
                          cpineda

                          BeanManager get the embargo? as serious?

                          • 10. Re: BeanManager not accessible via JNDI
                            lightguard

                            I don't believe PicketLink puts anything in JNDI, I know Seam 3 doesn't put anything in JNDI. It's CDI that has specified that the BeanManager is placed in JNDI. Identity is from PicketLink.

                            • 11. Re: BeanManager not accessible via JNDI
                              cpineda

                              Ok, but can not find any solution q let me somehow manipulate the Identity, not @ Injetc if not through programming, as I stated above.

                              • 12. Re: BeanManager not accessible via JNDI
                                lightguard

                                If you can get the BeanManager, you can look up the Identity instance manually. As far as I can see, that's the only way you're going to be able to access it and have it work correctly.

                                • 13. Re: BeanManager not accessible via JNDI
                                  cpineda

                                  Exactly, but not how to get the BeanManager, try the JNDI but casting problems

                                  • 14. Re: BeanManager not accessible via JNDI
                                    lightguard

                                    Going back up the thread are you in an EJB or just a POJO? If it's a POJO, you're out of luck unless you do what we've done in Apache DeltaSpike and store the BeanManager in a class using an extension.

                                    1 2 Previous Next