5 Replies Latest reply on Feb 13, 2013 4:50 AM by alesj

    Howto Inject a DynamicProxy

    antoen

      Howto Inject a DynamicProxy?

      Dear weld community,

       

      I am using JBOSS 7.1.1.Final and have the following Problem. The users of our API provide an interface with some annotations. Our implementation now builds a dynamicProxy with an invoationHandler for this interface and provides the user with an implementation. Is it possible to provide the users of our API with some CDI / WELD miracles, so that they can use injection to obtain the proxy?

      actual Example using the method "buildObject" deep within a factory: deep in our API:

       

      public <T> T buildObject(final Class<T> aInterface) 
        { 
          final T impl = 
              (T) java.lang.reflect.Proxy.newProxyInstance( 
                  aInterface.getClassLoader(), 
                  new Class[] { aInterface }, 
                  new PropertiesProxyInvocationHandler(someConfiguration)); 
          return impl; 
        }
      

       

      Code of the user of our API:

           
      public interface SomeInterface 
      { 
          @Fetch("http://www.example.com/api") 
          void doSomething(); 
       
          @Store("http://www.example.com/api") 
          void doSomethingElse(); 
      } 
      
                
      @Stateless public class MyService 
      {    
           public void execute()    
           {        
                final ApiFactory factory = ApiFactoryFactory.createDefaultFactory();        
                final ApiManagement management = factory.createDefaultManagement();
                final SomeInterface myImpl = management.getImplementation(SomeInterface.class);
                myImpl.doSomething();
           } 
      } 
       

            I would like to reduce the boilerplate for the user of our api with something like that:

           
      @Stateless public class MyService 
      {    
           @Inject final SomeInterface myImpl; 
       
          public void execute()
          {
               myImpl.doSomething()
          }
      } 
      

       

      Is there a CDI Extension / Weld Extension that can be used to inject a DynamicProxy for an interface?

       

      Any help is highly welcome.

       

      Best regards,

       

      Anton

        • 1. Re: Howto Inject a DynamicProxy
          alesj

          What about simply using @Produces?

          • 2. Re: Howto Inject a DynamicProxy
            antoen

            I fear its not that easy. (Or I do not fully understand your answer)

             

            I (provider of the api) do not know the interface the user of the api will write.

            So I can not provide a producer for the user.

             

            The user could write a producer, but he would have to write one for each interface (with annotations of our api) he is going to create.

            And he would have to write the boilerplate in that producer. But thats what I want to get rid of.

             

            Best regards,

             

            Anton

             

             

            • 3. Re: Howto Inject a DynamicProxy
              alesj

              Hmmm, true.

               

              Unless you do extra scanning, to know which interrfaces are missing its matching CDI beans, imo, it cannot work.

               

              I guess you could write an extension which would track injecton points,

              and add Bean for each missing interface with @Inject.

              Where this bean would be your generic proxy.

              • 4. Re: Howto Inject a DynamicProxy
                antoen

                Hi Justin,

                 

                thanks for your reply, but thats exactly the question I have.

                But I would need some help to solve this.

                 

                When I get it right you suggest two possible solutions:

                 

                Solution 1) Extra scanning

                Do some extra scanning and create the missing beans on the fly.

                 

                But I dont know how to trigger the extra scanning, so that I am not too late creating the beans so that weld will register them.

                Btw for non CDI Platforms we already do this extra scanning, but we do it lazy and I dont know where to hook into weld.

                Or is ther no too late and I can register this (handmade) beans manually with the BeansManager? (But triggered by which event?)

                 

                Solution 2) Tracking injection points

                Here I have pretty much the same problems. I dont know where to hook into WELD to

                a) track the injection points

                b) register the handmade beans

                 

                Do you have any hint where I could start with?

                 

                Best regards,

                 

                Anton

                • 5. Re: Howto Inject a DynamicProxy
                  alesj

                  Do you have any hint where I could start with?

                  By reading the docs / manual on CDI extensions? ;-)

                   

                  Once you do that, we can get back to more details,

                  if you still don't see where I'm aiming at.