8 Replies Latest reply on Jun 3, 2010 4:05 AM by alesj

    Lookup up a POJO service without injecting it inside -beans.xml?

    martinn

      I need to be able to look up a POJO deployed service on JBoss 5's MC, but I do not know how to obtain a reference to the kernel so I can obtain a ControllerContext.

       

      My onIy constraint is I can not modify the interface of the service where I need to lookup the POJO service (or else I could just inject it straight up via the -bean.xml descriptor).  The look up would work exactly as JNDI would - but why do JNDI if I can somehow directly access the JBoss MC kernel?

       

      Can someone point me to an example on how to do this, or is this even possible?

       

      I did find Ales Justin's mc-int utility that looks up the kernel inside a servlet given a "ServletContext" as input. I need something exactly like this.

       

      Am I missing something really obvious here because it looks to me like there should be some easy way of doing this .

       

      Sorry - I've been doing JMX and jboss 4.2.3 for a long time and am sorta new to this paradigm..

       

      Thanks!

        • 1. Re: Lookup up a POJO service without injecting it inside -beans.xml?
          alesj

          I cannot gather from your description on how exactly you're trying to do this.

          e.g. where would you need to do this lookup (ok, from some interface method that cannot be changed)?

           

          Why a simple setter injection doesn't work for you?

          Of either the service you're loooking for or the Controller instance.

          • 2. Re: Lookup up a POJO service without injecting it inside -beans.xml?
            martinn

            Zdravo Ales...

             

            here's my classes:

             

            package com.raf.test;

             

            import org.jboss.kernel.spi.dependency.KernelControllerContext;
            import org.jboss.kernel.spi.dependency.KernelControllerContextAware;
            import org.jboss.logging.Logger;

             

            public class ServiceA implements KernelControllerContextAware {
                private KernelControllerContext controllerContext;
                private Logger log;
                private String val;
                private ServiceB svcB;

             

                public ServiceA() {
                    log = Logger.getLogger(getClass().getName());
                }
               
                public String getValue() {
                    return val;
                }

             

                public void setValue(String val) {
                    this.val = val;
                }
               
                public ServiceB getServiceB() {
                    return this.svcB;
                }
               
                public void setServiceB(ServiceB val) {
                    this.svcB = val;
                }

             

                public void init() {
                    try {
                        log.info("lookup = " + controllerContext.getController().getInstalledContext("SvcB"));
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                    log.info("init-ed");
                }
               
                public void create() {
                    log.info("created");
                }
               
                public void start() {
                    log.info("started");
                }
               
                public void stop() {
                    log.info("stopped");
                }
               
                public void destroy() {
                    log.info("destroyed");
                }

             

                @Override
                public void setKernelControllerContext(KernelControllerContext arg0)
                        throws Exception {
                    this.controllerContext = arg0;
                }

             

                @Override
                public void unsetKernelControllerContext(KernelControllerContext arg0)
                        throws Exception {
                    this.controllerContext = null;
                }
            }

             

            package com.raf.test;

             

            import org.jboss.logging.Logger;

             

            public class ServiceB {

             

                private Logger log;
                private String val;

             

                public ServiceB() {
                    log = Logger.getLogger(getClass().getName());
                }
               
                public String getValue() {
                    return val;
                }

             

                public void setValue(String val) {
                    this.val = val;
                }

             

                public void init() {
                    log.info("init-ed");
                }
               
                public void create() {
                    log.info("created");
                }
               
                public void start() {
                    log.info("started");
                }
               
                public void stop() {
                    log.info("stopped");
                }
               
                public void destroy() {
                    log.info("destroyed");
                }
            }

             

            and the descriptor:

             

            <?xml version="1.0" encoding="UTF-8"?>
            <deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xsi:schemaLocation="urn:jboss:bean-deployer bean-deployer_2_0.xsd"
                        xmlns="urn:jboss:bean-deployer:2.0">

             

                <bean name="SvcA" class="com.raf.test.ServiceA">
                    <install method="init"/>
                </bean>
               
                <bean name="SvcB" class="com.raf.test.ServiceB">
                    <install method="init"/>
                </bean>
               
            </deployment>

             

            The "getInstalledContext("SvcB")" from ServiceA.java fails. It suggest some sort of scoping is at play because if I look up SvcA from ServiceA, it works just fine.  I want to be able to look up a service just the same as if it is injected to me via "<inject bean=" from any service. Is this possible? Thanks!

             

            Martin

            • 3. Re: Lookup up a POJO service without injecting it inside -beans.xml?
              alesj

              Zdravo :-)

               

              The "getInstalledContext("SvcB")" from ServiceA.java fails. It suggest some sort of scoping is at play because if I look up SvcA from ServiceA, it works just fine.  I want to be able to look up a service just the same as if it is injected to me via "<inject bean=" from any service. Is this possible? Thanks!

               

              No scoping. ;-)

              I think I know what the problem is.

               

              You're doing the lookup too soon.

              At the moment you try to find installed SvcB, that one is not installed yet.

               

              You can either

              * try to do lookup via getContext("SvcB", null)

              * make sure when SvcA invoked init, SvcB is already fully installed; SvcA depends on SvcB at Installed state

               

              If this is not it, post the stack trace you're catching in init.

              • 4. Re: Lookup up a POJO service without injecting it inside -beans.xml?
                martinn

                Ales -

                 

                getContext("SvcB", null) worked great, and so did the depends - the only caveat with the depends that I experienced in my other code (which I can't paste verbatim) is a circular dependency. However, the manual, thankfully, is clear on how to resolve those by depending on a different state (e.g. Instantiated) than 'installed', so I was able to resolve it that way too.

                 

                hvala za vaš  nasvet,

                 

                Martin

                p.s. I'm from Macedonia (ex-YU).

                • 5. Re: Lookup up a POJO service without injecting it inside -beans.xml?
                  alesj

                  Why do you actually have to go over KernelControllerContextAware interface?

                  Why not inject the service directly, via setter?

                  Or in the worst case, inject the Controller directly.

                  • 6. Re: Lookup up a POJO service without injecting it inside -beans.xml?
                    martinn

                    That's what I did eventually. I had a misunderstanding with the software architect - I understood him that I wasn't supposed to modify his interface, which I thought meant, no adding of injection setters/getters on the implementation classes - thus I asked you how to get at the kernelcontrollercontext via the locator pattern... Turns out, after talking to him, he only meant not modifying the _actual_ interface (public interface xxx...) and he was fine with me adding injectors (setters) on the impl....

                     

                    He was trying to develop some sort of a 'generic' interface for some of our products that he didn't want bound to anything jboss-y, that we could reuse anywhere, outside jboss or any other container.... long story that I can't quite fill you in on unless you come work for us .

                     

                    Hvala.

                     

                    Martin

                    • 7. Re: Lookup up a POJO service without injecting it inside -beans.xml?
                      dimonv

                      Hi,

                       

                      I'm interresting on the same use case and tried  to implement a kind of bean lookup. But in the version 2.0.6.GA of MC  there is no method org.jboss.kernel.spi.dependency.KernelController.getContext(String , ...).

                       

                      Is there an API for looking up MC for beans?

                       

                      Thanks

                      • 8. Re: Lookup up a POJO service without injecting it inside -beans.xml?
                        alesj
                        I'm interresting on the same use case and tried  to implement a kind of bean lookup. But in the version 2.0.6.GA of MC  there is no method org.jboss.kernel.spi.dependency.KernelController.getContext(String , ...).

                         

                        Is there an API for looking up MC for beans?

                        http://anonsvn.jboss.org/repos/jbossas/projects/microcontainer/tags/2.0.6.GA/dependency/src/main/java/org/jboss/dependency/spi/Controller.java