Uploaded image for project: 'Weld'
  1. Weld
  2. WELD-1502

Weld-servlet obtaining reference to BeanManager in Tomcat 7

    XMLWordPrintable

Details

    • Bug
    • Resolution: Won't Do
    • Major
    • None
    • 2.0.3.Final
    • Weld SPI
    • None
    • Hide

      Create a simple web project with tomcat. Configure weld-servlet to use with it, create a class like this:
      public class BeanLookuper {

      private static BeanManager getBeanManager() {
      try

      { return (BeanManager) InitialContext.doLookup("java:comp/env/BeanManager"); }

      catch (final NamingException e)

      { throw new RuntimeException(e); }

      }

      @SuppressWarnings("unchecked")
      public static <T> T lookup(final Class<T> clazz) {
      final BeanManager bm = getBeanManager();
      final Iterator<Bean<?>> iter = bm.getBeans(clazz).iterator();
      if (!iter.hasNext())

      { throw new IllegalStateException("CDI BeanManager cannot find an instance of requested type " + clazz.getName()); }

      final Bean<T> bean = (Bean<T>) iter.next();
      final CreationalContext<T> ctx = bm.createCreationalContext(bean);
      return (T) bm.getReference(bean, clazz, ctx);
      }

      }

      and try to call method "BeanLookuper.lookup(SomeClass.class);", from any servlet or jsp.

      Show
      Create a simple web project with tomcat. Configure weld-servlet to use with it, create a class like this: public class BeanLookuper { private static BeanManager getBeanManager() { try { return (BeanManager) InitialContext.doLookup("java:comp/env/BeanManager"); } catch (final NamingException e) { throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public static <T> T lookup(final Class<T> clazz) { final BeanManager bm = getBeanManager(); final Iterator<Bean<?>> iter = bm.getBeans(clazz).iterator(); if (!iter.hasNext()) { throw new IllegalStateException("CDI BeanManager cannot find an instance of requested type " + clazz.getName()); } final Bean<T> bean = (Bean<T>) iter.next(); final CreationalContext<T> ctx = bm.createCreationalContext(bean); return (T) bm.getReference(bean, clazz, ctx); } } and try to call method "BeanLookuper.lookup(SomeClass.class);", from any servlet or jsp.

    Description

      I configured weld with tomcat 7. Injection work fine for me, but when i try to obtain reference to BeanManager like this:
      InitialContext.doLookup("java:comp/env/BeanManager")
      I get exception :
      javax.naming.NamingException: Cannot create resource instance
      org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:117)
      javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
      org.apache.naming.NamingContext.lookup(NamingContext.java:843)
      org.apache.naming.NamingContext.lookup(NamingContext.java:154)
      org.apache.naming.NamingContext.lookup(NamingContext.java:831)
      org.apache.naming.NamingContext.lookup(NamingContext.java:154)
      org.apache.naming.NamingContext.lookup(NamingContext.java:831)
      org.apache.naming.NamingContext.lookup(NamingContext.java:168)
      org.apache.naming.SelectorContext.lookup(SelectorContext.java:158)
      javax.naming.InitialContext.lookup(InitialContext.java:411)
      javax.naming.InitialContext.doLookup(InitialContext.java:284)
      testapplication.BeanLookuper.getBeanManager(BeanLookuper.java:15)

      Attachments

        Activity

          People

            mkouba@redhat.com Martin Kouba
            ffatheranderson_jira Pavel S (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: