diff --git a/.brazil.json b/.brazil.json index 7d240cb9bf5c..362888889178 100644 --- a/.brazil.json +++ b/.brazil.json @@ -32,6 +32,7 @@ "s3-transfer-manager": { "packageName": "AwsJavaSdk-S3-TransferManager" }, "s3-event-notifications": { "packageName": "AwsJavaSdk-S3-EventNotifications" }, "sdk-core": { "packageName": "AwsJavaSdk-Core" }, + "utils-lite": { "packageName": "AwsJavaSdk-UtilsLite" }, "url-connection-client": { "packageName": "AwsJavaSdk-HttpClient-UrlConnectionClient" }, "utils": { "packageName": "AwsJavaSdk-Core-Utils" }, "imds": { "packageName": "AwsJavaSdk-Imds" }, diff --git a/bom/pom.xml b/bom/pom.xml index 68ad4b766ac0..413f221bdb11 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -212,6 +212,11 @@ utils ${awsjavasdk.version} + + software.amazon.awssdk + utils-lite + ${awsjavasdk.version} + software.amazon.awssdk cloudwatch-metric-publisher diff --git a/pom.xml b/pom.xml index 8efce297fd49..b58b0ef273fe 100644 --- a/pom.xml +++ b/pom.xml @@ -67,6 +67,7 @@ metric-publishers release-scripts utils + utils-lite codegen-lite codegen-lite-maven-plugin archetypes @@ -665,6 +666,7 @@ cloudwatch-metric-publisher emf-metric-logging-publisher utils + utils-lite imds retries retries-spi diff --git a/test/architecture-tests/pom.xml b/test/architecture-tests/pom.xml index 7f597acd81f5..b99e1c8821b9 100644 --- a/test/architecture-tests/pom.xml +++ b/test/architecture-tests/pom.xml @@ -61,6 +61,11 @@ software.amazon.awssdk ${awsjavasdk.version} + + utils-lite + software.amazon.awssdk + ${awsjavasdk.version} + s3 software.amazon.awssdk diff --git a/test/architecture-tests/src/test/java/software/amazon/awssdk/archtests/UtilsLitePackageTest.java b/test/architecture-tests/src/test/java/software/amazon/awssdk/archtests/UtilsLitePackageTest.java new file mode 100644 index 000000000000..437094a4c58f --- /dev/null +++ b/test/architecture-tests/src/test/java/software/amazon/awssdk/archtests/UtilsLitePackageTest.java @@ -0,0 +1,43 @@ +/* + * 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.archtests; + +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; + +import com.tngtech.archunit.core.domain.JavaClasses; +import com.tngtech.archunit.core.importer.ClassFileImporter; +import com.tngtech.archunit.lang.ArchRule; +import org.junit.jupiter.api.Test; + +/** + * Architecture tests for the utils-lite package to ensure it only contains allowed classes. + */ +public class UtilsLitePackageTest { + + private static final JavaClasses CLASSES = new ClassFileImporter() + .importPackages("software.amazon.awssdk.utilslite"); + + @Test + public void utilsLitePackage_shouldOnlyContainAllowedClasses() { + ArchRule rule = classes() + .that().resideInAPackage("software.amazon.awssdk.utilslite") + .should().haveNameMatching(".*\\.(SdkInternalThreadLocal|SdkInternalThreadLocalTest)") + .allowEmptyShould(true) + .because("utils-lite package should only contain SdkInternalThreadLocal and its test"); + + rule.check(CLASSES); + } +} \ No newline at end of file diff --git a/test/http-client-benchmarks/pom.xml b/test/http-client-benchmarks/pom.xml index 5c1c76cfec82..963925e8cd61 100644 --- a/test/http-client-benchmarks/pom.xml +++ b/test/http-client-benchmarks/pom.xml @@ -175,7 +175,7 @@ org.apache.maven.plugins maven-shade-plugin - 2.2 + 3.5.0 package diff --git a/test/sdk-benchmarks/pom.xml b/test/sdk-benchmarks/pom.xml index 070a7dc3b70e..20ac1a5dc50c 100644 --- a/test/sdk-benchmarks/pom.xml +++ b/test/sdk-benchmarks/pom.xml @@ -340,7 +340,7 @@ org.apache.maven.plugins maven-shade-plugin - 2.2 + 3.5.0 package diff --git a/test/tests-coverage-reporting/pom.xml b/test/tests-coverage-reporting/pom.xml index f361860bb693..689916cf59f9 100644 --- a/test/tests-coverage-reporting/pom.xml +++ b/test/tests-coverage-reporting/pom.xml @@ -326,6 +326,11 @@ http-client-spi ${awsjavasdk.version} + + software.amazon.awssdk + utils-lite + ${awsjavasdk.version} + diff --git a/utils-lite/pom.xml b/utils-lite/pom.xml new file mode 100644 index 000000000000..9642803ccd58 --- /dev/null +++ b/utils-lite/pom.xml @@ -0,0 +1,79 @@ + + + + + 4.0.0 + + software.amazon.awssdk + aws-sdk-java-pom + 2.33.3-SNAPSHOT + + utils-lite + AWS Java SDK :: Utils Lite + + A package providing minimal external utils. + + https://aws.amazon.com/sdkforjava + + + + + software.amazon.awssdk + bom-internal + ${project.version} + pom + import + + + + + + + software.amazon.awssdk + annotations + ${awsjavasdk.version} + + + org.junit.jupiter + junit-jupiter + test + + + org.assertj + assertj-core + test + + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + software.amazon.awssdk.utilslite + + + + + + + + \ No newline at end of file diff --git a/utils-lite/src/main/java/software/amazon/awssdk/utilslite/SdkInternalThreadLocal.java b/utils-lite/src/main/java/software/amazon/awssdk/utilslite/SdkInternalThreadLocal.java new file mode 100644 index 000000000000..34f3b5c9df30 --- /dev/null +++ b/utils-lite/src/main/java/software/amazon/awssdk/utilslite/SdkInternalThreadLocal.java @@ -0,0 +1,55 @@ +/* + * 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.utilslite; + +import java.util.HashMap; +import java.util.Map; +import software.amazon.awssdk.annotations.SdkProtectedApi; + +/** + * Utility for thread-local context storage. + */ +@SdkProtectedApi +public final class SdkInternalThreadLocal { + private static final ThreadLocal> STORAGE = ThreadLocal.withInitial(HashMap::new); + + private SdkInternalThreadLocal() { + } + + public static void put(String key, String value) { + if (value == null) { + STORAGE.get().remove(key); + } else { + STORAGE.get().put(key, value); + } + } + + public static String get(String key) { + return STORAGE.get().get(key); + } + + public static String remove(String key) { + return STORAGE.get().remove(key); + } + + public static void clear() { + STORAGE.get().clear(); + } + + public static boolean containsKey(String key) { + return STORAGE.get().containsKey(key); + } +} \ No newline at end of file diff --git a/utils-lite/src/test/java/software/amazon/awssdk/utilslite/ThreadStorageTest.java b/utils-lite/src/test/java/software/amazon/awssdk/utilslite/ThreadStorageTest.java new file mode 100644 index 000000000000..07e5e34e2ecc --- /dev/null +++ b/utils-lite/src/test/java/software/amazon/awssdk/utilslite/ThreadStorageTest.java @@ -0,0 +1,70 @@ +/* + * 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.utilslite; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class SdkInternalThreadLocalTest { + + @AfterEach + void cleanup() { + SdkInternalThreadLocal.clear(); + } + + @Test + void putAndGet_shouldStoreAndRetrieveValue() { + SdkInternalThreadLocal.put("test-key", "test-value"); + + assertThat(SdkInternalThreadLocal.get("test-key")).isEqualTo("test-value"); + } + + @Test + void get_withNonExistentKey_shouldReturnNull() { + assertThat(SdkInternalThreadLocal.get("non-existent")).isNull(); + } + + @Test + void put_withValidKeyValue_shouldStoreValue() { + SdkInternalThreadLocal.put("test-key", "test-value"); + + String removed = SdkInternalThreadLocal.remove("test-key"); + + assertThat(removed).isEqualTo("test-value"); + assertThat(SdkInternalThreadLocal.get("test-key")).isNull(); + } + + @Test + void remove_withExistingKey_shouldRemoveAndReturnValue() { + SdkInternalThreadLocal.put("test-key", "test-value"); + SdkInternalThreadLocal.put("test-key", null); + + assertThat(SdkInternalThreadLocal.get("test-key")).isNull(); + } + + @Test + void clear_withMultipleValues_shouldRemoveAllValues() { + SdkInternalThreadLocal.put("key1", "value1"); + SdkInternalThreadLocal.put("key2", "value2"); + + SdkInternalThreadLocal.clear(); + + assertThat(SdkInternalThreadLocal.get("key1")).isNull(); + assertThat(SdkInternalThreadLocal.get("key2")).isNull(); + } +} \ No newline at end of file