Skip to content
Closed
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
1 change: 1 addition & 0 deletions .brazil.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"s3-transfer-manager": { "packageName": "AwsJavaSdk-S3-TransferManager" },
"s3-event-notifications": { "packageName": "AwsJavaSdk-S3-EventNotifications" },
"sdk-core": { "packageName": "AwsJavaSdk-Core" },
"thread-context": { "packageName": "AwsJavaSdk-ThreadContext" },
"url-connection-client": { "packageName": "AwsJavaSdk-HttpClient-UrlConnectionClient" },
"utils": { "packageName": "AwsJavaSdk-Core-Utils" },
"imds": { "packageName": "AwsJavaSdk-Imds" },
Expand Down
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AWSSDKforJavav2-2e7c0a3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "AWS SDK for Java v2",
"contributor": "",
"description": "Adding a small utility class to store data on thread local that can be used across components."
}
5 changes: 5 additions & 0 deletions aws-sdk-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,11 @@ Amazon AutoScaling, etc).</description>
<artifactId>swf</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>thread-context</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>textract</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@
<artifactId>endpoints-spi</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>thread-context</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<!-- Services -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<module>archetypes</module>
<module>third-party</module>
<module>v2-migration</module>
<module>thread-context</module>
<module>test/http-client-tests</module>
<module>test/protocol-tests</module>
<module>test/protocol-tests-core</module>
Expand Down Expand Up @@ -658,6 +659,7 @@
<includeModule>protocols</includeModule>
<includeModule>regions</includeModule>
<includeModule>sdk-core</includeModule>
<includeModule>thread-context</includeModule>
<includeModule>http-client-spi</includeModule>
<includeModule>apache-client</includeModule>
<includeModule>netty-nio-client</includeModule>
Expand Down
5 changes: 5 additions & 0 deletions test/architecture-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
<groupId>software.amazon.awssdk</groupId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<artifactId>thread-context</artifactId>
<groupId>software.amazon.awssdk</groupId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<artifactId>s3</artifactId>
<groupId>software.amazon.awssdk</groupId>
Expand Down
5 changes: 5 additions & 0 deletions test/tests-coverage-reporting/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@
<groupId>software.amazon.awssdk</groupId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<artifactId>thread-context</artifactId>
<groupId>software.amazon.awssdk</groupId>
<version>${awsjavasdk.version}</version>
</dependency>

<!-- Need to explicitly add service modules to aggregate the tests coverage
and a few services that we know with more tests should be sufficient
Expand Down
74 changes: 74 additions & 0 deletions thread-context/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-sdk-java-pom</artifactId>
<version>2.33.2-SNAPSHOT</version>
</parent>
<artifactId>thread-context</artifactId>
<name>AWS Java SDK :: Thread Context</name>
<description>
Provides thread-local context storage utilities for sharing data across components.
</description>
<url>https://aws.amazon.com/sdkforjava</url>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom-internal</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Automatic-Module-Name>software.amazon.awssdk.threadcontext</Automatic-Module-Name>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.threadcontext;

import java.util.HashMap;
import java.util.Map;

public final class ThreadStorage {
private static final ThreadLocal<Map<String, String>> storage = ThreadLocal.withInitial(HashMap::new);

private ThreadStorage() {}

public static void put(String key, String value) {
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);
}
}
Original file line number Diff line number Diff line change
@@ -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.threadcontext;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

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

class ThreadStorageTest {

@AfterEach
void cleanup() {
ThreadStorage.clear();
}

@Test
void putAndGet_shouldStoreAndRetrieveValue() {
ThreadStorage.put("test-key", "test-value");

assertThat(ThreadStorage.get("test-key")).isEqualTo("test-value");
}

@Test
void get_withNonExistentKey_shouldReturnNull() {
assertThat(ThreadStorage.get("non-existent")).isNull();
}

@Test
void put_withValidKeyValue_shouldStoreValue() {
ThreadStorage.put("test-key", "test-value");

String removed = ThreadStorage.remove("test-key");

assertThat(removed).isEqualTo("test-value");
assertThat(ThreadStorage.get("test-key")).isNull();
}

@Test
void remove_withExistingKey_shouldRemoveAndReturnValue() {
ThreadStorage.put("test-key", "test-value");
ThreadStorage.put("test-key", null);

assertThat(ThreadStorage.get("test-key")).isNull();
}

@Test
void clear_withMultipleValues_shouldRemoveAllValues() {
ThreadStorage.put("key1", "value1");
ThreadStorage.put("key2", "value2");

ThreadStorage.clear();

assertThat(ThreadStorage.get("key1")).isNull();
assertThat(ThreadStorage.get("key2")).isNull();
}
}
Loading