7 Replies Latest reply on Feb 25, 2009 6:01 AM by alesj

    What is the correct time to invoke the getProperties on a Be

    jaikiran

      I am trying to dynamically create and deploy an MC bean as follows:

      // My MC bean
      public class MyBean
      {
      
       // the attributes especially the bean name will be dynamically modified
       @Inject
       private MyOtherBean otherBean;
      
      // other stuff
      }
      


      // The code which creates instances of these MyBean and deploys it into MC
      
      BeanMetaDataBuilder builder = BeanMetaDataBuilder.createBuilder("someuniquename", "org.myapp.MyBean");
      MyBean bean = new MyBean();
      builder.setConstructorValue(bean);
      
      // Now let's get the PropertyMetadata from the bean (i.e. the @Inject property "otherBean")
      Set<PropertyMetaData> propertiesOnMyBean = builder.getBeanMetaData().getProperties();
      
      


      The property metadata is always null. Isn't this the right time to call the builder.getBeanMetaData().getProperties()?


      The reason i need this information is that i want to dynamically change some attributes of @Inject for the "otherBean" - something on these lines dynamically :

      Set<PropertyMetaData> propertiesOnNoInterfaceMCBean = builder.getBeanMetaData().getProperties();
      for (PropertyMetaData propertyMetadata : propertiesOnNoInterfaceMCBean)
      {
       AnnotationMetaData anm = new AbstractAnnotationMetaData(Inject.class.getName());
       if (propertyMetadata.getAnnotations().contains(anm) && propertyMetadata.getType().equals(MyOtherBean.class.getName()))
       {
       logger.info("Changing Inject " + propertyMetadata.getName());
       // notice the bean name that we are adding to the @Inject is dynamic
       ValueMetaData inject = builder.createInject("dynamicBeanName", null, null, ControllerState.DESCRIBED);
       // addPropertyMetaData replaces the property metadata (if any)
       builder.addPropertyMetaData(propertyMetadata.getName(), inject);
      
       break;
       }
      }
      
      



        • 1. Re: What is the correct time to invoke the getProperties on
          alesj

          The builder just builds metadata.
          Annotation's metadata population happens only in Describe state,
          as we know there that our CL exists == can do class info inspection.

          I looks like you're actually trying to do what AnnotationToBeanMetaDataFactory is meant for.

          • 2. Re: What is the correct time to invoke the getProperties on
            jaikiran

             

            "alesj" wrote:
            The builder just builds metadata.
            Annotation's metadata population happens only in Describe state,
            as we know there that our CL exists == can do class info inspection.


            Makes sense.

            "alesj" wrote:

            I looks like you're actually trying to do what AnnotationToBeanMetaDataFactory is meant for.


            After looking at the AnnotationToBeanMetadatFactory, i don't think that's what i am after. So to give you an exact idea of what i am trying to do:

            I am dynamically building a bean MyBean which needs another bean MyOtherBean to be injected in it:

            public class MyBean
            {
            
             // Inject information will be dynamically added, including the bean name
             // to be injected
             @Inject
             private MyOtherBean otherBean;
            ...
            }
            


            The easy way to dynamically build this bean and add the injection attributes is this:
            MyBean mybean = new MyBean();
            BeanMetaDataBuilder builder = BeanMetaDataBuilder.createBuilder("blah", mybean.getClass().getName());
            builder.setConstructorValue(mybean);
            
            // create the inject attributes
            ValueMetaData inject = builder.createInject("beanNameOftheDependency", null, null, ControllerState.DESCRIBED);
            builder.addPropertyMetaData("otherBean", inject);


            Works fine. However, the thing that i want to avoid is this line:

            builder.addPropertyMetaData("otherBean", inject)


            I was trying to avoid adding the inject/depends on a "field name" and instead was trying to see if i could create the metadata based on the "type" to inject. Something like:

            builder.addPropertyMetadata(MyOtherBean.class.getName(),inject);


            The MC would then look for a field of type MyOtherBean and create the injection on MyBean.

            So if tomorrow, someone dislikes the field name and changes it to:

            public class MyBean
            {
            
             // Inject information will be dynamically added, including the bean name
             // to be injected
             @Inject
             private MyOtherBean iDontLikeTheEarlierName;
            ...
            }


            then i could still have this entire thing working.

            Maybe MC already has something to achieve this and i am not finding the correct API?

            • 3. Re: What is the correct time to invoke the getProperties on
              alesj

               

              "jaikiran" wrote:

              Maybe MC already has something to achieve this and i am not finding the correct API?

              BeanMetaDataBuilder::createContextualInject is the closest you can get.

              But your example is different.
              Current contextual injection would still need the property name,
              it just wouldn't care about what exact bean maps to that property,
              it would be determined via type matching.

              In your case you would have to "examine" the bean/class,
              to find the actual property/field name,
              as I don't see how else MC's IoC could know where to inject the matching bean.

              But this is "weird" custom behavior. :-)

              • 4. Re: What is the correct time to invoke the getProperties on
                jaikiran

                 

                "alesj" wrote:

                But this is "weird" custom behavior. :-)


                Custom - yes. Weird - maybe not :-)
                But i'll think a bit more to see if whatever i am trying to do is worth the efforts :)

                • 5. Re: What is the correct time to invoke the getProperties on
                  alesj

                   

                  "jaikiran" wrote:

                  Weird - maybe not :-)

                  If you bother to change the field name,
                  I think you can bother to change it as well in MC's xml descriptor. ;-)

                  • 6. Re: What is the correct time to invoke the getProperties on
                    jaikiran

                     

                    "alesj" wrote:


                    .. as well in MC's xml descriptor. ;-)


                    That's the whole point, this is a dynamic MC bean created on the fly with no xml configuration :)

                    • 7. Re: What is the correct time to invoke the getProperties on
                      alesj

                       

                      "jaikiran" wrote:

                      That's the whole point, this is a dynamic MC bean created on the fly with no xml configuration :)

                      OK, I still don't consider this a valid use case to make MC changes. :-)
                      But keeps us informed on how you achieved this. ;-)