7 Replies Latest reply on May 22, 2009 5:48 PM by Ales Justin

    Programmatically deploying an object

    Matt Wringe Master

      I have a webapp and when a listener gets called I want to deploy an object into the microcontainer . The listener is being handled in the webapplication, so I can't just add it to the beans.xml file and it doesn't have a direct reference to the mircocontainer kernel.

      Is there some sort of preferred method to programmatically deploy an object into a microcontainer in this situation?

      If not, I can always just write a custom pojo factory to handle it. I am just trying to see if there is already a preferred solution for this.


        • 1. Re: Programmatically deploying an object
          Ales Justin Master

           

          "mwringe" wrote:
          so I can't just add it to the beans.xml file and it doesn't have a direct reference to the mircocontainer kernel.

          Sure you do. ;-)

          Check the glue code that I wrote for Seam/webBeans:
          - http://anonsvn.jboss.org/repos/jbossas/projects/mc-int/trunk/servlet/

          "mwringe" wrote:

          Is there some sort of preferred method to programmatically deploy an object into a microcontainer in this situation?

          You can either get the MainDeployer / DeployerClient from
          * the Controller via "MainDeployer" name (which is too impl specific)
          * DeploymentUnit::getMainDeployer

          Then simply create pre-determined deployment (StructureContext + metadata attachments),
          and deploy it as any other deployment via DeployerClient.

          But if you just wanna deploy some bean, install it directly against KernelController.
          Just make sure you remove/undeploy it at some listener's undeploy.

          • 2. Re: Programmatically deploying an object
            Matt Wringe Master

             

            alesj wrote:
            Check the glue code that I wrote for Seam/webBeans:
            - http://anonsvn.jboss.org/repos/jbossas/projects/mc-int/trunk/servlet/


            Any example on how to actually use it?

            I see the code has access to the servlet context which is one of the things I am really interested in.

            • 3. Re: Programmatically deploying an object
              Ales Justin Master

               

              "mwringe" wrote:

              Any example on how to actually use it?

              It's easy, just follow the api. ;-)

              We used to use this with WebBeans integration,
              to bootstrap the wb framework.
              This was deleted as we now use different bootstrap mechanism,
              but you can probably browse wb's svn history for some usage.

              • 4. Re: Programmatically deploying an object
                Matt Wringe Master

                Ok, lets try this again. And please don't post code from somewhere and refuse to explain it. Looking through the code doesn't help if I don't know in what context to use it :)

                The situation is that I need to have access to the servlet context for deployment (see http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4228452 and https://jira.jboss.org/jira/browse/JBAS-6865). Until its actually supported properly, I need to find away around it.

                If I use a servlet context listener, I can easily get this object when the webapp is initialized. So I know how to get the servlet context indirectly, but the servletcontextlistener is handled by the server and not the microcontainer.

                Is there a proper way too add this object as an attachment to the deployment unit for the war? Or to hook this into the microcontainer to do so?

                • 5. Re: Programmatically deploying an object
                  Ales Justin Master

                   

                  "mwringe" wrote:
                  Ok, lets try this again. And please don't post code from somewhere and refuse to explain it. Looking through the code doesn't help if I don't know in what context to use it :)

                  This sounds to me like hand holding, which is something I don't do,
                  at least to a project that is not what I should be working on. ;-)

                  Second, it sounds like you didn't or aren't willing to do any investigation.
                  Which means I should do your homework.

                  If I say it's simple, I say it because it is, not to mislead you or show off.
                  As I know you previously did quite well with deployers,
                  I very much doubt that you're not able to trace down ServletContext's purpose in that code.

                  "mwringe" wrote:

                  The situation is that I need to have access to the servlet context for deployment (see http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4228452 and https://jira.jboss.org/jira/browse/JBAS-6865). Until its actually supported properly, I need to find away around it.

                  This is a different issue, just the opposite of what your initial post was about.
                  But that is something to be, unfortunately, addressed by the jbw/tomcat guys.
                  And if things go .tld way, we're in for a long waiting. ;-)

                  "mwringe" wrote:

                  If I use a servlet context listener, I can easily get this object when the webapp is initialized. So I know how to get the servlet context indirectly, but the servletcontextlistener is handled by the server and not the microcontainer.

                  Your first post said you're fine with this.

                  And this is exactly how mc-int helps you.
                  You should provide a custom ServletContextListener (SCL),
                  which then uses MC-int to get proper underlying Kernel or DeploymentUnit.
                  Kernel --> Controller --> register or do lookup
                  (VFS)DeploymentUnit --> check resources or any attachments

                  "mwringe" wrote:

                  Is there a proper way too add this object as an attachment to the deployment unit for the war? Or to hook this into the microcontainer to do so?

                  Which object?

                  If you mean SCL, you could just add a deployer,
                  that requires JBWMD + JBPortalMD --> add new SCL metadata to JBWMD

                  WebBeans deployers do something similar,
                  or do I need to draw this one down again ... sarc ... ;-)


                  • 6. Re: Programmatically deploying an object
                    Matt Wringe Master

                     


                    This sounds to me like hand holding, which is something I don't do,
                    at least to a project that is not what I should be working on. ;-)

                    Second, it sounds like you didn't or aren't willing to do any investigation.
                    Which means I should do your homework.

                    If I say it's simple, I say it because it is, not to mislead you or show off.
                    As I know you previously did quite well with deployers,
                    I very much doubt that you're not able to trace down ServletContext's purpose in that code.


                    Instead of going into a huge rant, I am going to apologize for what you thought I was asking, this is not the message I was trying to convey.

                    • 7. Re: Programmatically deploying an object
                      Ales Justin Master

                       

                      "mwringe" wrote:

                      Instead of going into a huge rant, I am going to apologize for what you thought I was asking, this is not the message I was trying to convey.

                      Apology accepted. :-)

                      OK, let me do some quick pseudo code.
                      e.g. lets say I wanna register a new bean to MC
                      MyServletContextListener::contextInitialized(SCE sce)
                      {
                       ServletContext servletContext = ...;
                       KernelControllerVDFConnector kcvc = new KernelControllerVDFConnector(servletContext);
                       KernelController controller = kcvc.getUtility();
                       BeanMetaDataBuilder builder = ...;
                       controller.install(builder.getBeanMetaData());
                      }
                      
                      MyServletContextListener::contextDestroyed(SCE sce)
                      {
                       ServletContext servletContext = ...;
                       KernelControllerVDFConnector kcvc = new KernelControllerVDFConnector(servletContext);
                       KernelController controller = kcvc.getUtility();
                       controller.uninstall(someBeanName);
                      }
                      


                      HTH