/* * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: MIT-0 * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * software and associated documentation files (the "Software"), to deal in the Software * without restriction, including without limitation the rights to use, copy, modify, * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package software.amazon.qldb.tutorial; import java.net.URI; import java.net.URISyntaxException; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; import software.amazon.awssdk.services.qldbsession.QldbSessionClient; import software.amazon.awssdk.services.qldbsession.QldbSessionClientBuilder; import software.amazon.qldb.QldbDriver; import software.amazon.qldb.RetryPolicy; /** * Connect to a session for a given ledger using default settings. *

* This code expects that you have AWS credentials setup per: * http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html */ public final class ConnectToLedger { public static final Logger log = LoggerFactory.getLogger(ConnectToLedger.class); public static AwsCredentialsProvider credentialsProvider; public static String endpoint = null; public static String ledgerName = Constants.LEDGER_NAME; public static String region = null; public static QldbDriver driver; private ConnectToLedger() { } /** * Create a pooled driver for creating sessions. * * @param retryAttempts How many times the transaction will be retried in * case of a retryable issue happens like Optimistic Concurrency Control exception, * server side failures or network issues. * @return The pooled driver for creating sessions. */ public static QldbDriver createQldbDriver(int retryAttempts) { QldbSessionClientBuilder builder = getAmazonQldbSessionClientBuilder(); return QldbDriver.builder() .ledger(ledgerName) .transactionRetryPolicy(RetryPolicy .builder() .maxRetries(retryAttempts) .build()) .sessionClientBuilder(builder) .build(); } /** * Create a pooled driver for creating sessions. * * @return The pooled driver for creating sessions. */ public static QldbDriver createQldbDriver() { QldbSessionClientBuilder builder = getAmazonQldbSessionClientBuilder(); return QldbDriver.builder() .ledger(ledgerName) .transactionRetryPolicy(RetryPolicy.builder() .maxRetries(Constants.RETRY_LIMIT).build()) .sessionClientBuilder(builder) .build(); } /** * Creates a QldbSession builder that is passed to the QldbDriver to connect to the Ledger. * * @return An instance of the AmazonQLDBSessionClientBuilder */ public static QldbSessionClientBuilder getAmazonQldbSessionClientBuilder() { QldbSessionClientBuilder builder = QldbSessionClient.builder(); if (null != endpoint && null != region) { try { builder.endpointOverride(new URI(endpoint)); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } } if (null != credentialsProvider) { builder.credentialsProvider(credentialsProvider); } return builder; } /** * Create a pooled driver for creating sessions. * * @return The pooled driver for creating sessions. */ public static QldbDriver getDriver() { if (driver == null) { driver = createQldbDriver(); } return driver; } public static void main(final String... args) { Iterable tables = ConnectToLedger.getDriver().getTableNames(); log.info("Existing tables in the ledger:"); for (String table : tables) { log.info("- {} ", table); } } }