@@ -0,0 +1,2676 @@
+/*%0A * Copyright 2016 Exorath%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0Apackage com.exorath.service.treasurehunt.dynamodb;%0A%0Aimport com.amazonaws.auth.AWSCredentials;%0Aimport com.amazonaws.auth.EnvironmentVariableCredentialsProvider;%0Aimport com.amazonaws.regions.Regions;%0Aimport com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;%0Aimport com.amazonaws.services.dynamodbv2.document.DynamoDB;%0Aimport com.amazonaws.services.dynamodbv2.document.Table;%0Aimport com.amazonaws.services.dynamodbv2.model.CreateTableRequest;%0Aimport com.amazonaws.services.dynamodbv2.model.ResourceInUseException;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Apublic class DynamoDBProvider %7B%0A%09private static final Logger logger = LoggerFactory.getLogger(DynamoDBProvider.class);%0A%0A%09private final DynamoDB db;%0A%0A%09public DynamoDBProvider(Regions region) %7B%0A%09%09AWSCredentials credentials = new EnvironmentVariableCredentialsProvider().getCredentials();%0A%09%09AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials).withRegion(region);%0A%09%09db = new DynamoDB(client);%0A%09%7D%0A%0A%09public DynamoDB getDB() %7B%0A%09%09return db;%0A%09%7D%0A%0A%09public Table getTable(String name) %7B%0A%09%09return getDB().getTable(name);%0A%09%7D%0A%0A%09public boolean hasTable(String name) %7B%0A%09%09for (Table table : db.listTables()) %7B%0A%09%09%09if (table.getTableName().equals(name)) %7B%0A%09%09%09%09return true;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return false;%0A%09%7D%0A%0A%09public Table createTable(CreateTableRequest request) throws InterruptedException %7B%0A%09%09Table table;%0A%09%09try %7B%0A%09%09%09table = db.createTable(request);%0A%09%09%09String name = request.getTableName();%0A%09%09%09long read = request.getProvisionedThroughput().getReadCapacityUnits();%0A%09%09%09long write = request.getProvisionedThroughput().getWriteCapacityUnits();%0A%09%09%09logger.info(%22Created DynamoDB table %22 + name + %22 with %22 + read + %22r/%22 + write + %22w provisioning. Waiting for it to activate.%22);%0A%09%09%7D catch (ResourceInUseException ex) %7B // Table already exists%0A%09%09%09table = db.getTable(request.getTableName());%0A%09%09%09logger.warn(%22DynamoDB table %22 + request.getTableName() + %22 already exists. Waiting for it to activate.%22);%0A%09%09%7D%0A%0A%09%09table.waitForActive();%0A%09%09logger.info(%22DynamoDB table %22 + request.getTableName() + %22 is active.%22);%0A%09%09return table;%0A%09%7D%0A%7D%0A

Add methods for easy checking if db has table