|
| 1 | +package com.guicedee.client.scopes.mutiny; |
| 2 | + |
| 3 | +import com.google.inject.Key; |
| 4 | +import com.guicedee.client.IGuiceContext; |
| 5 | +import com.guicedee.client.scopes.CallScopeProperties; |
| 6 | +import com.guicedee.client.scopes.CallScoper; |
| 7 | +import io.smallrye.mutiny.Uni; |
| 8 | +import io.smallrye.mutiny.infrastructure.UniInterceptor; |
| 9 | +import io.smallrye.mutiny.operators.AbstractUni; |
| 10 | +import io.smallrye.mutiny.subscription.UniSubscriber; |
| 11 | +import io.smallrye.mutiny.subscription.UniSubscription; |
| 12 | + |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 17 | + |
| 18 | +public class CallScopeUniInterceptor |
| 19 | + implements UniInterceptor |
| 20 | +{ |
| 21 | + private static final Key<CallScopeProperties> CALL_SCOPE_PROPERTIES_KEY = Key.get(CallScopeProperties.class); |
| 22 | + |
| 23 | + @Override |
| 24 | + public <T> Uni<T> onUniCreation(Uni<T> uni) |
| 25 | + { |
| 26 | + // Avoid re-wrapping if this interceptor already produced the Uni instance |
| 27 | + if (uni instanceof CallScopeAwareUni) |
| 28 | + { |
| 29 | + return uni; |
| 30 | + } |
| 31 | + CallScoper callScoper = IGuiceContext.get(CallScoper.class); |
| 32 | + if (callScoper.isStartedScope()) |
| 33 | + { |
| 34 | + recordTouch(callScoper, "uni-creation", captureLocation()); |
| 35 | + } |
| 36 | + Map<Key<?>, Object> snapshot = captureCallScope(callScoper); |
| 37 | + return new CallScopeAwareUni<>(uni, snapshot); |
| 38 | + } |
| 39 | + |
| 40 | + private Map<Key<?>, Object> captureCallScope(CallScoper callScoper) |
| 41 | + { |
| 42 | + if (!callScoper.isStartedScope()) |
| 43 | + { |
| 44 | + return Collections.emptyMap(); |
| 45 | + } |
| 46 | + Map<Key<?>, Object> values = callScoper.getValues(); |
| 47 | + if (values == null || values.isEmpty()) |
| 48 | + { |
| 49 | + return Collections.emptyMap(); |
| 50 | + } |
| 51 | + return new HashMap<>(values); |
| 52 | + } |
| 53 | + |
| 54 | + private static final class CallScopeAwareUni<T> extends AbstractUni<T> |
| 55 | + { |
| 56 | + private final Uni<T> upstream; |
| 57 | + private final Map<Key<?>, Object> capturedValues; |
| 58 | + |
| 59 | + private CallScopeAwareUni(Uni<T> upstream, Map<Key<?>, Object> capturedValues) |
| 60 | + { |
| 61 | + this.upstream = upstream; |
| 62 | + this.capturedValues = capturedValues.isEmpty() ? Collections.emptyMap() : capturedValues; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void subscribe(UniSubscriber<? super T> subscriber) |
| 67 | + { |
| 68 | + CallScoper callScoper = IGuiceContext.get(CallScoper.class); |
| 69 | + boolean startedHere = false; |
| 70 | + |
| 71 | + if (!callScoper.isStartedScope()) |
| 72 | + { |
| 73 | + callScoper.enter(); |
| 74 | + startedHere = true; |
| 75 | + if (!capturedValues.isEmpty()) |
| 76 | + { |
| 77 | + callScoper.setValues(capturedValues); |
| 78 | + } |
| 79 | + recordTouch(callScoper, "uni-bounce", captureLocation()); |
| 80 | + } |
| 81 | + |
| 82 | + ScopedUniSubscriber<T> scopedSubscriber = new ScopedUniSubscriber<>(subscriber, callScoper, startedHere); |
| 83 | + try |
| 84 | + { |
| 85 | + recordTouch(callScoper, startedHere ? "uni-bounce" : "uni-subscribe", captureLocation()); |
| 86 | + AbstractUni.subscribe(upstream, scopedSubscriber); |
| 87 | + } |
| 88 | + catch (Throwable t) |
| 89 | + { |
| 90 | + if (startedHere) |
| 91 | + { |
| 92 | + safeExit(callScoper); |
| 93 | + } |
| 94 | + throw t; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static final class ScopedUniSubscriber<T> implements UniSubscriber<T> |
| 100 | + { |
| 101 | + private final UniSubscriber<? super T> delegate; |
| 102 | + private final CallScoper callScoper; |
| 103 | + private final boolean startedScope; |
| 104 | + private final AtomicBoolean ended = new AtomicBoolean(false); |
| 105 | + |
| 106 | + private ScopedUniSubscriber(UniSubscriber<? super T> delegate, CallScoper callScoper, boolean startedScope) |
| 107 | + { |
| 108 | + this.delegate = delegate; |
| 109 | + this.callScoper = callScoper; |
| 110 | + this.startedScope = startedScope; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void onSubscribe(UniSubscription subscription) |
| 115 | + { |
| 116 | + UniSubscription wrapped = startedScope ? new ScopedUniSubscription(subscription, this::endScope) : subscription; |
| 117 | + try |
| 118 | + { |
| 119 | + delegate.onSubscribe(wrapped); |
| 120 | + } |
| 121 | + catch (Throwable t) |
| 122 | + { |
| 123 | + endScope(); |
| 124 | + throw t; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void onItem(T item) |
| 130 | + { |
| 131 | + try |
| 132 | + { |
| 133 | + delegate.onItem(item); |
| 134 | + } |
| 135 | + finally |
| 136 | + { |
| 137 | + endScope(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void onFailure(Throwable failure) |
| 143 | + { |
| 144 | + try |
| 145 | + { |
| 146 | + delegate.onFailure(failure); |
| 147 | + } |
| 148 | + finally |
| 149 | + { |
| 150 | + endScope(); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private void endScope() |
| 155 | + { |
| 156 | + if (startedScope && ended.compareAndSet(false, true)) |
| 157 | + { |
| 158 | + safeExit(callScoper); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private static final class ScopedUniSubscription implements UniSubscription |
| 164 | + { |
| 165 | + private final UniSubscription delegate; |
| 166 | + private final Runnable onCancel; |
| 167 | + |
| 168 | + private ScopedUniSubscription(UniSubscription delegate, Runnable onCancel) |
| 169 | + { |
| 170 | + this.delegate = delegate; |
| 171 | + this.onCancel = onCancel; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public void request(long n) |
| 176 | + { |
| 177 | + delegate.request(n); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void cancel() |
| 182 | + { |
| 183 | + try |
| 184 | + { |
| 185 | + delegate.cancel(); |
| 186 | + } |
| 187 | + finally |
| 188 | + { |
| 189 | + onCancel.run(); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + private static void safeExit(CallScoper callScoper) |
| 195 | + { |
| 196 | + try |
| 197 | + { |
| 198 | + callScoper.exit(); |
| 199 | + } |
| 200 | + catch (IllegalStateException ignored) |
| 201 | + { |
| 202 | + // Scope already exited |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + private static void recordTouch(CallScoper callScoper, String reason, String location) |
| 207 | + { |
| 208 | + CallScopeProperties properties = getCallScopeProperties(callScoper); |
| 209 | + if (properties != null) |
| 210 | + { |
| 211 | + properties.getTouches().add(reason + "@" + location); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + private static CallScopeProperties getCallScopeProperties(CallScoper callScoper) |
| 216 | + { |
| 217 | + Map<Key<?>, Object> values = callScoper.getValues(); |
| 218 | + if (values == null) |
| 219 | + { |
| 220 | + return null; |
| 221 | + } |
| 222 | + Object properties = values.get(CALL_SCOPE_PROPERTIES_KEY); |
| 223 | + if (properties instanceof CallScopeProperties) |
| 224 | + { |
| 225 | + return (CallScopeProperties) properties; |
| 226 | + } |
| 227 | + return null; |
| 228 | + } |
| 229 | + |
| 230 | + private static String captureLocation() |
| 231 | + { |
| 232 | + return StackWalker |
| 233 | + .getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE) |
| 234 | + .walk(stream -> stream |
| 235 | + .filter(f -> !f.getClassName().startsWith("io.smallrye.mutiny")) |
| 236 | + .filter(f -> !f.getClassName().equals(CallScopeUniInterceptor.class.getName())) |
| 237 | + .findFirst() |
| 238 | + .map(f -> f.getClassName() + "#" + f.getMethodName() + ":" + f.getLineNumber()) |
| 239 | + .orElse("unknown")); |
| 240 | + } |
| 241 | +} |
0 commit comments