Skip to content

Commit 13c2817

Browse files
Adding spring data es metadata to runtime info (#1058) (#1117)
* adding spring data es metadata to runtime info * change field name * change field name Co-authored-by: Laura Trotta <[email protected]>
1 parent 4fa418f commit 13c2817

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

java-client/src/main/java/co/elastic/clients/util/LanguageRuntimeVersions.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919

2020
package co.elastic.clients.util;
2121

22-
// Copied verbatim from https://github.com/elastic/jvm-languages-sniffer
22+
// Based on https://github.com/elastic/jvm-languages-sniffer
2323

2424
import java.lang.reflect.Field;
2525
import java.lang.reflect.Method;
26+
import java.util.Properties;
2627

2728
public class LanguageRuntimeVersions {
2829

@@ -59,6 +60,11 @@ public static String getRuntimeMetadata() {
5960
s.append(",jrb=").append(version);
6061
}
6162

63+
version = springDataVersion();
64+
if (version != null) {
65+
s.append(",sde=").append(version);
66+
}
67+
6268
return s.toString();
6369
}
6470

@@ -89,6 +95,18 @@ public static String jRubyVersion() {
8995
return keepMajorMinor(getStaticField("org.jruby.runtime.Constants", "VERSION"));
9096
}
9197

98+
public static String springDataVersion() {
99+
// org.springframework.data.elasticsearch.support.VersionInfo.versionProperties()
100+
Properties springProp = (Properties) callStaticMethodObject(
101+
"org.springframework.data.elasticsearch.support.VersionInfo",
102+
"versionProperties");
103+
if (springProp != null) {
104+
return keepMajorMinor(springProp.getProperty("version.spring-data-elasticsearch"));
105+
}
106+
return "";
107+
108+
}
109+
92110
private static String getStaticField(String className, String fieldName) {
93111
Class<?> clazz;
94112
try {
@@ -121,6 +139,22 @@ private static String callStaticMethod(String className, String methodName) {
121139
}
122140
}
123141

142+
private static Object callStaticMethodObject(String className, String methodName) {
143+
Class<?> clazz;
144+
try {
145+
clazz = Class.forName(className);
146+
} catch (ClassNotFoundException e) {
147+
return null;
148+
}
149+
150+
try {
151+
Method m = clazz.getMethod(methodName);
152+
return m.invoke(null);
153+
} catch (Exception e) {
154+
return null; // can't get version information
155+
}
156+
}
157+
124158
static String keepMajorMinor(String version) {
125159
if (version == null) {
126160
return null;

0 commit comments

Comments
 (0)