|
12 | 12 | import org.elasticsearch.common.io.stream.StreamInput; |
13 | 13 | import org.elasticsearch.common.io.stream.StreamOutput; |
14 | 14 | import org.elasticsearch.core.Booleans; |
| 15 | +import org.elasticsearch.internal.BuildExtension; |
15 | 16 |
|
16 | 17 | import java.io.IOException; |
17 | 18 | import java.net.URL; |
|
24 | 25 | */ |
25 | 26 | public record Build(Type type, String hash, String date, boolean isSnapshot, String version) { |
26 | 27 |
|
27 | | - private static final Build CURRENT; |
| 28 | + private static class CurrentHolder { |
| 29 | + private static final Build CURRENT = findCurrent(); |
28 | 30 |
|
29 | | - /** |
30 | | - * The current build of Elasticsearch. Filled with information scanned at |
31 | | - * startup from the jar. |
32 | | - */ |
33 | | - public static Build current() { |
34 | | - return CURRENT; |
35 | | - } |
36 | | - |
37 | | - public enum Type { |
38 | | - |
39 | | - DEB("deb"), |
40 | | - DOCKER("docker"), |
41 | | - RPM("rpm"), |
42 | | - TAR("tar"), |
43 | | - ZIP("zip"), |
44 | | - UNKNOWN("unknown"); |
45 | | - |
46 | | - final String displayName; |
47 | | - |
48 | | - public String displayName() { |
49 | | - return displayName; |
50 | | - } |
51 | | - |
52 | | - Type(final String displayName) { |
53 | | - this.displayName = displayName; |
54 | | - } |
55 | | - |
56 | | - public static Type fromDisplayName(final String displayName, final boolean strict) { |
57 | | - switch (displayName) { |
58 | | - case "deb": |
59 | | - return Type.DEB; |
60 | | - case "docker": |
61 | | - return Type.DOCKER; |
62 | | - case "rpm": |
63 | | - return Type.RPM; |
64 | | - case "tar": |
65 | | - return Type.TAR; |
66 | | - case "zip": |
67 | | - return Type.ZIP; |
68 | | - case "unknown": |
69 | | - return Type.UNKNOWN; |
70 | | - default: |
71 | | - if (strict) { |
72 | | - throw new IllegalStateException("unexpected distribution type [" + displayName + "]; your distribution is broken"); |
73 | | - } else { |
74 | | - return Type.UNKNOWN; |
75 | | - } |
| 31 | + // finds the pluggable current build, or uses the local build as a fallback |
| 32 | + private static Build findCurrent() { |
| 33 | + var buildExtension = BuildExtension.load(); |
| 34 | + if (buildExtension == null) { |
| 35 | + return findLocalBuild(); |
76 | 36 | } |
| 37 | + var build = buildExtension.getCurrentBuild(); |
| 38 | + return build; |
77 | 39 | } |
78 | | - |
79 | 40 | } |
80 | 41 |
|
81 | | - static { |
| 42 | + /** |
| 43 | + * Finds build info scanned from the server jar. |
| 44 | + */ |
| 45 | + private static Build findLocalBuild() { |
82 | 46 | final Type type; |
83 | 47 | final String hash; |
84 | 48 | final String date; |
@@ -139,7 +103,55 @@ public static Type fromDisplayName(final String displayName, final boolean stric |
139 | 103 | ); |
140 | 104 | } |
141 | 105 |
|
142 | | - CURRENT = new Build(type, hash, date, isSnapshot, version); |
| 106 | + return new Build(type, hash, date, isSnapshot, version); |
| 107 | + } |
| 108 | + |
| 109 | + public static Build current() { |
| 110 | + return CurrentHolder.CURRENT; |
| 111 | + } |
| 112 | + |
| 113 | + public enum Type { |
| 114 | + |
| 115 | + DEB("deb"), |
| 116 | + DOCKER("docker"), |
| 117 | + RPM("rpm"), |
| 118 | + TAR("tar"), |
| 119 | + ZIP("zip"), |
| 120 | + UNKNOWN("unknown"); |
| 121 | + |
| 122 | + final String displayName; |
| 123 | + |
| 124 | + public String displayName() { |
| 125 | + return displayName; |
| 126 | + } |
| 127 | + |
| 128 | + Type(final String displayName) { |
| 129 | + this.displayName = displayName; |
| 130 | + } |
| 131 | + |
| 132 | + public static Type fromDisplayName(final String displayName, final boolean strict) { |
| 133 | + switch (displayName) { |
| 134 | + case "deb": |
| 135 | + return Type.DEB; |
| 136 | + case "docker": |
| 137 | + return Type.DOCKER; |
| 138 | + case "rpm": |
| 139 | + return Type.RPM; |
| 140 | + case "tar": |
| 141 | + return Type.TAR; |
| 142 | + case "zip": |
| 143 | + return Type.ZIP; |
| 144 | + case "unknown": |
| 145 | + return Type.UNKNOWN; |
| 146 | + default: |
| 147 | + if (strict) { |
| 148 | + throw new IllegalStateException("unexpected distribution type [" + displayName + "]; your distribution is broken"); |
| 149 | + } else { |
| 150 | + return Type.UNKNOWN; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
143 | 155 | } |
144 | 156 |
|
145 | 157 | /** |
|
0 commit comments