Skip to content

Commit ed44aa7

Browse files
authored
feat: Adding vendor and vendor information in header (#1963)
* chore: Adding vendor and vendor information in java-version header
1 parent 44701e8 commit ed44aa7

File tree

2 files changed

+128
-18
lines changed

2 files changed

+128
-18
lines changed

gax-java/gax/src/main/java/com/google/api/gax/core/GaxProperties.java

+23-12
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@
2929
*/
3030
package com.google.api.gax.core;
3131

32-
import static org.graalvm.nativeimage.ImageInfo.PROPERTY_IMAGE_CODE_KEY;
33-
import static org.graalvm.nativeimage.ImageInfo.PROPERTY_IMAGE_CODE_VALUE_RUNTIME;
34-
3532
import com.google.api.core.InternalApi;
33+
import com.google.common.annotations.VisibleForTesting;
34+
import com.google.common.base.Strings;
3635
import java.io.IOException;
3736
import java.io.InputStream;
3837
import java.util.Properties;
@@ -84,12 +83,6 @@ public static String getLibraryVersion(Class<?> libraryClass, String propertyNam
8483

8584
/** Returns the version of the running JVM */
8685
public static String getJavaVersion() {
87-
// When running the application as a native image, append `-graalvm` to the
88-
// version.
89-
String imageCode = System.getProperty(PROPERTY_IMAGE_CODE_KEY);
90-
if (imageCode != null && imageCode.equals(PROPERTY_IMAGE_CODE_VALUE_RUNTIME)) {
91-
return System.getProperty("java.version") + "-graalvm";
92-
}
9386
return JAVA_VERSION;
9487
}
9588

@@ -98,8 +91,26 @@ public static String getGaxVersion() {
9891
return GAX_VERSION;
9992
}
10093

101-
/** Returns the current runtime version */
102-
private static String getRuntimeVersion() {
103-
return System.getProperty("java.version");
94+
/**
95+
* Returns the current runtime version. For GraalVM the values in this method will be fetched at
96+
* build time and the values should not differ from the runtime (executable)
97+
*/
98+
@VisibleForTesting
99+
static String getRuntimeVersion() {
100+
String javaRuntimeInformation = System.getProperty("java.version", "null");
101+
102+
// append the vendor information to the java-version if vendor is present.
103+
String vendor = System.getProperty("java.vendor");
104+
if (!Strings.isNullOrEmpty(vendor)) {
105+
javaRuntimeInformation = String.format("%s__%s", javaRuntimeInformation, vendor);
106+
// appends the vendor version information to the java-version if vendor version is present.
107+
String vendorVersion = System.getProperty("java.vendor.version");
108+
if (!Strings.isNullOrEmpty(vendorVersion)) {
109+
javaRuntimeInformation = String.format("%s__%s", javaRuntimeInformation, vendorVersion);
110+
}
111+
}
112+
// replacing all characters that are not numbers, letters, underscores, periods, or backslashes
113+
// with hyphens.
114+
return javaRuntimeInformation.replaceAll("[^0-9a-zA-Z_\\\\.]", "-");
104115
}
105116
}

gax-java/gax/src/test/java/com/google/api/gax/core/GaxPropertiesTest.java

+105-6
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
*/
3030
package com.google.api.gax.core;
3131

32-
import static org.graalvm.nativeimage.ImageInfo.PROPERTY_IMAGE_CODE_KEY;
33-
import static org.graalvm.nativeimage.ImageInfo.PROPERTY_IMAGE_CODE_VALUE_RUNTIME;
32+
import static org.junit.Assert.assertEquals;
3433
import static org.junit.Assert.assertTrue;
3534

35+
import com.google.common.base.Strings;
3636
import java.util.regex.Pattern;
37+
import org.junit.After;
3738
import org.junit.Test;
3839
import org.junit.runner.RunWith;
3940
import org.junit.runners.JUnit4;
@@ -57,10 +58,108 @@ public void testGaxVersion() {
5758
}
5859
}
5960

61+
private static String originalJavaVersion = System.getProperty("java.version");
62+
private static String originalJavaVendor = System.getProperty("java.vendor");
63+
private static String originalJavaVendorVersion = System.getProperty("java.vendor.version");
64+
65+
@After
66+
public void cleanup() {
67+
if (Strings.isNullOrEmpty(originalJavaVersion)) {
68+
System.clearProperty("java.version");
69+
} else {
70+
System.setProperty("java.version", originalJavaVersion);
71+
}
72+
73+
if (Strings.isNullOrEmpty(originalJavaVendor)) {
74+
System.clearProperty("java.vendor");
75+
} else {
76+
System.setProperty("java.vendor", originalJavaVendor);
77+
}
78+
79+
if (Strings.isNullOrEmpty(originalJavaVendorVersion)) {
80+
System.clearProperty("java.vendor.version");
81+
} else {
82+
System.setProperty("java.vendor.version", originalJavaVendorVersion);
83+
}
84+
}
85+
86+
@Test
87+
public void testGetJavaRuntimeInfo_graalVM() {
88+
// This case is one of major Java vendors
89+
System.setProperty("java.version", "17.0.3");
90+
System.setProperty("java.vendor", "GraalVM Community");
91+
System.setProperty("java.vendor.version", "GraalVM CE 22.1.0");
92+
93+
String runtimeInfo = GaxProperties.getRuntimeVersion();
94+
assertEquals("17.0.3__GraalVM-Community__GraalVM-CE-22.1.0", runtimeInfo);
95+
}
96+
97+
@Test
98+
public void testGetJavaRuntimeInfo_temurin() {
99+
// This case is one of major Java vendors
100+
System.setProperty("java.version", "11.0.19");
101+
System.setProperty("java.vendor", "Eclipse Adoptium");
102+
System.setProperty("java.vendor.version", "Temurin-11.0.19+7");
103+
104+
String runtimeInfo = GaxProperties.getRuntimeVersion();
105+
assertEquals("11.0.19__Eclipse-Adoptium__Temurin-11.0.19-7", runtimeInfo);
106+
}
107+
108+
@Test
109+
public void testGetJavaRuntimeInfo_coretto() {
110+
// This case is one of major Java vendors
111+
System.setProperty("java.version", "11.0.19");
112+
System.setProperty("java.vendor", "Amazon.com Inc.");
113+
System.setProperty("java.vendor.version", "Corretto-11.0.19.7.1");
114+
115+
String runtimeInfo = GaxProperties.getRuntimeVersion();
116+
assertEquals("11.0.19__Amazon.com-Inc.__Corretto-11.0.19.7.1", runtimeInfo);
117+
}
118+
60119
@Test
61-
public void testGetVersion_nativeImage() {
62-
System.setProperty(PROPERTY_IMAGE_CODE_KEY, PROPERTY_IMAGE_CODE_VALUE_RUNTIME);
63-
String javaVersion = GaxProperties.getJavaVersion();
64-
assertTrue(javaVersion.endsWith("-graalvm"));
120+
public void testGetJavaRuntimeInfo_specialCharacters() {
121+
// testing for unsupported characters and spaces
122+
System.setProperty("java.version", "20%^.&0~.1#45`*");
123+
System.setProperty("java.vendor", "A^!@#$*B()[]{} C ~%& D-E ?");
124+
System.setProperty("java.vendor.version", "1!@%$@#.AB!346.9^");
125+
126+
String runtimeInfo = GaxProperties.getRuntimeVersion();
127+
assertEquals("20--.-0-.1-45--__A------B-------C-----D-E--__1------.AB-346.9-", runtimeInfo);
128+
}
129+
130+
@Test
131+
public void testGetJavaRuntimeInfo_nullVendorVersion() {
132+
// testing for null java.vendor.version
133+
System.setProperty("java.version", "20.0.1");
134+
System.setProperty("java.vendor", "Oracle");
135+
System.clearProperty("java.vendor.version");
136+
137+
String runtimeInfo = GaxProperties.getRuntimeVersion();
138+
assertEquals("20.0.1__Oracle", runtimeInfo);
139+
}
140+
141+
@Test
142+
public void testGetJavaRuntimeInfo_nullVendorAndVendorVersion() {
143+
// testing for null java.vendor and java.vendor.version
144+
System.setProperty("java.version", "20.0.1");
145+
System.clearProperty("java.vendor");
146+
System.clearProperty("java.vendor.version");
147+
148+
String runtimeInfo = GaxProperties.getRuntimeVersion();
149+
assertEquals("20.0.1", runtimeInfo);
150+
}
151+
152+
@Test
153+
public void testGetJavaRuntimeInfo_nullJavaVersion() {
154+
// testing for null java.version
155+
// We don't expect this case to happen, however we don't want the method to fail when it really
156+
// happens.
157+
158+
System.clearProperty("java.version");
159+
System.setProperty("java.vendor", "oracle");
160+
System.setProperty("java.vendor.version", "20.0.1");
161+
162+
String runtimeInfo = GaxProperties.getRuntimeVersion();
163+
assertEquals("null__oracle__20.0.1", runtimeInfo);
65164
}
66165
}

0 commit comments

Comments
 (0)