use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.
the class ConnectionHandler method connectToSpanner.
@InternalApi
public void connectToSpanner(String database) {
OptionsMetadata options = getServer().getOptions();
String uri = options.hasDefaultConnectionUrl() ? options.getDefaultConnectionUrl() : options.buildConnectionURL(database);
if (uri.startsWith("jdbc:")) {
uri = uri.substring("jdbc:".length());
}
uri = appendPropertiesToUrl(uri, getServer().getProperties());
if (System.getProperty(CHANNEL_PROVIDER_PROPERTY) != null) {
uri = uri + ";" + ConnectionOptions.CHANNEL_PROVIDER_PROPERTY_NAME + "=" + System.getProperty(CHANNEL_PROVIDER_PROPERTY);
// This forces the connection to use NoCredentials.
uri = uri + ";usePlainText=true";
try {
Class.forName(System.getProperty(CHANNEL_PROVIDER_PROPERTY));
} catch (ClassNotFoundException e) {
throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Unknown or invalid channel provider: " + System.getProperty(CHANNEL_PROVIDER_PROPERTY));
}
}
ConnectionOptions connectionOptions = ConnectionOptions.newBuilder().setUri(uri).build();
Connection spannerConnection = connectionOptions.getConnection();
try {
// invalid, for example as a result of the credentials being wrong.
if (spannerConnection.getDialect() != Dialect.POSTGRESQL) {
throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, String.format("The database uses dialect %s. Currently PGAdapter only supports connections to PostgreSQL dialect databases. " + "These can be created using https://cloud.google.com/spanner/docs/quickstart-console#postgresql", spannerConnection.getDialect()));
}
} catch (SpannerException e) {
spannerConnection.close();
throw e;
}
this.spannerConnection = spannerConnection;
}
use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.
the class Server method main.
/**
* Main method for running a Spanner PostgreSQL Adaptor {@link Server} as a stand-alone
* application. Here we call for parameter parsing and start the Proxy Server.
*/
public static void main(String[] args) {
try {
System.out.printf("-- Starting PGAdapter version %s --\n", getVersion());
OptionsMetadata optionsMetadata = new OptionsMetadata(args);
ProxyServer server = new ProxyServer(optionsMetadata);
server.startServer();
} catch (Exception e) {
System.err.println("The server could not be started because an error occurred: " + (e.getMessage() == null ? e.toString() : e.getMessage()));
System.out.println("Run with option -h or --help to get help");
System.out.printf("Version: %s\n", getVersion());
}
}
use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.
the class AbstractMockServerTest method startMockSpannerAndPgAdapterServers.
@BeforeClass
public static void startMockSpannerAndPgAdapterServers() throws Exception {
mockSpanner = new MockSpannerServiceImpl();
// We don't want any unpredictable aborted transactions.
mockSpanner.setAbortProbability(0.0D);
mockSpanner.putStatementResult(StatementResult.query(SELECT1, SELECT1_RESULTSET));
mockSpanner.putStatementResult(StatementResult.query(SELECT2, SELECT2_RESULTSET));
mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
mockSpanner.putStatementResult(StatementResult.update(INSERT_STATEMENT, INSERT_COUNT));
mockSpanner.putStatementResult(MockSpannerServiceImpl.StatementResult.detectDialectResult(Dialect.POSTGRESQL));
mockOperationsService = new MockOperationsServiceImpl();
mockDatabaseAdmin = new MockDatabaseAdminServiceImpl(mockOperationsService);
InetSocketAddress address = new InetSocketAddress("localhost", 0);
spannerServer = NettyServerBuilder.forAddress(address).addService(mockSpanner).addService(mockDatabaseAdmin).addService(mockOperationsService).build().start();
// Create the test database on the mock server. This should be replaced by a simple feature in
// the mock server to just add a database instead of having to simulate the creation of it.
createDatabase();
ImmutableList.Builder<String> argsListBuilder = ImmutableList.<String>builder().add("-p", "p", "-i", "i", "-d", "d", "-c", // empty credentials file, as we are using a plain text connection.
"", "-s", // port 0 to let the OS pick an available port
"0", "-e", String.format("localhost:%d", spannerServer.getPort()), "-r", "usePlainText=true;");
String[] args = argsListBuilder.build().toArray(new String[0]);
pgServer = new ProxyServer(new OptionsMetadata(args));
pgServer.startServer();
}
Aggregations