diff --git a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreOptions.java b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreOptions.java index c8ad757b9..a07786dad 100644 --- a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreOptions.java +++ b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreOptions.java @@ -60,6 +60,7 @@ public final class FirestoreOptions extends ServiceOptions { @Nullable private String databaseId = null; @Nullable private TransportChannelProvider channelProvider = null; @Nullable private CredentialsProvider credentialsProvider = null; + @Nullable private String emulatorHost = null; private Builder() {} @@ -127,6 +133,7 @@ private Builder(FirestoreOptions options) { this.databaseId = options.databaseId; this.channelProvider = options.channelProvider; this.credentialsProvider = options.credentialsProvider; + this.emulatorHost = options.emulatorHost; } /** @@ -174,6 +181,17 @@ public Builder setCredentialsProvider(@Nonnull CredentialsProvider credentialsPr return this; } + /** + * Sets the emulator host to use with this Firestore client. The value passed to this method + * will take precedent if the {@code FIRESTORE_EMULATOR_HOST} environment variable is also set. + * + * @param emulatorHost The Firestore emulator host to use with this client. + */ + public Builder setEmulatorHost(@Nonnull String emulatorHost) { + this.emulatorHost = emulatorHost; + return this; + } + /** * Sets the database ID to use with this Firestore client. * @@ -196,7 +214,9 @@ public FirestoreOptions build() { } // Override credentials and channel provider if we are using the emulator. - String emulatorHost = System.getenv(FIRESTORE_EMULATOR_SYSTEM_VARIABLE); + if (emulatorHost == null) { + emulatorHost = System.getenv(FIRESTORE_EMULATOR_SYSTEM_VARIABLE); + } if (emulatorHost != null) { // Try creating a host in order to validate that the host name is valid. try { @@ -280,6 +300,8 @@ protected FirestoreOptions(Builder builder) { builder.credentialsProvider != null ? builder.credentialsProvider : GrpcTransportOptions.setUpCredentialsProvider(this); + + this.emulatorHost = builder.emulatorHost; } private static class FirestoreDefaults implements ServiceDefaults {