forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeEmitter.java
More file actions
571 lines (464 loc) · 22.3 KB
/
CodeEmitter.java
File metadata and controls
571 lines (464 loc) · 22.3 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
* Copyright (c) 2016. 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.emitters;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import com.amazonaws.codegen.internal.Freemarker;
import com.amazonaws.codegen.internal.ImmutableMapParameter;
import com.amazonaws.codegen.internal.Utils;
import com.amazonaws.codegen.model.config.customization.AuthPolicyActions;
import com.amazonaws.codegen.model.config.customization.CustomizationConfig;
import com.amazonaws.codegen.model.config.templates.CodeGenTemplatesConfig;
import com.amazonaws.codegen.model.intermediate.*;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import freemarker.template.Template;
import static com.amazonaws.codegen.internal.Constants.AUTH_POLICY_ENUM_CLASS_DIR;
import static com.amazonaws.codegen.internal.Constants.SMOKE_TESTS_DIR_NAME;
import static com.amazonaws.codegen.internal.Constants.PACKAGE_NAME_MODEL_SUFFIX;
import static com.amazonaws.codegen.internal.Constants.PACKAGE_NAME_TRANSFORM_SUFFIX;
import static com.amazonaws.codegen.internal.Constants.PROPERTIES_FILE_NAME_SUFFIX;
import static com.amazonaws.codegen.internal.Constants.*;
/**
* Consumes the intermediate model and submits tasks for generating the code. Waits until all tasks
* are completed.
*/
public class CodeEmitter implements AutoCloseable {
private static final boolean DEBUG = false;
protected final IntermediateModel model;
private final ExecutorService executors = Executors.newFixedThreadPool(10);
private final List<Future<Void>> futures = new ArrayList<Future<Void>>();
private final String baseDirectory;
private final String modelClassDir;
private final String transformClassDir;
private final String policyEnumClassDir;
private final String smokeTestsPackageDir;
private final String waiterClassDir;
protected final Freemarker freemarker;
public CodeEmitter(IntermediateModel model, String outputDirectory) {
if (model == null) {
throw new IllegalArgumentException("Intermediate Model cannot be null");
}
if (outputDirectory == null || outputDirectory.trim().isEmpty()) {
throw new IllegalArgumentException("Invalid output directory path");
}
this.model = model;
this.baseDirectory = outputDirectory + "/" + model.getMetadata().getPackagePath();
this.modelClassDir = baseDirectory + "/" + PACKAGE_NAME_MODEL_SUFFIX;
this.transformClassDir = modelClassDir + "/" + PACKAGE_NAME_TRANSFORM_SUFFIX;
this.policyEnumClassDir = outputDirectory + "/" + AUTH_POLICY_ENUM_CLASS_DIR;
this.waiterClassDir = baseDirectory + "/" + PACKAGE_NAME_WAITERS_SUFFIX ;
this.freemarker = new Freemarker(loadProtocolTemplatesConfig(model.getMetadata().getProtocol()));
this.smokeTestsPackageDir = outputDirectory + "/../" + SMOKE_TESTS_DIR_NAME + "/" + model.getMetadata().getPackagePath() + "/" + SMOKE_TESTS_DIR_NAME;
}
public void emit() throws JsonGenerationException, JsonMappingException, IOException, InterruptedException,
ExecutionException {
// Create client directories
Utils.createDirectory(transformClassDir);
Utils.createDirectory(waiterClassDir);
Utils.createDirectory(smokeTestsPackageDir);
emitWaiterOpFunctionClasses();
emitWaiterAcceptorClasses();
emitWaiterClasses();
emitClientInterfaces();
emitClientClasses();
emitClientBuilders();
emitModelClasses();
// If a custom base class is provided we assume it already exists and does not need
// to be generated
if (model.getCustomizationConfig().getSdkModeledExceptionBaseClassName() == null) {
emitExceptionBaseClass();
}
emitMarshallerClasses();
emitUnmarshallerClasses();
emitPolicyActionEnumClass();
emitPackageInfoFile();
// Skips generating smoketest files if skipSmokeTests is set in custom config
if (!model.getCustomizationConfig().isSkipSmokeTests()) {
emitSmokeTestFiles();
}
waitForCompletion();
}
@Override
public void close() {
executors.shutdown();
}
private void emitPackageInfoFile() throws IOException {
submitTask(new ClassGeneratorTask(baseDirectory, "package-info.java", freemarker
.getPackageInfoTemplate(), model));
}
/**
* Submits a task to generate files needed for smoke tests.
*/
private void emitSmokeTestFiles() throws IOException {
submitTask(new ClassGeneratorTask(
smokeTestsPackageDir,
model.getMetadata().getCucumberModuleInjectorClassName(),
freemarker.getCucumberModuleInjectorTemplate(),
model));
submitTask(new ClassGeneratorTask(
smokeTestsPackageDir,
"RunCucumberTest",
freemarker.getCucumberTestTemplate(),
model));
submitTask(new ClassGeneratorTask(
new CodeWriter(smokeTestsPackageDir, "cucumber", PROPERTIES_FILE_NAME_SUFFIX),
freemarker.getCucumberPropertiesTemplate(),
model));
}
/**
* Submits a task for client class generation
*/
protected void emitClientClasses() throws IOException {
submitTask(new ClassGeneratorTask(
baseDirectory,
model.getMetadata().getSyncAbstractClass(),
freemarker.getSyncAbstractClassTemplate(),
model));
submitTask(new ClassGeneratorTask(
baseDirectory,
model.getMetadata().getSyncClient(),
freemarker.getSyncClientTemplate(),
model));
if (model.getMetadata().hasAsyncClient()) {
submitTask(new ClassGeneratorTask(
baseDirectory,
model.getMetadata().getAsyncAbstractClass(),
freemarker.getAsyncAbstractClassTemplate(),
model));
submitTask(new ClassGeneratorTask(
baseDirectory,
model.getMetadata().getAsyncClient(),
freemarker.getAsyncClientTemplate(),
model));
}
}
/**
* Submits a task to generate the fluent builders for Sync and Async clients
*/
protected void emitClientBuilders() throws IOException {
submitTask(new ClassGeneratorTask(
baseDirectory,
model.getMetadata().getSyncClientBuilderClassName(),
freemarker.getSyncClientBuilderTemplate(),
model));
if (model.getMetadata().hasAsyncClient()) {
submitTask(new ClassGeneratorTask(
baseDirectory,
model.getMetadata().getAsyncClientBuilderClassName(),
freemarker.getAsyncClientBuilderTemplate(),
model));
}
}
private void submitTask(ClassGeneratorTask task) {
if (DEBUG) {
try {
task.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
futures.add(executors.submit(task));
}
}
/**
* Submits a task for client interface generation
*/
protected void emitClientInterfaces() throws IOException {
submitTask(new ClassGeneratorTask(
baseDirectory,
model.getMetadata().getSyncInterface(),
freemarker.getSyncInterfaceTemplate(),
model));
if (model.getMetadata().hasAsyncClient()) {
submitTask(new ClassGeneratorTask(
baseDirectory,
model.getMetadata().getAsyncInterface(),
freemarker.getAsyncInterfaceTemplate(),
model));
}
}
/**
* Constructs the data model and submits tasks for every generating classes for every shape in
* the intermediate model.
*/
protected void emitModelClasses() throws IOException {
Metadata metadata = model.getMetadata();
final Map<String, ShapeModel> shapes = model.getShapes();
for (Map.Entry<String, ShapeModel> entry : shapes.entrySet()) {
final String javaShapeName = entry.getKey();
final ShapeModel shapeModel = entry.getValue();
if (shapeModel.getCustomization().isSkipGeneratingModelClass()) {
System.out.println("Skip generating class " + shapeModel.getShapeName());
continue;
}
Map<String, Object> dataModel = ImmutableMapParameter.of(
"fileHeader", model.getFileHeader(),
"shape", shapeModel,
"metadata", metadata,
"baseExceptionFqcn", model.getSdkModeledExceptionBaseFqcn(),
"customConfig", model.getCustomizationConfig());
// Submit task for generating the
// model/request/response/enum/exception class.
submitTask(new ClassGeneratorTask(modelClassDir, javaShapeName,
freemarker.getShapeTemplate(shapeModel),
dataModel));
}
}
private void emitExceptionBaseClass() throws IOException {
final String baseClassName = model.getSdkModeledExceptionBaseClassName();
submitTask(new ClassGeneratorTask(modelClassDir,
baseClassName,
freemarker.getBaseExceptionClassTemplate(),
ImmutableMapParameter.of(
"fileHeader", model.getFileHeader(),
"className", baseClassName,
"metadata", model.getMetadata(),
"baseExceptionFqcn",
model.getServiceBaseExceptionFqcn())));
}
/**
* Constructs the data model and submits tasks for every generating marshallers for shapes in
* the intermediate model.
*/
protected void emitMarshallerClasses() throws IOException {
Metadata metadata = model.getMetadata();
final Map<String, ShapeModel> shapes = model.getShapes();
final Template template = freemarker.getModelMarshallerTemplate();
for (Map.Entry<String, ShapeModel> entry : shapes.entrySet()) {
final String javaShapeName = entry.getKey();
final ShapeModel shapeModel = entry.getValue();
final ShapeType shapeType = ShapeType.fromValue(shapeModel.getType());
if (shapeModel.getCustomization().isSkipGeneratingMarshaller()) {
System.out.println("Skip generating marshaller class for "
+ shapeModel.getShapeName());
continue;
}
Map<String, Object> marshallerDataModel = ImmutableMapParameter.of(
"fileHeader", model.getFileHeader(),
"shapeName", javaShapeName,
"shapes", shapes,
"metadata", metadata,
"customConfig", model.getCustomizationConfig()
);
if (ShapeType.Request == shapeType) {
String className = getMarshallerClassName(shapeType, javaShapeName, metadata);
// Submit a task for generating the request marshaller.
submitTask(new ClassGeneratorTask(transformClassDir, className, template, marshallerDataModel));
} else if (ShapeType.Model == shapeType) {
// Submit a task for generating model marshallers for json protocol.
if (metadata.isJsonProtocol()) {
String className = getMarshallerClassName(shapeType, javaShapeName, metadata);
submitTask(new ClassGeneratorTask(transformClassDir, className, template, marshallerDataModel));
}
}
}
}
protected String getMarshallerClassName(ShapeType shapeType, String shapeName, Metadata metadata) {
if (ShapeType.Request == shapeType) {
return shapeName + "Marshaller";
} else if ((ShapeType.Model == shapeType) && (metadata.isJsonProtocol())) {
return shapeName + "JsonMarshaller";
}
throw new IllegalArgumentException("Not able generate marshaller class name for " + shapeName + " type "
+ shapeType.getValue());
}
/**
* Constructs the data model and submits tasks for every generating unmarshallers for shapes in
* the intermediate model.
*/
protected void emitUnmarshallerClasses() throws IOException {
Metadata metadata = model.getMetadata();
final Template template = freemarker.getModelUnmarshallerTemplate();
final Map<String, ShapeModel> shapes = model.getShapes();
for (Map.Entry<String, ShapeModel> entry : shapes.entrySet()) {
final String javaShapeName = entry.getKey();
final ShapeModel shapeModel = entry.getValue();
final ShapeType shapeType = ShapeType.fromValue(shapeModel.getType());
if (shapeModel.getCustomization().isSkipGeneratingUnmarshaller()) {
System.out.println("Skip generating unmarshaller class for "
+ shapeModel.getShapeName());
continue;
}
Map<String, Object> dataModel = ImmutableMapParameter.of(
"fileHeader", model.getFileHeader(),
"shape", shapeModel,
"metadata", metadata,
"exceptionUnmarshallerImpl", model.getExceptionUnmarshallerImpl());
switch (shapeType) {
case Response:
case Model: {
String unmarshallerNameSuffix = metadata.isJsonProtocol() ? "JsonUnmarshaller" : "StaxUnmarshaller";
submitTask(new ClassGeneratorTask(transformClassDir, javaShapeName + unmarshallerNameSuffix, template,
dataModel));
break;
}
case Exception: {
// Generating Exception Unmarshallers is not required for the JSON protocol
if (!metadata.isJsonProtocol()) {
submitTask(new ClassGeneratorTask(transformClassDir, javaShapeName + "Unmarshaller",
freemarker.getExceptionUnmarshallerTemplate(), dataModel));
}
break;
}
default:
continue;
}
}
}
private void waitForCompletion() throws InterruptedException, ExecutionException {
Exception firstEx = null;
for (Future<Void> future : futures) {
try {
future.get();
} catch (Exception e) {
if (firstEx == null)
firstEx = e;
}
}
if (firstEx instanceof ExecutionException)
throw (ExecutionException) firstEx;
else if (firstEx instanceof InterruptedException)
throw (InterruptedException) firstEx;
else if (firstEx instanceof RuntimeException)
throw (RuntimeException) firstEx;
}
private CodeGenTemplatesConfig loadProtocolTemplatesConfig(Protocol protocol) {
// CBOR is a type of JSON Protocol. Use JSON Protocol for templates
Protocol templateProtocol = protocol;
if (Protocol.CBOR.equals(protocol)) {
templateProtocol = Protocol.JSON;
}
CodeGenTemplatesConfig protocolDefaultConfig = CodeGenTemplatesConfig.load(templateProtocol);
CustomizationConfig customConfig = model.getCustomizationConfig();
if (customConfig == null || customConfig.getCustomCodeTemplates() == null)
return protocolDefaultConfig;
// merge any custom config and return the result.
return CodeGenTemplatesConfig.merge(protocolDefaultConfig, customConfig.getCustomCodeTemplates());
}
/**
* Submits a task to generate the policy action enum class file.
*/
private void emitPolicyActionEnumClass() throws IOException {
AuthPolicyActions policyActions = model.getCustomizationConfig().getAuthPolicyActions();
// Return if the policy action enum file generation needs to be skipped.
if (policyActions != null && policyActions.isSkip())
return;
// Creates the directory for the enum class file
Utils.createDirectory(policyEnumClassDir);
String serviceName = getPolicyActionServiceName(model.getMetadata(), policyActions);
String actionPrefix = getEnumActionPrefix(model.getMetadata(), policyActions);
Map<String, Object> dataModel = ImmutableMapParameter.of(
"fileHeader", model.getFileHeader(),
"operations", model.getOperations().keySet(),
"metadata", model.getMetadata(),
"serviceName", serviceName,
"actionPrefix", actionPrefix);
submitTask(new ClassGeneratorTask(new CodeWriter(policyEnumClassDir, serviceName + "Actions"),
freemarker.getPolicyActionClassTemplate(), dataModel));
}
/**
* Constructs the data model and submits tasks for every generating SDKFunction for every unique
* operation in the waiter intermediate model
* @throws IOException
*/
private void emitWaiterOpFunctionClasses() throws IOException {
List<String> generatedOperations = new ArrayList<>();
for (Map.Entry<String, WaiterDefinitionModel> entry : model.getWaiters().entrySet()) {
final WaiterDefinitionModel waiterModel = entry.getValue();
if(!generatedOperations.contains(waiterModel.getOperationName())) {
generatedOperations.add(waiterModel.getOperationName());
Map<String, Object> dataModel = ImmutableMapParameter.of(
"fileHeader", model.getFileHeader(),
"waiter", waiterModel,
"operation", model.getOperation(waiterModel.getOperationName()),
"metadata", model.getMetadata());
final String className = waiterModel.getOperationModel().getOperationName() + "Function";
submitTask(new ClassGeneratorTask(waiterClassDir, className,
freemarker.getWaiterSDKFunctionTemplate(),
dataModel));
}
}
}
/**
* Constructs the data model and submits tasks for every generating Acceptor for each waiter definition
* in the intermediate model
* @throws IOException
*/
private void emitWaiterAcceptorClasses() throws IOException {
for (Map.Entry<String, WaiterDefinitionModel> entry : model.getWaiters().entrySet()) {
if(containsAllStatusMatchers(entry)){
continue;
}
final String waiterName = entry.getKey();
final WaiterDefinitionModel waiterModel = entry.getValue();
Map<String, Object> dataModel = ImmutableMapParameter.of(
"fileHeader", model.getFileHeader(),
"waiter", waiterModel,
"operation", model.getOperation(waiterModel.getOperationName()),
"metadata", model.getMetadata());
submitTask(new ClassGeneratorTask(waiterClassDir, waiterName,
freemarker.getWaiterAcceptorTemplate(),
dataModel));
}
}
private static boolean containsAllStatusMatchers(Map.Entry<String, WaiterDefinitionModel> entry){
return entry.getValue().getAcceptors().stream()
.allMatch(a -> a.getMatcher().equals("status"));
}
/**
* Constructs the data model and submits tasks for every generating synchronous waiter and
* asynchronous waiter for each waiter in the intermediate model. Skips if the waiter file
* is empty.
* @throws IOException
*/
private void emitWaiterClasses() throws IOException {
if(model.getWaiters().size() > 0) {
Metadata metadata = model.getMetadata();
final String className = metadata.getSyncInterface() + "Waiters";
Map<String, Object> dataModel = ImmutableMapParameter.of(
"fileHeader", model.getFileHeader(),
"className", className,
"waiters", model.getWaiters(),
"operation", model.getOperations(),
"metadata", metadata);
submitTask(new ClassGeneratorTask(waiterClassDir, className,
freemarker.getWaiterTemplate(),
dataModel));
}
}
private String getPolicyActionServiceName(Metadata metadata, AuthPolicyActions policyActions) {
// This is to support the file naming for exiting/legacy clients.
// The files don't follow the standard naming conventions.
// To avoid breaking changes, the fileNamePrefix contains the name of
// the file to be used.
if (policyActions != null && policyActions.getFileNamePrefix() != null) {
return Utils.capitialize(policyActions.getFileNamePrefix());
}
return Utils.capitialize(metadata.getEndpointPrefix());
}
private String getEnumActionPrefix(Metadata metadata, AuthPolicyActions policyActions) {
if (policyActions != null && policyActions.getActionPrefix() != null) {
return policyActions.getActionPrefix();
}
return metadata.getEndpointPrefix();
}
}