|
| 1 | +package io.sentry; |
| 2 | + |
| 3 | +import io.sentry.protocol.App; |
| 4 | +import io.sentry.protocol.Browser; |
| 5 | +import io.sentry.protocol.Contexts; |
| 6 | +import io.sentry.protocol.Device; |
| 7 | +import io.sentry.protocol.Gpu; |
| 8 | +import io.sentry.protocol.OperatingSystem; |
| 9 | +import io.sentry.protocol.Response; |
| 10 | +import io.sentry.protocol.SentryRuntime; |
| 11 | +import io.sentry.util.HintUtils; |
| 12 | +import java.io.IOException; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | +import org.jetbrains.annotations.Nullable; |
| 15 | + |
| 16 | +public final class CombinedContextsView extends Contexts { |
| 17 | + |
| 18 | + private static final long serialVersionUID = 3585992094653318439L; |
| 19 | + private final @NotNull Contexts globalContexts; |
| 20 | + private final @NotNull Contexts isolationContexts; |
| 21 | + private final @NotNull Contexts currentContexts; |
| 22 | + |
| 23 | + private final @NotNull ScopeType defaultScopeType; |
| 24 | + |
| 25 | + public CombinedContextsView( |
| 26 | + final @NotNull Contexts globalContexts, |
| 27 | + final @NotNull Contexts isolationContexts, |
| 28 | + final @NotNull Contexts currentContexts, |
| 29 | + final @NotNull ScopeType defaultScopeType) { |
| 30 | + this.globalContexts = globalContexts; |
| 31 | + this.isolationContexts = isolationContexts; |
| 32 | + this.currentContexts = currentContexts; |
| 33 | + this.defaultScopeType = defaultScopeType; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public @Nullable SpanContext getTrace() { |
| 38 | + final @Nullable SpanContext current = currentContexts.getTrace(); |
| 39 | + if (current != null) { |
| 40 | + return current; |
| 41 | + } |
| 42 | + final @Nullable SpanContext isolation = isolationContexts.getTrace(); |
| 43 | + if (isolation != null) { |
| 44 | + return isolation; |
| 45 | + } |
| 46 | + return globalContexts.getTrace(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void setTrace(@Nullable SpanContext traceContext) { |
| 51 | + getDefaultContexts().setTrace(traceContext); |
| 52 | + } |
| 53 | + |
| 54 | + private Contexts getDefaultContexts() { |
| 55 | + switch (defaultScopeType) { |
| 56 | + case CURRENT: |
| 57 | + return currentContexts; |
| 58 | + case ISOLATION: |
| 59 | + return isolationContexts; |
| 60 | + case GLOBAL: |
| 61 | + return globalContexts; |
| 62 | + default: |
| 63 | + return currentContexts; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public @Nullable App getApp() { |
| 69 | + final @Nullable App current = currentContexts.getApp(); |
| 70 | + if (current != null) { |
| 71 | + return current; |
| 72 | + } |
| 73 | + final @Nullable App isolation = isolationContexts.getApp(); |
| 74 | + if (isolation != null) { |
| 75 | + return isolation; |
| 76 | + } |
| 77 | + return globalContexts.getApp(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void setApp(@NotNull App app) { |
| 82 | + getDefaultContexts().setApp(app); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public @Nullable Browser getBrowser() { |
| 87 | + final @Nullable Browser current = currentContexts.getBrowser(); |
| 88 | + if (current != null) { |
| 89 | + return current; |
| 90 | + } |
| 91 | + final @Nullable Browser isolation = isolationContexts.getBrowser(); |
| 92 | + if (isolation != null) { |
| 93 | + return isolation; |
| 94 | + } |
| 95 | + return globalContexts.getBrowser(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void setBrowser(@NotNull Browser browser) { |
| 100 | + getDefaultContexts().setBrowser(browser); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public @Nullable Device getDevice() { |
| 105 | + final @Nullable Device current = currentContexts.getDevice(); |
| 106 | + if (current != null) { |
| 107 | + return current; |
| 108 | + } |
| 109 | + final @Nullable Device isolation = isolationContexts.getDevice(); |
| 110 | + if (isolation != null) { |
| 111 | + return isolation; |
| 112 | + } |
| 113 | + return globalContexts.getDevice(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void setDevice(@NotNull Device device) { |
| 118 | + getDefaultContexts().setDevice(device); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public @Nullable OperatingSystem getOperatingSystem() { |
| 123 | + final @Nullable OperatingSystem current = currentContexts.getOperatingSystem(); |
| 124 | + if (current != null) { |
| 125 | + return current; |
| 126 | + } |
| 127 | + final @Nullable OperatingSystem isolation = isolationContexts.getOperatingSystem(); |
| 128 | + if (isolation != null) { |
| 129 | + return isolation; |
| 130 | + } |
| 131 | + return globalContexts.getOperatingSystem(); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public void setOperatingSystem(@NotNull OperatingSystem operatingSystem) { |
| 136 | + getDefaultContexts().setOperatingSystem(operatingSystem); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public @Nullable SentryRuntime getRuntime() { |
| 141 | + final @Nullable SentryRuntime current = currentContexts.getRuntime(); |
| 142 | + if (current != null) { |
| 143 | + return current; |
| 144 | + } |
| 145 | + final @Nullable SentryRuntime isolation = isolationContexts.getRuntime(); |
| 146 | + if (isolation != null) { |
| 147 | + return isolation; |
| 148 | + } |
| 149 | + return globalContexts.getRuntime(); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void setRuntime(@NotNull SentryRuntime runtime) { |
| 154 | + getDefaultContexts().setRuntime(runtime); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public @Nullable Gpu getGpu() { |
| 159 | + final @Nullable Gpu current = currentContexts.getGpu(); |
| 160 | + if (current != null) { |
| 161 | + return current; |
| 162 | + } |
| 163 | + final @Nullable Gpu isolation = isolationContexts.getGpu(); |
| 164 | + if (isolation != null) { |
| 165 | + return isolation; |
| 166 | + } |
| 167 | + return globalContexts.getGpu(); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void setGpu(@NotNull Gpu gpu) { |
| 172 | + getDefaultContexts().setGpu(gpu); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public @Nullable Response getResponse() { |
| 177 | + final @Nullable Response current = currentContexts.getResponse(); |
| 178 | + if (current != null) { |
| 179 | + return current; |
| 180 | + } |
| 181 | + final @Nullable Response isolation = isolationContexts.getResponse(); |
| 182 | + if (isolation != null) { |
| 183 | + return isolation; |
| 184 | + } |
| 185 | + return globalContexts.getResponse(); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public void withResponse(HintUtils.SentryConsumer<Response> callback) { |
| 190 | + if (currentContexts.getResponse() != null) { |
| 191 | + currentContexts.withResponse(callback); |
| 192 | + } else if (isolationContexts.getResponse() != null) { |
| 193 | + isolationContexts.withResponse(callback); |
| 194 | + } else if (globalContexts.getResponse() != null) { |
| 195 | + globalContexts.withResponse(callback); |
| 196 | + } else { |
| 197 | + getDefaultContexts().withResponse(callback); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public void setResponse(@NotNull Response response) { |
| 203 | + getDefaultContexts().setResponse(response); |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public void serialize(@NotNull ObjectWriter writer, @NotNull ILogger logger) throws IOException { |
| 208 | + final @NotNull Contexts allContexts = new Contexts(); |
| 209 | + allContexts.putAll(globalContexts); |
| 210 | + allContexts.putAll(isolationContexts); |
| 211 | + allContexts.putAll(currentContexts); |
| 212 | + allContexts.serialize(writer, logger); |
| 213 | + } |
| 214 | +} |
0 commit comments