Skip to content

Commit

Permalink
check if added user agent is already in list
Browse files Browse the repository at this point in the history
  • Loading branch information
shubha-rajan committed Jun 23, 2020
1 parent 223a6ee commit b677b91
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions core/src/main/java/com/google/cloud/sql/core/CoreSocketFactory.java
Expand Up @@ -93,19 +93,7 @@ public final class CoreSocketFactory {
private final int serverProxyPort;

private static List<String> userAgents = new ArrayList<String>();
private static String version;

static {
try {
Properties packageInfo = new Properties();
packageInfo.load(CoreSocketFactory.class.getClassLoader().getResourceAsStream(
"com.google.cloud.sql.core/project.properties"));
version = packageInfo.getProperty("version");
} catch (IOException e) {
// leave version null
}

}
private static String version = getVersion();


@VisibleForTesting
Expand Down Expand Up @@ -350,15 +338,27 @@ private static KeyPair generateRsaKeyPair() {
return generator.generateKeyPair();
}

private static String getVersion() {
try {
Properties packageInfo = new Properties();
packageInfo.load(CoreSocketFactory.class.getClassLoader().getResourceAsStream(
"com.google.cloud.sql/project.properties"));
return packageInfo.getProperty("version");
} catch (IOException e) {
return "unknown";
}
}

/** Sets the default string which is appended to the SQLAdmin API client User-Agent header. */
public static void addUserAgent(String artifactId) {
if (version != null) {
userAgents.add(artifactId + "/" + version);
} else {
userAgents.add(artifactId);
String userAgent = artifactId + "/" + version;
if (!userAgents.contains(userAgent)) {
userAgents.add(userAgent);
}
}



/** Returns the default string which is appended to the SQLAdmin API client User-Agent header. */
public static String getUserAgentString() {
return String.join(" ", userAgents);
Expand Down

0 comments on commit b677b91

Please sign in to comment.