1 Reply Latest reply on Nov 3, 2008 3:22 AM by alesj

    Programmatically getting JNDI Context

    amit.bhayani

      I have a app which is on MBeans as of now. I am trying to port to MC. One of the requirements I have is one of the MBeans creates POJO's and register with JNDI as and when required. In MC I was thinking this can be replaced by a Factory that gives new instance of POJO bean when 'newInsatnce' is called. However the requirement is such that it should get registered with JNDI for latter look-up. This app should be flexible to be deployed in JBoss AS and hence use JBoss JNDI or standalone and hence use MockInitialContextFactory.

      I have seen JndiDecoratedTestCase and it registers the beans to JNDI, but JNDI name is predefined here. While in my app the JNDI name is calculated at runtime. I am wondering how can this be achieved?

      Can I get access to JNDI Contex programmatically which has INITIAL_CONTEXT_FACTORY set to MockInitialContextFactory in standalone and set to JBoss context in JBoss environment? This way I can use factory to get bean and then code in MBean for JNDI registration.


      Thanks in advance.

        • 1. Re: Programmatically getting JNDI Context
          alesj

           

          "amit.bhayani@jboss.com" wrote:
          I have seen JndiDecoratedTestCase and it registers the beans to JNDI, but JNDI name is predefined here. While in my app the JNDI name is calculated at runtime.

          As you could see in JndiDecoratedTestCase, to push bean in JNDI, you had to enable AOP interceptor:
           <interceptor xmlns="urn:jboss:aop-beans:1.0" name="JndiAspect" class="org.jboss.aop.microcontainer.aspects.jndi.JndiIntroduction">
           <property name="env">
           <map class="java.util.Properties" keyClass="java.lang.String" valueClass="java.lang.String">
           <entry><key>java.naming.factory.initial</key><value>org.jboss.test.microcontainer.support.jndi.MockInitialContextFactory</value></entry>
           </map>
           </property>
           </interceptor>
          
           <introduction xmlns="urn:jboss:aop-beans:1.0" class="@org.jboss.aop.microcontainer.aspects.jndi.JndiBinding">
           <interfaces>org.jboss.kernel.spi.dependency.KernelControllerContextAware</interfaces>
           </introduction>
          
           <bind xmlns="urn:jboss:aop-beans:1.0" pointcut="execution(* $instanceof{org.jboss.kernel.spi.dependency.KernelControllerContextAware}->$implements{org.jboss.kernel.spi.dependency.KernelControllerContextAware}(..))">
           <interceptor-ref name="JndiAspect"/>
           </bind>
          

          Although JndiIntroduction is deprecated and suggest usage of JndiLifecycleCallback (a lot easier == less xml):
           <lifecycle-configure xmlns="urn:jboss:aop-beans:1.0"
           name="DependencyAdvice"
           class="org.jboss.aop.microcontainer.aspects.jndi.JndiLifecycleCallback"
           classes="@org.jboss.aop.microcontainer.aspects.jndi.JndiBinding">
           <property name="env">
           <map class="java.util.Properties" keyClass="java.lang.String" valueClass="java.lang.String">
           <entry><key>java.naming.factory.initial</key><value>org.jboss.test.microcontainer.support.jndi.MockInitialContextFactory</value></entry>
           </map>
           </property>
           </lifecycle-configure>
          


          "amit.bhayani@jboss.com" wrote:
          I am wondering how can this be achieved?

          Why the code above?
          To make you see it's all about configuration. ;-)

          You can easily implement / configure your own interceptor / lifecycle.
          e.g. custom @AmitsJndiAnnotation + matching lifecycle.
          Or customize the one's above, as you can mix-n-match AOP schema with MC's.

          You can either change original JndiLifecycleCallback - making it more customizable - or write your own.
          In any case, push the changes here, and I'll make sure they get applied.
          So other users already have all that configuration power you wanted. :-)