|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +package com.microsoft.durabletask; |
| 4 | + |
| 5 | +import javax.annotation.Nonnull; |
| 6 | +import java.lang.reflect.InvocationHandler; |
| 7 | +import java.lang.reflect.Method; |
| 8 | +import java.lang.reflect.ParameterizedType; |
| 9 | +import java.lang.reflect.Proxy; |
| 10 | +import java.lang.reflect.Type; |
| 11 | + |
| 12 | +/** |
| 13 | + * Creates type-safe proxies for interacting with durable entities from orchestrations. |
| 14 | + * <p> |
| 15 | + * A typed entity proxy is a JDK dynamic proxy that implements a user-defined interface. |
| 16 | + * Method calls on the proxy are translated into entity operations: |
| 17 | + * <ul> |
| 18 | + * <li>{@code void} methods become fire-and-forget signals via |
| 19 | + * {@link TaskOrchestrationContext#signalEntity}</li> |
| 20 | + * <li>Methods returning {@link Task}{@code <V>} become two-way calls via |
| 21 | + * {@link TaskOrchestrationContext#callEntity}</li> |
| 22 | + * </ul> |
| 23 | + * <p> |
| 24 | + * The method name is used as the entity operation name (case-insensitive matching on the |
| 25 | + * entity side). Methods must accept 0 or 1 parameters; the single parameter is passed as |
| 26 | + * the operation input. |
| 27 | + * |
| 28 | + * <p>Example: |
| 29 | + * <pre>{@code |
| 30 | + * // Define entity operations as an interface |
| 31 | + * public interface ICounter { |
| 32 | + * void add(int amount); // fire-and-forget signal |
| 33 | + * void reset(); // fire-and-forget signal |
| 34 | + * Task<Integer> get(); // two-way call returning a result |
| 35 | + * } |
| 36 | + * |
| 37 | + * // Use in an orchestration |
| 38 | + * ICounter counter = ctx.createEntityProxy(entityId, ICounter.class); |
| 39 | + * counter.add(5); |
| 40 | + * counter.reset(); |
| 41 | + * int value = counter.get().await(); |
| 42 | + * }</pre> |
| 43 | + * |
| 44 | + * @see TaskOrchestrationContext#createEntityProxy(EntityInstanceId, Class) |
| 45 | + * @see TaskOrchestrationEntityFeature#createProxy(EntityInstanceId, Class) |
| 46 | + */ |
| 47 | +public final class EntityProxy { |
| 48 | + |
| 49 | + private EntityProxy() { |
| 50 | + // Utility class — not instantiable |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Creates a typed entity proxy for the given entity instance. |
| 55 | + * |
| 56 | + * @param ctx the orchestration context (used to send signals and calls) |
| 57 | + * @param entityId the target entity's instance ID |
| 58 | + * @param proxyInterface the interface whose methods map to entity operations |
| 59 | + * @param <T> the proxy interface type |
| 60 | + * @return a proxy instance that implements {@code proxyInterface} |
| 61 | + * @throws IllegalArgumentException if {@code proxyInterface} is not an interface |
| 62 | + */ |
| 63 | + @SuppressWarnings("unchecked") |
| 64 | + public static <T> T create( |
| 65 | + @Nonnull TaskOrchestrationContext ctx, |
| 66 | + @Nonnull EntityInstanceId entityId, |
| 67 | + @Nonnull Class<T> proxyInterface) { |
| 68 | + if (ctx == null) { |
| 69 | + throw new IllegalArgumentException("ctx must not be null"); |
| 70 | + } |
| 71 | + if (entityId == null) { |
| 72 | + throw new IllegalArgumentException("entityId must not be null"); |
| 73 | + } |
| 74 | + if (proxyInterface == null) { |
| 75 | + throw new IllegalArgumentException("proxyInterface must not be null"); |
| 76 | + } |
| 77 | + if (!proxyInterface.isInterface()) { |
| 78 | + throw new IllegalArgumentException( |
| 79 | + "proxyInterface must be an interface, got: " + proxyInterface.getName()); |
| 80 | + } |
| 81 | + |
| 82 | + return (T) Proxy.newProxyInstance( |
| 83 | + proxyInterface.getClassLoader(), |
| 84 | + new Class<?>[]{ proxyInterface }, |
| 85 | + new EntityInvocationHandler(ctx, entityId)); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Invocation handler that translates interface method calls into entity operations. |
| 90 | + */ |
| 91 | + private static final class EntityInvocationHandler implements InvocationHandler { |
| 92 | + private final TaskOrchestrationContext ctx; |
| 93 | + private final EntityInstanceId entityId; |
| 94 | + |
| 95 | + EntityInvocationHandler(TaskOrchestrationContext ctx, EntityInstanceId entityId) { |
| 96 | + this.ctx = ctx; |
| 97 | + this.entityId = entityId; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 102 | + // Handle java.lang.Object methods |
| 103 | + if (method.getDeclaringClass() == Object.class) { |
| 104 | + switch (method.getName()) { |
| 105 | + case "toString": |
| 106 | + return "EntityProxy[" + entityId + "]"; |
| 107 | + case "hashCode": |
| 108 | + return entityId.hashCode(); |
| 109 | + case "equals": |
| 110 | + if (args[0] == proxy) { |
| 111 | + return true; |
| 112 | + } |
| 113 | + if (args[0] == null || !Proxy.isProxyClass(args[0].getClass())) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + InvocationHandler otherHandler = Proxy.getInvocationHandler(args[0]); |
| 117 | + if (otherHandler instanceof EntityInvocationHandler) { |
| 118 | + return entityId.equals(((EntityInvocationHandler) otherHandler).entityId); |
| 119 | + } |
| 120 | + return false; |
| 121 | + default: |
| 122 | + return method.invoke(this, args); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + String operationName = method.getName(); |
| 127 | + Object input = (args != null && args.length == 1) ? args[0] : null; |
| 128 | + |
| 129 | + if (args != null && args.length > 1) { |
| 130 | + throw new UnsupportedOperationException( |
| 131 | + "Entity proxy methods must have 0 or 1 parameters. " + |
| 132 | + "Method '" + operationName + "' has " + args.length + " parameters. " + |
| 133 | + "Use a single wrapper object to pass multiple values."); |
| 134 | + } |
| 135 | + |
| 136 | + Class<?> returnType = method.getReturnType(); |
| 137 | + |
| 138 | + if (returnType == void.class) { |
| 139 | + // Fire-and-forget signal |
| 140 | + ctx.signalEntity(entityId, operationName, input); |
| 141 | + return null; |
| 142 | + } else if (Task.class.isAssignableFrom(returnType)) { |
| 143 | + // Two-way entity call — extract the Task<V> type parameter |
| 144 | + Class<?> resultType = extractTaskTypeParameter(method); |
| 145 | + return ctx.callEntity(entityId, operationName, input, resultType); |
| 146 | + } else { |
| 147 | + throw new UnsupportedOperationException( |
| 148 | + "Entity proxy methods must return void (for signals) or Task<V> (for calls). " + |
| 149 | + "Method '" + operationName + "' returns " + returnType.getName() + "."); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Extracts the generic type parameter from a method returning {@code Task<V>}. |
| 155 | + * Falls back to {@code Void.class} if the type cannot be determined. |
| 156 | + */ |
| 157 | + private static Class<?> extractTaskTypeParameter(Method method) { |
| 158 | + Type genericReturnType = method.getGenericReturnType(); |
| 159 | + if (genericReturnType instanceof ParameterizedType) { |
| 160 | + ParameterizedType pt = (ParameterizedType) genericReturnType; |
| 161 | + Type[] typeArgs = pt.getActualTypeArguments(); |
| 162 | + if (typeArgs.length > 0) { |
| 163 | + return getRawClass(typeArgs[0]); |
| 164 | + } |
| 165 | + } |
| 166 | + return Void.class; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Resolves a {@link Type} to its raw {@link Class}, handling parameterized types |
| 171 | + * and wildcard types. |
| 172 | + */ |
| 173 | + private static Class<?> getRawClass(Type type) { |
| 174 | + if (type instanceof Class) { |
| 175 | + return (Class<?>) type; |
| 176 | + } else if (type instanceof ParameterizedType) { |
| 177 | + return getRawClass(((ParameterizedType) type).getRawType()); |
| 178 | + } |
| 179 | + return Object.class; |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments