Skip to content

Commit

Permalink
chore: set Java header at native image runtime (#2069)
Browse files Browse the repository at this point in the history
* chore: set Java header during native image runtime
  • Loading branch information
mpeddada1 committed May 12, 2022
1 parent 3502186 commit ea078f8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Expand Up @@ -37,6 +37,7 @@
import com.google.api.client.http.UriTemplate;
import com.google.api.client.util.GenericData;
import com.google.api.client.util.Preconditions;
import com.google.common.base.Joiner;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -164,6 +165,16 @@ static class ApiClientVersion {
}

public String toString() {
// When running the application as a native image, append `-graalvm` to the
// version.
String imageCode = System.getProperty("org.graalvm.nativeimage.imagecode");
if (imageCode != null && imageCode.equals("runtime")) {
String[] tokens = versionString.split(" ");
if (tokens.length > 0 && tokens[0].startsWith("gl-java")) {
tokens[0] += "-graalvm";
return Joiner.on(" ").join(tokens);
}
}
return versionString;
}

Expand Down
Expand Up @@ -261,6 +261,14 @@ public void testSetsApiClientHeaderWithOsVersion() {
assertTrue("Api version should contain the os version", version.matches(".* my-os/1.2.3"));
}

public void testSetsApiClientHeader_NativeImage() throws IOException {
System.setProperty("org.graalvm.nativeimage.imagecode", "runtime");
System.setProperty("java.version", "11.0.0");
String version = new ApiClientVersion().toString();
assertTrue(
"Api version should contain -graalvm suffix", version.matches("gl-java/11.0.0-graalvm.*"));
}

public void testSetsApiClientHeaderWithoutOsVersion() {
System.setProperty("os.name", "My OS");
System.clearProperty("os.version");
Expand Down

0 comments on commit ea078f8

Please sign in to comment.