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

temporalio / sdk-java / #188

25 Sep 2023 04:42PM UTC coverage: 77.369% (-0.3%) from 77.663%
#188

push

github-actions

web-flow
Fix null pointer on trigger immediately (#1865)

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

18670 of 24131 relevant lines covered (77.37%)

0.77 hits per line

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

76.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.Header;
27
import io.temporal.common.interceptors.WorkflowInboundCallsInterceptor;
28
import io.temporal.common.interceptors.WorkflowInboundCallsInterceptor.UpdateInput;
29
import io.temporal.common.interceptors.WorkflowInboundCallsInterceptor.UpdateOutput;
30
import io.temporal.common.interceptors.WorkflowOutboundCallsInterceptor;
31
import io.temporal.common.interceptors.WorkflowOutboundCallsInterceptor.UpdateRegistrationRequest;
32
import io.temporal.workflow.DynamicUpdateHandler;
33
import java.util.HashMap;
34
import java.util.Map;
35
import java.util.Optional;
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38

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

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

46
  private DynamicUpdateHandler dynamicUpdateHandler;
47
  private WorkflowInboundCallsInterceptor inboundCallsInterceptor;
48

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

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

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

74
    inboundCallsInterceptor.validateUpdate(
1✔
75
        new WorkflowInboundCallsInterceptor.UpdateInput(updateName, header, args));
76
  }
1✔
77

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

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

113
  public void registerDynamicUpdateHandler(
114
      WorkflowOutboundCallsInterceptor.RegisterDynamicUpdateHandlerInput input) {
115
    dynamicUpdateHandler = input.getHandler();
×
116
  }
×
117

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

133
  public UpdateOutput handleInterceptedExecuteUpdate(UpdateInput input) {
134
    String updateName = input.getUpdateName();
1✔
135
    Object[] args = input.getArguments();
1✔
136
    UpdateRegistrationRequest handler = updateCallbacks.get(updateName);
1✔
137
    Object result;
138
    if (handler == null) {
1✔
139
      if (dynamicUpdateHandler != null) {
×
140
        result = dynamicUpdateHandler.handleExecute(updateName, (EncodedValues) args[0]);
×
141
      } else {
142
        throw new IllegalStateException("Unknown update name: " + updateName);
×
143
      }
144
    } else {
145
      result = handler.getExecuteCallback().apply(args);
1✔
146
    }
147
    return new WorkflowInboundCallsInterceptor.UpdateOutput(result);
1✔
148
  }
149
}
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