Dynamic Proxies

satya - Monday, September 15, 2008 11:15:41 AM

Dynamic Proxies

Search for: Dynamic Proxies

satya - Monday, September 15, 2008 11:16:20 AM

Java theory and practice: Decorating with dynamic proxies - Brian Goetz

Java theory and practice: Decorating with dynamic proxies - Brian Goetz

satya - Monday, September 15, 2008 11:18:10 AM

Java Dynamic Proxies: One Step from Aspect-oriented Programming - Lara D'bero

Java Dynamic Proxies: One Step from Aspect-oriented Programming - Lara D'bero

satya - 8/29/2014 2:27:10 PM

Java 7 dynamic proxy api

Java 7 dynamic proxy api

satya - 8/29/2014 3:44:07 PM

Here is the general idea


//Consider
I1: Interface1
I2: Interface2
I3: Interface3

Object MainProxy = SomeProxyFor(I1, I2, I3) and Call(Handler);
I1 i1Proxy = (I1)MainProxy;
I2 i2Proxy = (I2)MainProxy;

Handler
{
   invoke(method) { figure out who to call and call; }
}
etc.

//I am thinking this is possible

satya - 8/29/2014 3:45:03 PM

Can you figure out which interface a method is invoked on in a java dynamic proxy?

Can you figure out which interface a method is invoked on in a java dynamic proxy?

Search for: Can you figure out which interface a method is invoked on in a java dynamic proxy?

satya - 8/29/2014 3:48:19 PM

Understand JavaAssist

Understand JavaAssist

satya - 8/29/2014 3:48:32 PM

JavaAssist and Android

JavaAssist and Android

Search for: JavaAssist and Android

satya - 8/29/2014 3:50:23 PM

An introductory article to a number of things around code injection and aspects

An introductory article to a number of things around code injection and aspects

satya - 8/30/2014 10:19:53 AM

Here is an example of creating a proxy


/**
 * 
 * Allow a name space for clients to discover various services
 * Usage:
 *   Services.persistenceServices.bookps.addBook(); etc.
 * Dynamic proxy will take care of transactions.
 * Dynamic proxy will take care of mock data.
 * Dynamic Proxy will allow more than one interface 
 *   to apply the above aspects. 
 */
public class Services 
{
   public static PersistenceServices persistenceServices = new PersistenceServices();
   public static class PersistenceServices   {
      ////se this pointer during initialization
      public static IBookPS bookps = null; 
   }
   
   private static Object mainProxy;
   static 
   {
      //set up bookps
      ClassLoader cl = IBookPS.class.getClassLoader();
      //Add more interfaces as available
      Class[] variousServiceInterfaces = new Class[] { IBookPS.class };
      
      //Create a big object that can proxy all the related interfaces
      //for which similar common aspects are applied
      //In this cases it is android sqllite transactions
      mainProxy = 
         Proxy.newProxyInstance(cl, 
               variousServiceInterfaces, 
               new TestInterceptor());
      
      //Preset the namespace for easy discovery
      PersistenceServices.bookps = (IBookPS)mainProxy;
   }
}

satya - 8/30/2014 10:21:18 AM

Here is the handler code


/**
 * A test interceptor for multiple interfaces
 * Tests to see how to route a method call
 * to a suitable implementation
 * 
 *  Eventually will be used for Android SQLite 
 *  transaction support.
 *  
 */
public class TestInterceptor implements InvocationHandler
{
   private BookPSSQLite bookServiceImpl;
   TestInterceptor()
   {
      this.bookServiceImpl = new BookPSSQLite();
   }
   public Object invoke(Object proxy, Method method, Object[] args)
         throws Throwable {
      logMethodSignature(method);
      return null;
   }
   
   private void logMethodSignature(Method method)
   {
      String interfaceName = method.getDeclaringClass().getName();
      String mname = method.getName();
      System.out.println(String.format("%s : %s", interfaceName, mname));
   }
}

satya - 8/30/2014 11:22:22 AM

This is a better implementation for the services


/**
 * 
 * Allow a name space for clients to discover various services
 * Usage:
 *   Services.PersistenceServices.bookps.addBook(); etc.
 * Dynamic proxy will take care of transactions.
 * Dynamic proxy will take care of mock data.
 * Dynamic Proxy will allow more than one interface 
 *   to apply the above aspects. 
 */
public class Services 
{
   private static Object mainProxy;

   //A psuedo trick to kick off initializing this static class
   //All of the code in static blocks will run
   static void init(){}

   static  
   {
      //set up bookps
      ClassLoader cl = IBookPS.class.getClassLoader();
      //Add more interfaces as available
      Class[] variousServiceInterfaces = new Class[] { IBookPS.class };
      
      //Create a big object that can proxy all the related interfaces
      //for which similar common aspects are applied
      //In this cases it is android sqllite transactions
      mainProxy = 
         Proxy.newProxyInstance(cl, 
               variousServiceInterfaces, 
               new TestInterceptor());
      
      //Preset the namespace for easy discovery
      PersistenceServices.bookps = (IBookPS)mainProxy;
   }
   
   public static class PersistenceServices   {
      ////se this pointer during initialization
      public static IBookPS bookps = null;
      
      //Make sure things are initializd;
      static {Services.init();}

   }
}

satya - 8/30/2014 11:25:45 AM

You will also need thread locals to stuff the SQliteDatabase db that can be used later

In this approach the proxy will get the db and put it on the current running thread without explicitly passing that variable to the functions downstream. Code is cleaner and less error prone.

satya - 8/30/2014 11:27:16 AM

Here is some research on thread locals

Here is some research on thread locals