Skip to content

Commit eaa61ed

Browse files
authored
Deprecate USER_TOKEN_PROPERTY_NAME and add method alternatives. (#133)
1 parent f108b0d commit eaa61ed

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

core/src/main/java/com/google/cloud/sql/core/CoreSocketFactory.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,17 @@ public final class CoreSocketFactory {
8787
public static final String MYSQL_SOCKET_FILE_FORMAT = "/cloudsql/%s";
8888
public static final String POSTGRES_SOCKET_FILE_FORMAT = "/cloudsql/%s/.s.PGSQL.5432";
8989

90+
/**
91+
* Property used to set the application name for the underlying SQLAdmin client.
92+
*
93+
* @deprecated Use {@link #setApplicationName(String)} to set the application name
94+
* programmatically.
95+
*/
96+
@Deprecated public static final String USER_TOKEN_PROPERTY_NAME = "_CLOUD_SQL_USER_TOKEN";
97+
9098
private static final Logger logger = Logger.getLogger(CoreSocketFactory.class.getName());
9199

92100
private static final String DEFAULT_IP_TYPES = "PUBLIC,PRIVATE";
93-
private static final String USER_TOKEN_PROPERTY_NAME = "_CLOUD_SQL_USER_TOKEN";
94101

95102
static final String ADMIN_API_NOT_ENABLED_REASON = "accessNotConfigured";
96103
static final String INSTANCE_NOT_AUTHORIZED_REASON = "notAuthorized";
@@ -614,12 +621,11 @@ private static SQLAdmin createAdminApiClient(HttpRequestInitializer requestIniti
614621

615622
String rootUrl = System.getProperty(API_ROOT_URL_PROPERTY);
616623
String servicePath = System.getProperty(API_SERVICE_PATH_PROPERTY);
617-
String userToken = System.getProperty(USER_TOKEN_PROPERTY_NAME);
618624

619625
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
620626
SQLAdmin.Builder adminApiBuilder =
621627
new Builder(httpTransport, jsonFactory, requestInitializer)
622-
.setApplicationName(userToken != null ? userToken : "Cloud SQL Java Socket Factory");
628+
.setApplicationName(getApplicationName());
623629
if (rootUrl != null) {
624630
logTestPropertyWarning(API_ROOT_URL_PROPERTY);
625631
adminApiBuilder.setRootUrl(rootUrl);
@@ -723,6 +729,27 @@ public SSLSocketFactory getSslSocketFactory() {
723729
}
724730
}
725731

732+
/**
733+
* Sets the User-Agent header for requests made using the underlying SQLAdmin API client.
734+
*
735+
* @throws IllegalStateException if the SQLAdmin client has already been initialized
736+
*/
737+
public static void setApplicationName(String applicationName) {
738+
if (coreSocketFactory != null) {
739+
throw new IllegalStateException(
740+
"Unable to set ApplicationName - SQLAdmin client already initialized.");
741+
}
742+
System.setProperty(USER_TOKEN_PROPERTY_NAME, applicationName);
743+
}
744+
745+
/** Returns the current User-Agent header set for the underlying SQLAdmin API client. */
746+
public static String getApplicationName() {
747+
if (coreSocketFactory != null) {
748+
return coreSocketFactory.adminApi.getApplicationName();
749+
}
750+
return System.getProperty(USER_TOKEN_PROPERTY_NAME, "Cloud SQL Java Socket Factory");
751+
}
752+
726753
@VisibleForTesting
727754
enum CertificateCaching {
728755
USE_CACHE,

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
</developer>
5555
</developers>
5656

57+
<properties>
58+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
59+
</properties>
60+
5761
<modules>
5862
<module>core</module>
5963
<module>connector-j-5</module>

0 commit comments

Comments
 (0)