|
19 | 19 |
|
20 | 20 | package co.elastic.clients.util; |
21 | 21 |
|
22 | | -// Copied verbatim from https://github.com/elastic/jvm-languages-sniffer |
| 22 | +// Based on https://github.com/elastic/jvm-languages-sniffer |
23 | 23 |
|
24 | 24 | import java.lang.reflect.Field; |
25 | 25 | import java.lang.reflect.Method; |
| 26 | +import java.util.Properties; |
26 | 27 |
|
27 | 28 | public class LanguageRuntimeVersions { |
28 | 29 |
|
@@ -59,6 +60,11 @@ public static String getRuntimeMetadata() { |
59 | 60 | s.append(",jrb=").append(version); |
60 | 61 | } |
61 | 62 |
|
| 63 | + version = springDataVersion(); |
| 64 | + if (version != null) { |
| 65 | + s.append(",sde=").append(version); |
| 66 | + } |
| 67 | + |
62 | 68 | return s.toString(); |
63 | 69 | } |
64 | 70 |
|
@@ -89,6 +95,18 @@ public static String jRubyVersion() { |
89 | 95 | return keepMajorMinor(getStaticField("org.jruby.runtime.Constants", "VERSION")); |
90 | 96 | } |
91 | 97 |
|
| 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 | + |
92 | 110 | private static String getStaticField(String className, String fieldName) { |
93 | 111 | Class<?> clazz; |
94 | 112 | try { |
@@ -121,6 +139,22 @@ private static String callStaticMethod(String className, String methodName) { |
121 | 139 | } |
122 | 140 | } |
123 | 141 |
|
| 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 | + |
124 | 158 | static String keepMajorMinor(String version) { |
125 | 159 | if (version == null) { |
126 | 160 | return null; |
|
0 commit comments