9 Replies Latest reply on Mar 9, 2009 5:45 AM by wolfc

    Attachments eligible for being passed to the deployers?

    jaikiran

      If one of the deployers adds a attachment to an unit that is being deployed, will that attachment then be passed on to the list of available deployers for being processed?

      Ex:

      public class MyDeployer extends AbstractDeployer
      {
      
       public MyDeployer()
       {
       setOutput(MyType.class);
      
       }
      
       public void deploy(DeploymentUnit unit)
       {
       // do something
       ...
       // add attachment
       MyType myObject = new MyType();
       unit.addAttachment(name,myObject);
       }
      
      }


      So at a later point of time, will the attachment of MyType be passed on to the available deployers?

        • 1. Re: Attachments eligible for being passed to the deployers?
          jaikiran

           

          So at a later point of time, will the attachment of MyType be passed on to the available deployers?


          Yes it will.

          I did not put up my question correctly. Let me just debug through the issue that i am running into, before maybe posting the correct question.


          • 2. Re: Attachments eligible for being passed to the deployers?
            alesj

             

            "jaikiran" wrote:
            So at a later point of time, will the attachment of MyType be passed on to the available deployers?


            Yes it will.

            Sure, that's the whole point of attachments.

            "jaikiran" wrote:

            I did not put up my question correctly. Let me just debug through the issue that i am running into, before maybe posting the correct question.

            :-)

            • 3. Re: Attachments eligible for being passed to the deployers?
              jaikiran

              So here's the real question :)

              Do attachments to component contexts of an unit get treated the same way? Do these attachments to component contexts get passed onto the deployers? From what i see this is not the case.

              Here's the example:

              // This one doesn't do much and is not of much relevance in this
              // example. It just creates a component which can be consumed
              // by our component deployer (MyComponentDeployer - see next)
              public class TestDeployer extends AbstractDeployer
              {
              
               public TestDeployer()
               {
               setStage(DeploymentStages.POST_CLASSLOADER);
               // this is our output which is consumed by MyComponentDeployer
               setOutput(TestAttachment.class);
               // some input - not relevant in this example
               setInput(JBossMetaData.class);
               }
              
              // creates a component to the unit and adds an attachment to the component
              // so that the MyComponentDeployer can consume it
               public void deploy(DeploymentUnit unit) throws DeploymentException
               {
               DeploymentUnit comp = unit.addComponent("something");
               comp.addAttachment(TestAttachment.class, new TestAttachment("hello"));
               logger.info("Added attachment to component in unit " + unit);
               }
              }
              



              // This one is important - this deployer is a component only deployer
              // and works on the component that was created by our earlier TestDeployer
              public class MyComponentDeployer extends AbstractDeployer
              {
              
              
               public MyComponentDeployer()
               {
               setStage(DeploymentStages.REAL);
               // the attachment that was added to the component by our earlier deployer
               // will be our input
               setInput(TestAttachment.class);
               // we work only on components
               setComponentsOnly(true);
               }
              
               // Here we just add an attachment to the unit (which acutally is an component)
               // Any further deployers (MyFinalDeployer - see next) will use this attachment
               // as input
               public void deploy(DeploymentUnit unit) throws DeploymentException
               {
               logger.info("Deploying unit : " + unit);
               // doesn't work
               unit.addAttachment(AnotherType.class, new AnotherType());
               }
              
              }
              
              


              // This one (tries to) consumes the attachment that was added by the component
              // deployer (see above)
              public class MyFinalDeployer extends AbstractDeployer
              {
               private static Logger logger = Logger.getLogger(OneMoreDeployer.class);
              
               public MyFinalDeployer()
               {
               setStage(DeploymentStages.REAL);
               // the attachment that was added by the component deployer
               setInput(AnotherType.class);
              
               }
              
               // never reaches here.
               public void deploy(DeploymentUnit unit) throws DeploymentException
               {
               logger.info("Deploying another type " + unit);
              
               }
              
              }
              
              
              


              There are 3 deployers involved here:

              1) The TestDeployer creates a component to an unit and also attaches an attachment to the component (works fine)
              2) The MyComponentDeployer consumes this component context and additionally creates an attachment to it (works fine/or maybe not).
              3) MyFinalDeployer is supposed to consume the attachment created in #2, but it never does. This deployer is considered "not relevant".

              The only way i could get this to work was to add the attachment to the parent of the component context in #2. So:

              public class MyComponentDeployer extends AbstractDeployer
              {
              
               // I don't know why i need to add the attachment to the
               // parent. But it works, so let's for the time being do that
               public void deploy(DeploymentUnit unit) throws DeploymentException
               {
               logger.info("Deploying unit : " + unit);
               // here - i'm clueless, the attachment needs to be added to parent for some reason
               // Note, this is a component deployer and works on component context
               unit.getParent().addAttachment(AnotherType.class, new AnotherType());
               }
              
              }
              


              Doing this makes MyFinalDeployer (in #3) "relevant" and the unit is passed on the deploy method of MyFinalDeployer.

              So the question is - what am i missing? :)

              • 4. Re: Attachments eligible for being passed to the deployers?
                alesj

                 

                "jaikiran" wrote:

                So the question is - what am i missing? :)

                Your #3 deployer is not a component deployer.

                Your #2 puts attachment into component,
                hence it's only component deployers that can see that.

                Component deployers should mostly consume attachments
                not creating them - hence the name "component". ;-)

                • 5. Re: Attachments eligible for being passed to the deployers?
                  jaikiran

                   

                  "alesj" wrote:


                  Your #3 deployer is not a component deployer.

                  That's correct.

                  "alesj" wrote:

                  Your #2 puts attachment into component,
                  hence it's only component deployers that can see that.


                  Any specific reason for this special treatment? Because after all that is a simple attachment (and not a component) and hence i was considering that it would be visible to non-component deployers.

                  "alesj" wrote:

                  Component deployers should mostly consume attachments
                  not creating them - hence the name "component". ;-)

                  And i thought the name "component" was because they work on components rather than what output they generate :)

                  So would that mean, the attachment generated by a component deployer is of no pratical use to non-component deployers?

                  So would this be a clean way of doing things in a component deployer:

                  public void deploy(DeploymentUnit unit) throws DeploymentException
                   {
                   logger.info("Deploying unit : " + unit);
                  
                   // Am i deploying a component?
                   if (unit.isComponent())
                   {
                   // yes, i am deploying a component
                   ...
                   // should my attachments be visible to non-component deployers?
                   if (shouldMyAttachmentsBeVisibleToAll())
                   {
                   // should be visible to all, so add to parent
                   unit.getParent().addAttachment(something,someotherthing);
                   }
                   else
                   {
                   // only to component deployers
                   unit.addAttachment(something,someotherthing);
                   }
                  
                   }
                  
                   }




                  • 6. Re: Attachments eligible for being passed to the deployers?
                    jaikiran

                     

                    "alesj" wrote:

                    Your #3 deployer is not a component deployer.


                    Just as a FYI - making my #3 deployer a component deployer did get it working. The #3 deployer was give the chance of deploying the deployment.

                    • 7. Re: Attachments eligible for being passed to the deployers?
                      alesj

                       

                      "jaikiran" wrote:

                      So would that mean, the attachment generated by a component deployer is of no pratical use to non-component deployers?

                      No, that's exactly what I'm trying to explain.
                      Component deployers should only consume attachments,
                      not create/generate them.

                      "jaikiran" wrote:

                      So would this be a clean way of doing things in a component deployer:

                      public void deploy(DeploymentUnit unit) throws DeploymentException
                       {
                       logger.info("Deploying unit : " + unit);
                      
                       // Am i deploying a component?
                       if (unit.isComponent())
                       {
                       // yes, i am deploying a component
                       ...
                       // should my attachments be visible to non-component deployers?
                       if (shouldMyAttachmentsBeVisibleToAll())
                       {
                       // should be visible to all, so add to parent
                       unit.getParent().addAttachment(something,someotherthing);
                       }
                       else
                       {
                       // only to component deployers
                       unit.addAttachment(something,someotherthing);
                       }
                      
                       }
                      
                       }



                      The moment you're hacking with getParent::addAtachment you know
                      there is something wrong with how you're doing it.
                      I know we have something like that in our JPA deployers. :-(

                      • 8. Re: Attachments eligible for being passed to the deployers?
                        jaikiran

                         

                        "alesj" wrote:

                        Component deployers should only consume attachments,
                        not create/generate them.


                        I wasn't aware of this. And given that the component deployers work on a DeploymentUnit which provides the addAttachment API, i assumed that it would behave the same as other deployers.


                        • 9. Re: Attachments eligible for being passed to the deployers?
                          wolfc

                          So why does the AliasDeploymentDeployer add an attachment?

                           protected static void addAliasComponent(DeploymentUnit unit, NamedAliasMetaData alias)
                           {
                           DeploymentUnit component = unit.addComponent(alias.getAliasValue().toString());
                           component.addAttachment(NamedAliasMetaData.class.getName(), alias);
                           }
                          

                          When will you answer my question? :-)
                          http://www.jboss.org/index.html?module=bb&op=viewtopic&t=145178