forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddWaiters.java
More file actions
126 lines (101 loc) · 4.94 KB
/
AddWaiters.java
File metadata and controls
126 lines (101 loc) · 4.94 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.codegen;
import com.amazonaws.codegen.model.intermediate.AcceptorModel;
import com.amazonaws.codegen.model.intermediate.OperationModel;
import com.amazonaws.codegen.model.intermediate.WaiterDefinitionModel;
import com.amazonaws.codegen.model.service.Acceptor;
import com.amazonaws.codegen.model.service.WaiterDefinition;
import com.amazonaws.codegen.model.service.Waiters;
import com.amazonaws.jmespath.JmesPathExpression;
import com.amazonaws.util.IOUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.amazonaws.codegen.AstJsonToAstJava.fromAstJsonToAstJava;
class AddWaiters {
private static final ObjectMapper mapper = new ObjectMapper();
private final Waiters waiters;
private final Map<String, OperationModel> operations;
private final String codeGenBinDirectory;
AddWaiters(Waiters waiters, Map<String, OperationModel> operations, String codeGenBinDirectory) {
this.waiters = waiters;
this.operations = operations;
this.codeGenBinDirectory = codeGenBinDirectory;
}
Map<String, WaiterDefinitionModel> constructWaiters() throws IOException {
Map<String, WaiterDefinitionModel> javaWaiterModels = new HashMap<>();
Map<String, JmesPathExpression> argumentToAstMap = new HashMap<>();
for (Map.Entry<String, WaiterDefinition> entry : waiters.getWaiters().entrySet()) {
final String waiterName = entry.getKey();
final WaiterDefinition waiterDefinition = entry.getValue();
List<AcceptorModel> acceptors = new ArrayList<>();
WaiterDefinitionModel waiterDefinitionModel = new WaiterDefinitionModel();
waiterDefinitionModel.setDelay(waiterDefinition.getDelay());
waiterDefinitionModel.setMaxAttempts(waiterDefinition.getMaxAttempts());
waiterDefinitionModel.setWaiterName(waiterName);
waiterDefinitionModel.setOperationModel(operations.get(waiterDefinition.getOperation()));
for (Acceptor acceptor : waiterDefinition.getAcceptors()) {
AcceptorModel acceptorModel = new AcceptorModel();
acceptorModel.setMatcher(acceptor.getMatcher());
acceptorModel.setState(acceptor.getState());
acceptorModel.setExpected(acceptor.getExpected());
acceptorModel.setArgument(acceptor.getArgument());
acceptorModel.setAst(getAstFromArgument(acceptor.getArgument(), argumentToAstMap));
acceptors.add(acceptorModel);
}
waiterDefinitionModel.setAcceptors(acceptors);
javaWaiterModels.put(waiterName, waiterDefinitionModel);
}
return javaWaiterModels;
}
private JmesPathExpression getAstFromArgument(String argument, Map<String, JmesPathExpression> argumentToAstMap) throws
IOException {
if (argument != null && !argumentToAstMap.containsKey(argument)) {
final Process p = executeToAstProcess(argument);
if(p.exitValue()!= 0) {
throw new RuntimeException(IOUtils.toString(p.getErrorStream()));
}
JsonNode jsonNode = mapper.readTree(IOUtils.toString(p.getInputStream()));
JmesPathExpression ast = fromAstJsonToAstJava(jsonNode);
argumentToAstMap.put(argument, ast);
IOUtils.closeQuietly(p.getInputStream(), null);
return ast;
} else if (argument != null) {
return argumentToAstMap.get(argument);
}
return null;
}
/**
* Execute the jp-to-ast.py command and wait for it to complete.
*
* @param argument JP expression to compile to AST.
* @return Process with access to output streams.
*/
private Process executeToAstProcess(String argument) throws IOException {
try {
Process p = new ProcessBuilder("python", codeGenBinDirectory + "/jp-to-ast.py", argument).start();
p.waitFor();
return p;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
}