_Csandeep

Amazon Dynamo DB Connect

Jul 20th, 2020 (edited)
2,393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. package in.net.cadentic.core.database;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.Properties;
  6.  
  7. import com.amazonaws.auth.AWSCredentials;
  8. import com.amazonaws.auth.AWSStaticCredentialsProvider;
  9. import com.amazonaws.client.builder.AwsClientBuilder;
  10. import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
  11. import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
  12.  
  13. /**
  14.  * The Singleton Class AmazonDynamoDBClientHandler - creates and manages the
  15.  * client instance and all its Amazon AWS API calls.
  16.  *
  17.  *
  18.  * @author Sandeep
  19.  * @version 1.0
  20.  * @see {@link https://docs.aws.amazon.com/general/latest/gr/ddb.html}
  21.  * @see {@link https://sdk-for-java.amazonwebservices.com/latest/aws-java-sdk.zip}
  22.  */
  23. public class AmazonDynamoDBClientHandler {
  24.  
  25.     /** The Constant PROPERTIES_FILE. */
  26.     private static final String PROPERTIES_FILE = "aws.properties";
  27.  
  28.     /** The Constant PROPERTIES. */
  29.     private static final Properties PROPERTIES = new Properties();
  30.  
  31.     /** The Constant AWS_ACCESS_KEYID. */
  32.     private static final String AWS_ACCESS_KEYID = "AWSAccessKeyId";
  33.  
  34.     /** The Constant AWS_Secret_KEY. */
  35.     private static final String AWS_Secret_KEY = "AWSSecretKey";
  36.  
  37.     /** The Constant SERVICE_END_POINT. */
  38.     private static final String SERVICE_END_POINT = "serviceEndPoint";
  39.  
  40.     /** The Constant SIGNING_REGION. */
  41.     private static final String SIGNING_REGION = "signingRegion";
  42.  
  43.     /** The AmazonDynamoDB */
  44.     private AmazonDynamoDB amazonDynamoDB = null;
  45.  
  46.     /**
  47.      * Instantiates a new AmazonDynamoDBClientHandler.
  48.      */
  49.     private AmazonDynamoDBClientHandler() {
  50.     }
  51.  
  52.     /**
  53.      * Gets the single instance of AmazonDynamoDBClientHandler.
  54.      *
  55.      * @return single instance of AmazonDynamoDBClientHandler
  56.      */
  57.     public static AmazonDynamoDBClientHandler getNewInstance() {
  58.         return new AmazonDynamoDBClientHandler();
  59.     }
  60.  
  61.     /**
  62.      * Creates a new Amazon AWS DynamoDB client.
  63.      *
  64.      * @return the Amazon AWS DynamoDB client
  65.      * @throws IOException
  66.      */
  67.     public AmazonDynamoDB createNewClient() throws IOException {
  68.  
  69.         // ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  70.  
  71.         // final ClassLoader classLoader = getClass().getClassLoader();
  72.  
  73.         InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
  74.  
  75.         PROPERTIES.load(inputStream);
  76.  
  77.         if (amazonDynamoDB == null) {
  78.             amazonDynamoDB = AmazonDynamoDBClientBuilder.standard()
  79.                     .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(
  80.                             PROPERTIES.getProperty(SERVICE_END_POINT), PROPERTIES.getProperty(SIGNING_REGION)))
  81.                     .withCredentials(new AWSStaticCredentialsProvider(new AWSCredentials() {
  82.                         @Override
  83.                         public String getAWSAccessKeyId() {
  84.                             return PROPERTIES.getProperty(AWS_ACCESS_KEYID);
  85.                         }
  86.  
  87.                         @Override
  88.                         public String getAWSSecretKey() {
  89.                             return PROPERTIES.getProperty(AWS_Secret_KEY);
  90.                         }
  91.                     })).build();
  92.         }
  93.         return amazonDynamoDB;
  94.     }
  95.  
  96.     /**
  97.      * Shuts down client instance releasing any resources that might be held open.
  98.      *
  99.      */
  100.     public void shutdownClient() {
  101.         amazonDynamoDB.shutdown();
  102.     }
  103. }
Add Comment
Please, Sign In to add comment