Search in sources :

Example 6 with FitInjection

use of com.netflix.titus.common.framework.fit.FitInjection in project titus-control-plane by Netflix.

the class FitInvocationHandler method newProxy.

public static <I> I newProxy(Object delegate, FitInjection injection) {
    List<Class<?>> interfaces = ReflectionExt.findAllInterfaces(delegate.getClass());
    Preconditions.checkArgument(interfaces.size() == 1, "%s expected to implement exactly one interface", delegate.getClass().getName());
    Class<?> interf = interfaces.get(0);
    Map<Method, Function<Object[], Object>> handlers = new HashMap<>();
    for (Method method : interf.getMethods()) {
        Class<?> returnType = method.getReturnType();
        if (returnType.isAssignableFrom(CompletableFuture.class)) {
            handlers.put(method, args -> handleCompletableFuture(injection, method, delegate, args));
        } else if (returnType.isAssignableFrom(ListenableFuture.class)) {
            handlers.put(method, args -> handleListenableFuture(injection, method, delegate, args));
        } else if (returnType.isAssignableFrom(Observable.class)) {
            handlers.put(method, args -> handleObservable(injection, method, delegate, args));
        } else {
            handlers.put(method, args -> handleSynchronous(injection, method, delegate, args));
        }
    }
    return (I) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { interf }, new FitInvocationHandler(delegate, handlers));
}
Also used : Proxy(java.lang.reflect.Proxy) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) FitInjection(com.netflix.titus.common.framework.fit.FitInjection) ReflectionExt(com.netflix.titus.common.util.ReflectionExt) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) InvocationTargetException(java.lang.reflect.InvocationTargetException) Observable(rx.Observable) List(java.util.List) Map(java.util.Map) Preconditions(com.google.common.base.Preconditions) InvocationHandler(java.lang.reflect.InvocationHandler) Method(java.lang.reflect.Method) Function(java.util.function.Function) HashMap(java.util.HashMap) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Method(java.lang.reflect.Method)

Aggregations

FitInjection (com.netflix.titus.common.framework.fit.FitInjection)6 FitAction (com.netflix.titus.common.framework.fit.FitAction)2 FitComponent (com.netflix.titus.common.framework.fit.FitComponent)2 List (java.util.List)2 Path (javax.ws.rs.Path)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Preconditions (com.google.common.base.Preconditions)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 FitFramework (com.netflix.titus.common.framework.fit.FitFramework)1 FitLatencyAction (com.netflix.titus.common.framework.fit.internal.action.FitLatencyAction)1 TitusRuntime (com.netflix.titus.common.runtime.TitusRuntime)1 CollectionsExt (com.netflix.titus.common.util.CollectionsExt)1 ExceptionExt (com.netflix.titus.common.util.ExceptionExt)1 ReflectionExt (com.netflix.titus.common.util.ReflectionExt)1 Metadata (io.grpc.Metadata)1 ServerCall (io.grpc.ServerCall)1 ServerCallHandler (io.grpc.ServerCallHandler)1 ServerInterceptor (io.grpc.ServerInterceptor)1 Status (io.grpc.Status)1 InvocationHandler (java.lang.reflect.InvocationHandler)1