All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snapscript.platform.ThreadLocalHandler Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.platform;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import org.snapscript.core.platform.Bridge;
import org.snapscript.platform.generate.BridgeInstance;

public class ThreadLocalHandler implements InvocationHandler {
   
   private final ThreadLocal local;
   private final InvocationRouter router;
   
   public ThreadLocalHandler(ThreadLocal local, InvocationRouter router) {
      this.router = router;
      this.local = local;
   }

   @Override
   public Object invoke(Object proxy, Method method, Object[] list) throws Throwable {
      Bridge bridge = (Bridge)proxy;
      BridgeInstance instance = (BridgeInstance)bridge.getInstance();
      
      if(instance == null) {
         instance = local.get(); // thread local is set first
      
         if(instance == null) {
            throw new IllegalStateException("Object has not been constructed");
         }
         bridge.setInstance(instance); // now we knot its set
      }
      instance.getHolder().setBridge(bridge);
      return router.route(bridge, method, list);
   }
   
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy