Created
November 14, 2022 14:19
-
-
Save manojahi/66b65d299882a2a52edf65ddb3c2ac1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DynamodbTableNameResolver extends DynamoDBMapperConfig.DefaultTableNameResolver { | |
private String region; | |
private String activeProfile; | |
private Environment environment = null; | |
public DynamodbTableNameResolver(String region, String activeProfile, Environment environment) { | |
this.region = region; | |
this.activeProfile = activeProfile; | |
this.environment = environment; | |
} | |
private static final String DELIMITER = "--"; | |
@Override | |
public String getTableName(Class<?> clazz, DynamoDBMapperConfig dynamoDBMapperConfig) { | |
if (environment == null) { | |
environment = AppContextAware.bean(Environment.class); | |
} | |
String tableNameToReturn; | |
DynamoDBTable dynamoDBTable = clazz.getAnnotation(DynamoDBTable.class); | |
if (dynamoDBTable == null) { | |
throw new RuntimeException("Not a dynamodb table to resolve name"); | |
} | |
tableNameToReturn = dynamoDBTable.tableName(); | |
DynamodbTableEnv dynamodbTableEnv = clazz.getAnnotation(DynamodbTableEnv.class); | |
if(dynamoDBTable.tableName().isEmpty() && (dynamodbTableEnv==null || dynamodbTableEnv.value().isEmpty())){ | |
throw new RuntimeException("Table name in DynamoDBTable or DynamodbTableEnv is mandatory"); | |
} | |
//If the @DynamoDBTable has value and no annotation @DynamodbTableEnv is present then the region and profile | |
// is considered to form the full name. Below is the example | |
//Ex : users--ap-south-1--prod, users--ca-central-1--dev | |
if (dynamodbTableEnv == null) { | |
return tableNameToReturn + DELIMITER + region + DELIMITER + activeProfile; | |
} else { | |
//if a value is present annotation @DynamodbTableEnv then it is treated as the key from the environment | |
return environment.getProperty(dynamodbTableEnv.value()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment