• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

temporalio / sdk-java / #169

pending completion
#169

push

github-actions

web-flow
Remove use of deprecated API (#1758)

4 of 4 new or added lines in 1 file covered. (100.0%)

17345 of 21558 relevant lines covered (80.46%)

0.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

11.67
/temporal-sdk/src/main/java/io/temporal/internal/sync/UpdateDispatcher.java
1
/*
2
 * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3
 *
4
 * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
 *
6
 * Modifications copyright (C) 2017 Uber Technologies, Inc.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this material except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

21
package io.temporal.internal.sync;
22

23
import io.temporal.api.common.v1.Payloads;
24
import io.temporal.common.converter.DataConverter;
25
import io.temporal.common.converter.EncodedValues;
26
import io.temporal.common.interceptors.WorkflowInboundCallsInterceptor;
27
import io.temporal.common.interceptors.WorkflowInboundCallsInterceptor.UpdateInput;
28
import io.temporal.common.interceptors.WorkflowInboundCallsInterceptor.UpdateOutput;
29
import io.temporal.common.interceptors.WorkflowOutboundCallsInterceptor;
30
import io.temporal.common.interceptors.WorkflowOutboundCallsInterceptor.UpdateRegistrationRequest;
31
import io.temporal.workflow.DynamicUpdateHandler;
32
import java.util.HashMap;
33
import java.util.Map;
34
import java.util.Optional;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

38
class UpdateDispatcher {
39
  private static final Logger log = LoggerFactory.getLogger(UpdateDispatcher.class);
1✔
40

41
  private final DataConverter dataConverterWithWorkflowContext;
42
  private final Map<String, WorkflowOutboundCallsInterceptor.UpdateRegistrationRequest>
1✔
43
      updateCallbacks = new HashMap<>();
44

45
  private DynamicUpdateHandler dynamicUpdateHandler;
46
  private WorkflowInboundCallsInterceptor inboundCallsInterceptor;
47

48
  public UpdateDispatcher(DataConverter dataConverterWithWorkflowContext) {
1✔
49
    this.dataConverterWithWorkflowContext = dataConverterWithWorkflowContext;
1✔
50
  }
1✔
51

52
  public void setInboundCallsInterceptor(WorkflowInboundCallsInterceptor inboundCallsInterceptor) {
53
    this.inboundCallsInterceptor = inboundCallsInterceptor;
1✔
54
  }
1✔
55

56
  public void handleValidateUpdate(String updateName, Optional<Payloads> input, long eventId) {
57
    WorkflowOutboundCallsInterceptor.UpdateRegistrationRequest handler =
×
58
        updateCallbacks.get(updateName);
×
59
    Object[] args;
60
    if (handler == null) {
×
61
      if (dynamicUpdateHandler == null) {
×
62
        throw new IllegalArgumentException(
×
63
            "Unknown update name: " + updateName + ", knownTypes=" + updateCallbacks.keySet());
×
64
      }
65
      args = new Object[] {new EncodedValues(input, dataConverterWithWorkflowContext)};
×
66
    } else {
67
      args =
×
68
          dataConverterWithWorkflowContext.fromPayloads(
×
69
              input, handler.getArgTypes(), handler.getGenericArgTypes());
×
70
    }
71

72
    inboundCallsInterceptor.validateUpdate(
×
73
        new WorkflowInboundCallsInterceptor.UpdateInput(updateName, args));
74
  }
×
75

76
  public Optional<Payloads> handleExecuteUpdate(
77
      String updateName, Optional<Payloads> input, long eventId) {
78
    WorkflowOutboundCallsInterceptor.UpdateRegistrationRequest handler =
×
79
        updateCallbacks.get(updateName);
×
80
    Object[] args;
81
    if (handler == null) {
×
82
      if (dynamicUpdateHandler == null) {
×
83
        throw new IllegalArgumentException(
×
84
            "Unknown update name: " + updateName + ", knownTypes=" + updateCallbacks.keySet());
×
85
      }
86
      args = new Object[] {new EncodedValues(input, dataConverterWithWorkflowContext)};
×
87
    } else {
88
      args =
×
89
          dataConverterWithWorkflowContext.fromPayloads(
×
90
              input, handler.getArgTypes(), handler.getGenericArgTypes());
×
91
    }
92
    Object result =
×
93
        inboundCallsInterceptor
94
            .executeUpdate(new WorkflowInboundCallsInterceptor.UpdateInput(updateName, args))
×
95
            .getResult();
×
96
    return dataConverterWithWorkflowContext.toPayloads(result);
×
97
  }
98

99
  public void registerUpdateHandlers(
100
      WorkflowOutboundCallsInterceptor.RegisterUpdateHandlersInput input) {
101
    for (WorkflowOutboundCallsInterceptor.UpdateRegistrationRequest request : input.getRequests()) {
×
102
      String updateName = request.getUpdateName();
×
103
      if (updateCallbacks.containsKey(updateName)) {
×
104
        throw new IllegalStateException("Update \"" + updateName + "\" is already registered");
×
105
      }
106
      updateCallbacks.put(updateName, request);
×
107
    }
×
108
  }
×
109

110
  public void registerDynamicUpdateHandler(
111
      WorkflowOutboundCallsInterceptor.RegisterDynamicUpdateHandlerInput input) {
112
    dynamicUpdateHandler = input.getHandler();
×
113
  }
×
114

115
  public void handleInterceptedValidateUpdate(UpdateInput input) {
116
    String updateName = input.getUpdateName();
×
117
    Object[] args = input.getArguments();
×
118
    UpdateRegistrationRequest handler = updateCallbacks.get(updateName);
×
119
    if (handler == null) {
×
120
      if (dynamicUpdateHandler != null) {
×
121
        dynamicUpdateHandler.handleValidate(updateName, (EncodedValues) args[0]);
×
122
      } else {
123
        throw new IllegalStateException("Unknown update name: " + updateName);
×
124
      }
125
    } else {
126
      handler.getValidateCallback().apply(args);
×
127
    }
128
  }
×
129

130
  public UpdateOutput handleInterceptedExecuteUpdate(UpdateInput input) {
131
    String updateName = input.getUpdateName();
×
132
    Object[] args = input.getArguments();
×
133
    UpdateRegistrationRequest handler = updateCallbacks.get(updateName);
×
134
    Object result;
135
    if (handler == null) {
×
136
      if (dynamicUpdateHandler != null) {
×
137
        result = dynamicUpdateHandler.handleExecute(updateName, (EncodedValues) args[0]);
×
138
      } else {
139
        throw new IllegalStateException("Unknown update name: " + updateName);
×
140
      }
141
    } else {
142
      result = handler.getExecuteCallback().apply(args);
×
143
    }
144
    return new WorkflowInboundCallsInterceptor.UpdateOutput(result);
×
145
  }
146
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc