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

temporalio / sdk-java / #343

31 Oct 2024 06:31PM UTC coverage: 75.148% (-3.6%) from 78.794%
#343

push

github

web-flow
Fix jacoco coverage (#2304)

5139 of 8240 branches covered (62.37%)

Branch coverage included in aggregate %.

22841 of 28993 relevant lines covered (78.78%)

0.79 hits per line

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

92.42
/temporal-sdk/src/main/java/io/temporal/internal/sync/QueryDispatcher.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.api.sdk.v1.WorkflowInteractionDefinition;
25
import io.temporal.common.converter.DataConverter;
26
import io.temporal.common.converter.EncodedValues;
27
import io.temporal.common.interceptors.Header;
28
import io.temporal.common.interceptors.WorkflowInboundCallsInterceptor;
29
import io.temporal.common.interceptors.WorkflowOutboundCallsInterceptor;
30
import io.temporal.workflow.DynamicQueryHandler;
31
import java.util.*;
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

35
class QueryDispatcher {
36
  private static final Logger log = LoggerFactory.getLogger(QueryDispatcher.class);
1✔
37

38
  private final DataConverter dataConverterWithWorkflowContext;
39
  private final Map<String, WorkflowOutboundCallsInterceptor.RegisterQueryInput> queryCallbacks =
1✔
40
      new HashMap<>();
41

42
  private DynamicQueryHandler dynamicQueryHandler;
43
  private WorkflowInboundCallsInterceptor inboundCallsInterceptor;
44

45
  public QueryDispatcher(DataConverter dataConverterWithWorkflowContext) {
1✔
46
    this.dataConverterWithWorkflowContext = dataConverterWithWorkflowContext;
1✔
47
  }
1✔
48

49
  public void setInboundCallsInterceptor(WorkflowInboundCallsInterceptor inboundCallsInterceptor) {
50
    this.inboundCallsInterceptor = inboundCallsInterceptor;
1✔
51
  }
1✔
52

53
  /** Called from the interceptor tail */
54
  public WorkflowInboundCallsInterceptor.QueryOutput handleInterceptedQuery(
55
      WorkflowInboundCallsInterceptor.QueryInput input) {
56
    String queryName = input.getQueryName();
1✔
57
    Object[] args = input.getArguments();
1✔
58
    WorkflowOutboundCallsInterceptor.RegisterQueryInput handler = queryCallbacks.get(queryName);
1✔
59
    Object result;
60
    if (handler == null) {
1✔
61
      if (dynamicQueryHandler != null) {
1!
62
        result = dynamicQueryHandler.handle(queryName, (EncodedValues) args[0]);
1✔
63
      } else {
64
        throw new IllegalStateException("Unknown query type: " + queryName);
×
65
      }
66
    } else {
67
      result = handler.getCallback().apply(args);
1✔
68
    }
69
    return new WorkflowInboundCallsInterceptor.QueryOutput(result);
1✔
70
  }
71

72
  public Optional<Payloads> handleQuery(String queryName, Header header, Optional<Payloads> input) {
73
    WorkflowOutboundCallsInterceptor.RegisterQueryInput handler = queryCallbacks.get(queryName);
1✔
74
    Object[] args;
75
    if (handler == null) {
1✔
76
      if (dynamicQueryHandler == null) {
1✔
77
        throw new IllegalArgumentException(
1✔
78
            "Unknown query type: " + queryName + ", knownTypes=" + queryCallbacks.keySet());
1✔
79
      }
80
      args = new Object[] {new EncodedValues(input, dataConverterWithWorkflowContext)};
1✔
81
    } else {
82
      args =
1✔
83
          dataConverterWithWorkflowContext.fromPayloads(
1✔
84
              input, handler.getArgTypes(), handler.getGenericArgTypes());
1✔
85
    }
86
    Object result =
1✔
87
        inboundCallsInterceptor
88
            .handleQuery(new WorkflowInboundCallsInterceptor.QueryInput(queryName, header, args))
1✔
89
            .getResult();
1✔
90
    return dataConverterWithWorkflowContext.toPayloads(result);
1✔
91
  }
92

93
  public void registerQueryHandlers(WorkflowOutboundCallsInterceptor.RegisterQueryInput request) {
94
    String queryType = request.getQueryType();
1✔
95
    if (queryCallbacks.containsKey(queryType)) {
1!
96
      throw new IllegalStateException("Query \"" + queryType + "\" is already registered");
×
97
    }
98
    queryCallbacks.put(queryType, request);
1✔
99
  }
1✔
100

101
  public void registerDynamicQueryHandler(
102
      WorkflowOutboundCallsInterceptor.RegisterDynamicQueryHandlerInput input) {
103
    dynamicQueryHandler = input.getHandler();
1✔
104
  }
1✔
105

106
  public List<WorkflowInteractionDefinition> getQueryHandlers() {
107
    List<WorkflowInteractionDefinition> handlers = new ArrayList<>(queryCallbacks.size() + 1);
1✔
108
    for (Map.Entry<String, WorkflowOutboundCallsInterceptor.RegisterQueryInput> entry :
109
        queryCallbacks.entrySet()) {
1✔
110
      WorkflowOutboundCallsInterceptor.RegisterQueryInput handler = entry.getValue();
1✔
111
      handlers.add(
1✔
112
          WorkflowInteractionDefinition.newBuilder()
1✔
113
              .setName(handler.getQueryType())
1✔
114
              .setDescription(handler.getDescription())
1✔
115
              .build());
1✔
116
    }
1✔
117
    if (dynamicQueryHandler != null) {
1!
118
      handlers.add(
1✔
119
          WorkflowInteractionDefinition.newBuilder()
1✔
120
              .setDescription(dynamicQueryHandler.getDescription())
1✔
121
              .build());
1✔
122
    }
123
    handlers.sort(Comparator.comparing(WorkflowInteractionDefinition::getName));
1✔
124
    return handlers;
1✔
125
  }
126
}
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