|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package co.elastic.clients.transport.rest_client; |
| 21 | + |
| 22 | +// Copied verbatim from https://github.com/elastic/jvm-languages-sniffer |
| 23 | + |
| 24 | +import java.lang.reflect.Field; |
| 25 | +import java.lang.reflect.Method; |
| 26 | + |
| 27 | +class LanguageRuntimeVersions { |
| 28 | + |
| 29 | + /** |
| 30 | + * Returns runtime information by looking up classes identifying non-Java JVM |
| 31 | + * languages and appending a key with their name and their major.minor version, if available |
| 32 | + */ |
| 33 | + public static String getRuntimeMetadata() { |
| 34 | + StringBuilder s = new StringBuilder(); |
| 35 | + String version; |
| 36 | + |
| 37 | + version = kotlinVersion(); |
| 38 | + if (version != null) { |
| 39 | + s.append(",kt=").append(version); |
| 40 | + } |
| 41 | + |
| 42 | + version = scalaVersion(); |
| 43 | + if (version != null) { |
| 44 | + s.append(",sc=").append(version); |
| 45 | + } |
| 46 | + |
| 47 | + version = clojureVersion(); |
| 48 | + if (version != null) { |
| 49 | + s.append(",clj=").append(version); |
| 50 | + } |
| 51 | + |
| 52 | + version = groovyVersion(); |
| 53 | + if (version != null) { |
| 54 | + s.append(",gy=").append(version); |
| 55 | + } |
| 56 | + |
| 57 | + version = jRubyVersion(); |
| 58 | + if (version != null) { |
| 59 | + s.append(",jrb=").append(version); |
| 60 | + } |
| 61 | + |
| 62 | + return s.toString(); |
| 63 | + } |
| 64 | + |
| 65 | + public static String kotlinVersion() { |
| 66 | + // KotlinVersion.CURRENT.toString() |
| 67 | + return keepMajorMinor(getStaticField("kotlin.KotlinVersion", "CURRENT")); |
| 68 | + } |
| 69 | + |
| 70 | + public static String scalaVersion() { |
| 71 | + // scala.util.Properties.versionNumberString() |
| 72 | + return keepMajorMinor(callStaticMethod("scala.util.Properties", "versionNumberString")); |
| 73 | + } |
| 74 | + |
| 75 | + public static String clojureVersion() { |
| 76 | + // (clojure-version) which translates to |
| 77 | + // clojure.core$clojure_version.invokeStatic() |
| 78 | + return keepMajorMinor(callStaticMethod("clojure.core$clojure_version", "invokeStatic")); |
| 79 | + } |
| 80 | + |
| 81 | + public static String groovyVersion() { |
| 82 | + // groovy.lang.GroovySystem.getVersion() |
| 83 | + // There's also getShortVersion(), but only since Groovy 3.0.1 |
| 84 | + return keepMajorMinor(callStaticMethod("groovy.lang.GroovySystem", "getVersion")); |
| 85 | + } |
| 86 | + |
| 87 | + public static String jRubyVersion() { |
| 88 | + // org.jruby.runtime.Constants.VERSION |
| 89 | + return keepMajorMinor(getStaticField("org.jruby.runtime.Constants", "VERSION")); |
| 90 | + } |
| 91 | + |
| 92 | + private static String getStaticField(String className, String fieldName) { |
| 93 | + Class<?> clazz; |
| 94 | + try { |
| 95 | + clazz = Class.forName(className); |
| 96 | + } catch (ClassNotFoundException e) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + try { |
| 101 | + Field field = clazz.getField(fieldName); |
| 102 | + return field.get(null).toString(); |
| 103 | + } catch (Exception e) { |
| 104 | + return ""; // can't get version information |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static String callStaticMethod(String className, String methodName) { |
| 109 | + Class<?> clazz; |
| 110 | + try { |
| 111 | + clazz = Class.forName(className); |
| 112 | + } catch (ClassNotFoundException e) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + try { |
| 117 | + Method m = clazz.getMethod(methodName); |
| 118 | + return m.invoke(null).toString(); |
| 119 | + } catch (Exception e) { |
| 120 | + return ""; // can't get version information |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + static String keepMajorMinor(String version) { |
| 125 | + if (version == null) { |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + int firstDot = version.indexOf('.'); |
| 130 | + int secondDot = version.indexOf('.', firstDot + 1); |
| 131 | + if (secondDot < 0) { |
| 132 | + return version; |
| 133 | + } else { |
| 134 | + return version.substring(0, secondDot); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments