Skip to content

Include partitions.json data statically in provider #3637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-96d5a14.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS SDK for Java v2",
"contributor": "",
"description": "Include the raw `partitions.json` data as a string within `DefaultPartitionDataProvider` so it doesn't need to be loaded at runtime as a classpath resource."
}
5 changes: 5 additions & 0 deletions codegen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@
<artifactId>ruleset-testing-core</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>json-utils</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>

<dependency>
<artifactId>org.eclipse.jdt.core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import software.amazon.awssdk.codegen.model.config.customization.CustomizationConfig;
import software.amazon.awssdk.codegen.model.service.ClientContextParam;
import software.amazon.awssdk.codegen.poet.rules.ClientContextParamsClassSpec;
import software.amazon.awssdk.codegen.poet.rules.DefaultPartitionDataProviderSpec;
import software.amazon.awssdk.codegen.poet.rules.EndpointAuthSchemeInterceptorClassSpec;
import software.amazon.awssdk.codegen.poet.rules.EndpointParametersClassSpec;
import software.amazon.awssdk.codegen.poet.rules.EndpointProviderInterfaceSpec;
Expand Down Expand Up @@ -58,6 +59,7 @@ protected List<GeneratorTask> createTasks() throws Exception {
tasks.add(generateClientContextParams());
}
tasks.add(new RulesEngineRuntimeGeneratorTask(generatorTaskParams));
tasks.add(generateDefaultPartitionsProvider());
return tasks;
}

Expand All @@ -73,6 +75,11 @@ private GeneratorTask generateDefaultProvider() {
return new PoetGeneratorTask(endpointRulesInternalDir(), model.getFileHeader(), new EndpointProviderSpec(model));
}

private GeneratorTask generateDefaultPartitionsProvider() {
return new PoetGeneratorTask(endpointRulesInternalDir(), model.getFileHeader(),
new DefaultPartitionDataProviderSpec(model));
}

private Collection<GeneratorTask> generateInterceptors() {
return Arrays.asList(
new PoetGeneratorTask(endpointRulesInternalDir(), model.getFileHeader(), new EndpointResolverInterceptorSpec(model)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import software.amazon.awssdk.codegen.emitters.GeneratorTask;
import software.amazon.awssdk.codegen.emitters.GeneratorTaskParams;
import software.amazon.awssdk.codegen.emitters.SimpleGeneratorTask;
import software.amazon.awssdk.codegen.poet.rules.EndpointRulesSpecUtils;
import software.amazon.awssdk.utils.IoUtils;
import software.amazon.awssdk.utils.StringUtils;
import software.amazon.awssdk.utils.Validate;
Expand All @@ -39,20 +37,22 @@ public final class RulesEngineRuntimeGeneratorTask extends BaseGeneratorTasks {
private final String engineInternalResourcesDir;
private final String engineInternalPackageName;
private final String fileHeader;
private final EndpointRulesSpecUtils endpointRulesSpecUtils;

public RulesEngineRuntimeGeneratorTask(GeneratorTaskParams generatorTaskParams) {
super(generatorTaskParams);
this.engineInternalClassDir = generatorTaskParams.getPathProvider().getEndpointRulesInternalDirectory();
this.engineInternalResourcesDir = generatorTaskParams.getPathProvider().getEndpointRulesInternalResourcesDirectory();
this.engineInternalPackageName = generatorTaskParams.getModel().getMetadata().getFullInternalEndpointRulesPackageName();
this.fileHeader = generatorTaskParams.getModel().getFileHeader();
this.endpointRulesSpecUtils = new EndpointRulesSpecUtils(generatorTaskParams.getModel());
}

@Override
protected List<GeneratorTask> createTasks() throws Exception {
List<GeneratorTask> copyTasks = new ArrayList<>();

List<String> rulesEngineFiles = rulesEngineResourceFiles();
List<String> rulesEngineFiles = endpointRulesSpecUtils.rulesEngineResourceFiles();

for (String path : rulesEngineJavaFilePaths(rulesEngineFiles)) {
String newFileName = computeNewName(path);
Expand All @@ -62,15 +62,6 @@ protected List<GeneratorTask> createTasks() throws Exception {
() -> rulesEngineFileContent("/" + path)));
}

for (String path : rulesEngineJsonFilePaths(rulesEngineFiles)) {
String newFileName = computeNewName(path);
copyTasks.add(new SimpleGeneratorTask(engineInternalResourcesDir,
newFileName,
".json",
"",
() -> loadResourceAsString("/" + path)));
}

return copyTasks;
}

Expand All @@ -80,23 +71,6 @@ private List<String> rulesEngineJavaFilePaths(Collection<String> runtimeEngineFi
.collect(Collectors.toList());
}

private List<String> rulesEngineJsonFilePaths(Collection<String> runtimeEngineFiles) {
return runtimeEngineFiles.stream()
.filter(e -> e.endsWith(".json.resource"))
.collect(Collectors.toList());
}

private List<String> rulesEngineResourceFiles() {
URL currentJarUrl = RulesEngineRuntimeGeneratorTask.class.getProtectionDomain().getCodeSource().getLocation();
try (JarFile jarFile = new JarFile(currentJarUrl.getFile())) {
return jarFile.stream()
.map(ZipEntry::getName)
.filter(e -> e.startsWith("software/amazon/awssdk/codegen/rules"))
.collect(Collectors.toList());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private String rulesEngineFileContent(String path) {
return "package " + engineInternalPackageName + ";\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright 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 software.amazon.awssdk.codegen.poet.rules;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeSpec;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import javax.lang.model.element.Modifier;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.poet.ClassSpec;
import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.protocols.jsoncore.JsonNode;
import software.amazon.awssdk.utils.IoUtils;
import software.amazon.awssdk.utils.Lazy;
import software.amazon.awssdk.utils.Validate;

public class DefaultPartitionDataProviderSpec implements ClassSpec {
private final IntermediateModel model;
private final EndpointRulesSpecUtils endpointRulesSpecUtils;

private final ClassName partitionsClass;

public DefaultPartitionDataProviderSpec(IntermediateModel model) {
this.model = model;
this.endpointRulesSpecUtils = new EndpointRulesSpecUtils(model);
this.partitionsClass = endpointRulesSpecUtils.rulesRuntimeClassName("Partitions");
}

@Override
public TypeSpec poetSpec() {
TypeSpec.Builder builder = PoetUtils.createClassBuilder(className())
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addAnnotation(SdkInternalApi.class)
.addSuperinterface(
endpointRulesSpecUtils.rulesRuntimeClassName("PartitionDataProvider"));

builder.addField(partitionDataField());
builder.addField(partitionsLazyField());
builder.addMethod(loadPartitionsMethod());
builder.addMethod(doLoadPartitionsMethod());
return builder.build();
}

@Override
public ClassName className() {
return endpointRulesSpecUtils.rulesRuntimeClassName("DefaultPartitionDataProvider");
}

private MethodSpec loadPartitionsMethod() {
MethodSpec.Builder builder = MethodSpec.methodBuilder("loadPartitions")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.returns(partitionsClass);

builder.addStatement("return PARTITIONS.getValue()");

return builder.build();
}

private FieldSpec partitionDataField() {
FieldSpec.Builder builder = FieldSpec.builder(String.class, "DEFAULT_PARTITION_DATA", Modifier.PRIVATE,
Modifier.STATIC, Modifier.FINAL);
builder.initializer("$S", readPartitionsJson());
return builder.build();
}

private FieldSpec partitionsLazyField() {
ParameterizedTypeName lazyType = ParameterizedTypeName.get(ClassName.get(Lazy.class),
partitionsClass);
FieldSpec.Builder builder = FieldSpec.builder(lazyType, "PARTITIONS")
.addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL);
CodeBlock init = CodeBlock.builder()
.addStatement("new $T<>($T::doLoadPartitions)", Lazy.class, className())
.build();

builder.initializer(init);

return builder.build();
}

private MethodSpec doLoadPartitionsMethod() {
MethodSpec.Builder builder = MethodSpec.methodBuilder("doLoadPartitions")
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
.returns(partitionsClass);

builder.addStatement("return $T.fromNode($T.parser().parse(DEFAULT_PARTITION_DATA))", partitionsClass, JsonNode.class);

return builder.build();
}

private String readPartitionsJson() {
String jsonPath = endpointRulesSpecUtils.rulesEngineResourceFiles()
.stream()
.filter(e -> e.endsWith("partitions.json.resource"))
.findFirst()
.orElseThrow(
() -> new RuntimeException("Could not find partitions.json.resource"));

return loadResourceAsString("/" + jsonPath);
}

private String loadResourceAsString(String path) {
try {
return IoUtils.toUtf8String(loadResource(path));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private InputStream loadResource(String name) {
InputStream resourceAsStream = DefaultPartitionDataProviderSpec.class.getResourceAsStream(name);
Validate.notNull(resourceAsStream, "Failed to load resource from %s", name);
return resourceAsStream;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import software.amazon.awssdk.codegen.internal.Utils;
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.intermediate.Metadata;
Expand Down Expand Up @@ -182,4 +189,16 @@ public boolean isS3Control() {
public TypeName resolverReturnType() {
return ParameterizedTypeName.get(CompletableFuture.class, Endpoint.class);
}

public List<String> rulesEngineResourceFiles() {
URL currentJarUrl = EndpointRulesSpecUtils.class.getProtectionDomain().getCodeSource().getLocation();
try (JarFile jarFile = new JarFile(currentJarUrl.getFile())) {
return jarFile.stream()
.map(ZipEntry::getName)
.filter(e -> e.startsWith("software/amazon/awssdk/codegen/rules"))
.collect(Collectors.toList());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 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 software.amazon.awssdk.services.endpointproviders;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.restjsonendpointproviders.endpoints.internal.DefaultPartitionDataProvider;
import software.amazon.awssdk.services.restjsonendpointproviders.endpoints.internal.Partition;
import software.amazon.awssdk.services.restjsonendpointproviders.endpoints.internal.Partitions;

public class DefaultPartitionDataProviderTest {
private DefaultPartitionDataProvider provider;

@BeforeEach
public void setup() {
provider = new DefaultPartitionDataProvider();
}

@Test
public void loadPartitions_returnsData() {
Partitions partitions = provider.loadPartitions();
assertThat(partitions.partitions()).isNotEmpty();
}

@Test
public void loadPartitions_partitionsContainsValidData() {
Partition awsPartition = provider.loadPartitions()
.partitions()
.stream().filter(e -> e.id().equals("aws"))
.findFirst()
.orElseThrow(
() -> new RuntimeException("could not find aws partition"));

assertThat(awsPartition.regions()).containsKey("us-west-2");
}
}