Skip to content

Commit 15e2e2d

Browse files
authored
Make defaultLibraryVersion() more robust (#1436)
1 parent 273de20 commit 15e2e2d

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.Enumeration;
4949
import java.util.Locale;
5050
import java.util.Objects;
51+
import java.util.Properties;
5152
import java.util.ServiceLoader;
5253
import java.util.Set;
5354
import 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 =

google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public class ServiceOptionsTest {
102102
private static final TestServiceOptions DEPRECATED_DEFAULT_OPTIONS =
103103
TestServiceOptions.newBuilder().projectId("project-id").build();
104104
private static final TestServiceOptions OPTIONS_COPY = OPTIONS.toBuilder().build();
105-
private static final String LIBRARY_NAME = "gcloud-java";
105+
private static final String LIBRARY_NAME = "google-cloud-java";
106106
private static final Pattern APPLICATION_NAME_PATTERN =
107107
Pattern.compile(LIBRARY_NAME + "(/[0-9]+.[0-9]+.[0-9]+)?");
108108

0 commit comments

Comments
 (0)