|  | 
|  | 1 | +package software.aws.mcs.auth; | 
|  | 2 | + | 
|  | 3 | +/*- | 
|  | 4 | + * #%L | 
|  | 5 | + * AWS SigV4 Auth Java Driver 4.x Plugin | 
|  | 6 | + * %% | 
|  | 7 | + * Copyright (C) 2020-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | 
|  | 8 | + * %% | 
|  | 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 10 | + * you may not use this file except in compliance with the License. | 
|  | 11 | + * You may obtain a copy of the License at | 
|  | 12 | + * | 
|  | 13 | + * http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 14 | + * | 
|  | 15 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 16 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 18 | + * See the License for the specific language governing permissions and | 
|  | 19 | + * limitations under the License. | 
|  | 20 | + * #L% | 
|  | 21 | + */ | 
|  | 22 | + | 
|  | 23 | +import java.io.File; | 
|  | 24 | +import java.net.InetSocketAddress; | 
|  | 25 | +import java.net.URL; | 
|  | 26 | +import java.util.ArrayList; | 
|  | 27 | + | 
|  | 28 | +import com.datastax.oss.driver.api.core.CqlSession; | 
|  | 29 | +import com.datastax.oss.driver.api.core.config.DriverConfigLoader; | 
|  | 30 | +import com.datastax.oss.driver.api.core.cql.ResultSet; | 
|  | 31 | +import com.datastax.oss.driver.api.core.cql.Row; | 
|  | 32 | + | 
|  | 33 | +public class TestSigV4Config { | 
|  | 34 | + static String[] DEFAULT_CONTACT_POINTS = {"127.0.0.1:9042"}; | 
|  | 35 | + | 
|  | 36 | + public static void main(String[] args) throws Exception { | 
|  | 37 | + String[] contactPointsRaw = DEFAULT_CONTACT_POINTS; | 
|  | 38 | + | 
|  | 39 | + if (args.length == 1) { | 
|  | 40 | + contactPointsRaw = args[0].split(","); | 
|  | 41 | + } else if (args.length > 1) { | 
|  | 42 | + System.out.println("Usage: TestSigV4 [<contact points, comma separated, 'IP:port' format>]"); | 
|  | 43 | + System.exit(-1); | 
|  | 44 | + } | 
|  | 45 | + | 
|  | 46 | + ArrayList<InetSocketAddress> contactPoints = new ArrayList<>(contactPointsRaw.length); | 
|  | 47 | + | 
|  | 48 | + for (int i = 0; i < contactPointsRaw.length; i++) { | 
|  | 49 | + String[] parts = contactPointsRaw[i].split(":"); | 
|  | 50 | + contactPoints.add(InetSocketAddress.createUnresolved(parts[0], Integer.parseInt(parts[1]))); | 
|  | 51 | + } | 
|  | 52 | + | 
|  | 53 | + System.out.println("Using endpoints: " + contactPoints); | 
|  | 54 | + | 
|  | 55 | + //By default the reference.conf is loaded by the driver which contains all defaults. | 
|  | 56 | + //You can override this by providing reference.conf on the classpath | 
|  | 57 | + //to isolate test you can load conf with a custom name | 
|  | 58 | + URL url = TestSigV4Config.class.getClassLoader().getResource("keyspaces-reference.conf"); | 
|  | 59 | + | 
|  | 60 | + File file = new File(url.toURI()); | 
|  | 61 | + // The CqlSession object is the main entry point of the driver. | 
|  | 62 | + // It holds the known state of the actual Cassandra cluster (notably the Metadata). | 
|  | 63 | + // This class is thread-safe, you should create a single instance (per target Cassandra cluster), and share | 
|  | 64 | + // it throughout your application. | 
|  | 65 | + try (CqlSession session = CqlSession.builder() | 
|  | 66 | + .withConfigLoader(DriverConfigLoader.fromFile(file)) | 
|  | 67 | + .addContactPoints(contactPoints) | 
|  | 68 | + .withLocalDatacenter("us-west-2") | 
|  | 69 | + .build()) { | 
|  | 70 | + | 
|  | 71 | + // We use execute to send a query to Cassandra. This returns a ResultSet, which is essentially a collection | 
|  | 72 | + // of Row objects. | 
|  | 73 | + ResultSet rs = session.execute("select release_version from system.local"); | 
|  | 74 | + // Extract the first row (which is the only one in this case). | 
|  | 75 | + Row row = rs.one(); | 
|  | 76 | + | 
|  | 77 | + // Extract the value of the first (and only) column from the row. | 
|  | 78 | + String releaseVersion = row.getString("release_version"); | 
|  | 79 | + System.out.printf("Cassandra version is: %s%n", releaseVersion); | 
|  | 80 | + } | 
|  | 81 | + } | 
|  | 82 | +} | 
0 commit comments