Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package in.net.cadentic.core.database;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- import com.amazonaws.auth.AWSCredentials;
- import com.amazonaws.auth.AWSStaticCredentialsProvider;
- import com.amazonaws.client.builder.AwsClientBuilder;
- import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
- import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
- /**
- * The Singleton Class AmazonDynamoDBClientHandler - creates and manages the
- * client instance and all its Amazon AWS API calls.
- *
- *
- * @author Sandeep
- * @version 1.0
- * @see {@link https://docs.aws.amazon.com/general/latest/gr/ddb.html}
- * @see {@link https://sdk-for-java.amazonwebservices.com/latest/aws-java-sdk.zip}
- */
- public class AmazonDynamoDBClientHandler {
- /** The Constant PROPERTIES_FILE. */
- private static final String PROPERTIES_FILE = "aws.properties";
- /** The Constant PROPERTIES. */
- private static final Properties PROPERTIES = new Properties();
- /** The Constant AWS_ACCESS_KEYID. */
- private static final String AWS_ACCESS_KEYID = "AWSAccessKeyId";
- /** The Constant AWS_Secret_KEY. */
- private static final String AWS_Secret_KEY = "AWSSecretKey";
- /** The Constant SERVICE_END_POINT. */
- private static final String SERVICE_END_POINT = "serviceEndPoint";
- /** The Constant SIGNING_REGION. */
- private static final String SIGNING_REGION = "signingRegion";
- /** The AmazonDynamoDB */
- private AmazonDynamoDB amazonDynamoDB = null;
- /**
- * Instantiates a new AmazonDynamoDBClientHandler.
- */
- private AmazonDynamoDBClientHandler() {
- }
- /**
- * Gets the single instance of AmazonDynamoDBClientHandler.
- *
- * @return single instance of AmazonDynamoDBClientHandler
- */
- public static AmazonDynamoDBClientHandler getNewInstance() {
- return new AmazonDynamoDBClientHandler();
- }
- /**
- * Creates a new Amazon AWS DynamoDB client.
- *
- * @return the Amazon AWS DynamoDB client
- * @throws IOException
- */
- public AmazonDynamoDB createNewClient() throws IOException {
- // ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
- // final ClassLoader classLoader = getClass().getClassLoader();
- InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
- PROPERTIES.load(inputStream);
- if (amazonDynamoDB == null) {
- amazonDynamoDB = AmazonDynamoDBClientBuilder.standard()
- .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(
- PROPERTIES.getProperty(SERVICE_END_POINT), PROPERTIES.getProperty(SIGNING_REGION)))
- .withCredentials(new AWSStaticCredentialsProvider(new AWSCredentials() {
- @Override
- public String getAWSAccessKeyId() {
- return PROPERTIES.getProperty(AWS_ACCESS_KEYID);
- }
- @Override
- public String getAWSSecretKey() {
- return PROPERTIES.getProperty(AWS_Secret_KEY);
- }
- })).build();
- }
- return amazonDynamoDB;
- }
- /**
- * Shuts down client instance releasing any resources that might be held open.
- *
- */
- public void shutdownClient() {
- amazonDynamoDB.shutdown();
- }
- }
Add Comment
Please, Sign In to add comment