-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContractLoader.java
More file actions
73 lines (65 loc) · 3.15 KB
/
ContractLoader.java
File metadata and controls
73 lines (65 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package blue.language.processor;
import blue.language.mapping.NodeToObjectConverter;
import blue.language.model.Node;
import blue.language.processor.model.ChannelContract;
import blue.language.processor.model.Contract;
import blue.language.processor.model.HandlerContract;
import blue.language.processor.model.MarkerContract;
import blue.language.processor.model.ProcessEmbedded;
import blue.language.processor.util.ProcessorContractConstants;
import java.util.Map;
import java.util.Objects;
/**
* Parses contracts under a scope and produces a {@link ContractBundle}.
*/
final class ContractLoader {
private final ContractProcessorRegistry registry;
private final NodeToObjectConverter converter;
ContractLoader(ContractProcessorRegistry registry, NodeToObjectConverter converter) {
this.registry = Objects.requireNonNull(registry, "registry");
this.converter = Objects.requireNonNull(converter, "converter");
}
ContractBundle load(Node scopeNode, String scopePath) {
ContractBundle.Builder builder = ContractBundle.builder();
Map<String, Node> properties = scopeNode.getProperties();
if (properties == null) {
return builder.build();
}
Node contractsNode = properties.get("contracts");
if (contractsNode == null || contractsNode.getProperties() == null) {
return builder.build();
}
for (Map.Entry<String, Node> entry : contractsNode.getProperties().entrySet()) {
String key = entry.getKey();
Contract contract = converter.convertWithType(entry.getValue(), Contract.class, false);
if (contract == null) {
continue;
}
contract.setKey(key);
if (contract instanceof ChannelContract) {
ChannelContract channel = (ChannelContract) contract;
if (!ProcessorContractConstants.isProcessorManagedChannel(channel)
&& !registry.lookupChannel(channel.getClass()).isPresent()) {
throw new MustUnderstandFailureException(
"Unsupported contract type: " + channel.getClass().getName());
}
builder.addChannel(key, channel);
} else if (contract instanceof HandlerContract) {
HandlerContract handler = (HandlerContract) contract;
if (!registry.lookupHandler(handler.getClass()).isPresent()) {
throw new MustUnderstandFailureException(
"Unsupported contract type: " + handler.getClass().getName());
}
if (handler.getChannelKey() == null || handler.getChannelKey().isEmpty()) {
throw new IllegalStateException("Handler " + key + " must declare channel");
}
builder.addHandler(key, handler);
} else if (contract instanceof ProcessEmbedded) {
builder.setEmbedded((ProcessEmbedded) contract);
} else if (contract instanceof MarkerContract) {
builder.addMarker(key, (MarkerContract) contract);
}
}
return builder.build();
}
}