Skip to content

Commit

Permalink
Merge pull request #307 from mziccard/add-options-get-app-name
Browse files Browse the repository at this point in the history
Add applicationName to ServiceOptions, set user agent accordingly in Storage
  • Loading branch information
aozarov committed Oct 29, 2015
2 parents d9c6b85 + f0fe56a commit ec10ca6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -56,6 +60,11 @@ public abstract class ServiceOptions<
private static final String DEFAULT_HOST = "https://www.googleapis.com";
private static final long serialVersionUID = 1203687993961393350L;
private static final String PROJECT_ENV_NAME = "GCLOUD_PROJECT";
private static final String MANIFEST_ARTIFACT_ID_KEY = "artifactId";
private static final String MANIFEST_VERSION_KEY = "Implementation-Version";
private static final String ARTIFACT_ID = "gcloud-java-core";
private static final String APPLICATION_BASE_NAME = "gcloud-java";
private static final String APPLICATION_NAME = getApplicationName();

private final String projectId;
private final String host;
Expand Down Expand Up @@ -522,6 +531,13 @@ public Clock clock() {
return clock;
}

/**
* Returns the application's name as a string in the format {@code gcloud-java/[version]}.
*/
public String applicationName() {
return APPLICATION_NAME;
}

protected int baseHashCode() {
return Objects.hash(projectId, host, httpTransportFactoryClassName, authCredentialsState,
retryParams, serviceFactoryClassName, serviceRpcFactoryClassName, connectTimeout,
Expand Down Expand Up @@ -568,4 +584,23 @@ private static <T> T newInstance(String className) throws IOException, ClassNotF
private static <T> T getFromServiceLoader(Class<? extends T> clazz, T defaultInstance) {
return Iterables.getFirst(ServiceLoader.load(clazz), defaultInstance);
}

private static String getApplicationName() {
String version = null;
try {
Enumeration<URL> resources =
ServiceOptions.class.getClassLoader().getResources(JarFile.MANIFEST_NAME);
while (resources.hasMoreElements() && version == null) {
Manifest manifest = new Manifest(resources.nextElement().openStream());
Attributes manifestAttributes = manifest.getMainAttributes();
String artifactId = manifestAttributes.getValue(MANIFEST_ARTIFACT_ID_KEY);
if (artifactId != null && artifactId.equals(ARTIFACT_ID)) {
version = manifestAttributes.getValue(MANIFEST_VERSION_KEY);
}
}
} catch (IOException e) {
// ignore
}
return version != null ? APPLICATION_BASE_NAME + "/" + version : APPLICATION_BASE_NAME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public DefaultStorageRpc(StorageOptions options) {
this.options = options;
storage = new Storage.Builder(transport, new JacksonFactory(), initializer)
.setRootUrl(options.host())
.setApplicationName("gcloud-java")
.setApplicationName(options.applicationName())
.build();
}

Expand Down

0 comments on commit ec10ca6

Please sign in to comment.