4848import java .util .Enumeration ;
4949import java .util .Locale ;
5050import java .util .Objects ;
51+ import java .util .Properties ;
5152import java .util .ServiceLoader ;
5253import java .util .Set ;
5354import java .util .jar .Attributes ;
@@ -72,12 +73,15 @@ public abstract class ServiceOptions<ServiceT extends Service<OptionsT>, Service
7273 private static final String MANIFEST_ARTIFACT_ID_KEY = "artifactId" ;
7374 private static final String MANIFEST_VERSION_KEY = "Implementation-Version" ;
7475 private static final String ARTIFACT_ID = "google-cloud-core" ;
75- private static final String LIBRARY_NAME = "gcloud -java" ;
76+ private static final String LIBRARY_NAME = "google-cloud -java" ;
7677 private static final String LIBRARY_VERSION = defaultLibraryVersion ();
7778 private static final String APPLICATION_NAME =
7879 LIBRARY_VERSION == null ? LIBRARY_NAME : LIBRARY_NAME + "/" + LIBRARY_VERSION ;
7980 private static final long serialVersionUID = -5714029257168617973L ;
8081
82+ private static final String META_FILE_ROOT = "/META-INF/maven/" ;
83+ private static final String META_VERSION_KEY = "version" ;
84+
8185 private final String projectId ;
8286 private final String host ;
8387 private final RetryParams retryParams ;
@@ -566,30 +570,30 @@ public Clock getClock() {
566570 }
567571
568572 /**
569- * Returns the application's name as a string in the format {@code gcloud -java/[version]}.
573+ * Returns the application's name as a string in the format {@code google-cloud -java/[version]}.
570574 */
571575 @ Deprecated
572576 public String applicationName () {
573577 return getApplicationName ();
574578 }
575579
576580 /**
577- * Returns the application's name as a string in the format {@code gcloud -java/[version]}.
581+ * Returns the application's name as a string in the format {@code google-cloud -java/[version]}.
578582 */
579583 public String getApplicationName () {
580584 return APPLICATION_NAME ;
581585 }
582586
583587 /**
584- * Returns the library's name, {@code gcloud -java}, as a string.
588+ * Returns the library's name, {@code google-cloud -java}, as a string.
585589 */
586590 @ Deprecated
587591 public String libraryName () {
588592 return getLibraryName ();
589593 }
590594
591595 /**
592- * Returns the library's name, {@code gcloud -java}, as a string.
596+ * Returns the library's name, {@code google-cloud -java}, as a string.
593597 */
594598 public String getLibraryName () {
595599 return LIBRARY_NAME ;
@@ -676,6 +680,31 @@ static <T> T getFromServiceLoader(Class<? extends T> clazz, T defaultInstance) {
676680 }
677681
678682 private static String defaultLibraryVersion () {
683+ String version = getPomVersion ();
684+ if (version == null ) {
685+ version = getManifestVersion ();
686+ }
687+ return version ;
688+ }
689+
690+ private static String getPomVersion () {
691+ try {
692+ Properties properties = new Properties ();
693+ String mavenPropertiesPath = META_FILE_ROOT
694+ + ServiceOptions .class .getPackage ().getName () + "/"
695+ + ARTIFACT_ID + "/pom.properties" ;
696+ InputStream inputStream = ServiceOptions .class .getResourceAsStream (mavenPropertiesPath );
697+ if (inputStream != null ) {
698+ properties .load (inputStream );
699+ return properties .getProperty (META_VERSION_KEY , "" );
700+ }
701+ } catch (Exception e ) {
702+ // ignore
703+ }
704+ return null ;
705+ }
706+
707+ private static String getManifestVersion () {
679708 String version = null ;
680709 try {
681710 Enumeration <URL > resources =
0 commit comments