Skip to content

chore: set Java header at native image runtime #2069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use constants instead of string literals here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a valid point! However, this might come down to the question of whether it is worth having dependency on graal-sdk in this repo? If we use the latest version (22.1.0) then we'll see the following incompatibility:

bad class file: /home/runner/.m2/repository/org/graalvm/sdk/graal-sdk/22.1.0/graal-sdk-22.1.0.jar(org/graalvm/nativeimage/ImageInfo.class)
Error:      class file has wrong version 55.0, should be 52.0
Error:      Please remove or make sure it appears in the correct subdirectory of the classpath.
Error:  -> [Help 1]

In which case, we may need to do something similar to googleapis/gax-java#1671. This could still pose a slight risk since we still target Java 7. But open to hearing about what you think!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with either. @suztomo what do you think? Is it worth it to bring the whole setup for compiling with 11 and testing on 7 in this case?

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
Original file line number Diff line number Diff line change
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