Skip to content

Commit 132f9b8

Browse files
authored
Extract EntitlementCheckerUtils (#127896)
1 parent 1ec3fed commit 132f9b8

File tree

3 files changed

+53
-40
lines changed

3 files changed

+53
-40
lines changed

libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/DynamicInstrumentation.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
import java.util.stream.Stream;
4545
import java.util.stream.StreamSupport;
4646

47-
import static org.elasticsearch.entitlement.initialization.EntitlementInitialization.getVersionSpecificCheckerClass;
48-
4947
class DynamicInstrumentation {
5048

5149
interface InstrumentationInfoFactory {
@@ -191,7 +189,7 @@ public InstrumentationService.InstrumentationInfo of(String methodName, Class<?>
191189
);
192190

193191
if (Runtime.version().feature() >= 20) {
194-
var java20EntitlementCheckerClass = getVersionSpecificCheckerClass(EntitlementChecker.class, 20);
192+
var java20EntitlementCheckerClass = EntitlementCheckerUtils.getVersionSpecificCheckerClass(EntitlementChecker.class, 20);
195193
var java20Methods = Stream.of(
196194
INSTRUMENTATION_SERVICE.lookupImplementationMethod(
197195
FileSystemProvider.class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.entitlement.initialization;
11+
12+
class EntitlementCheckerUtils {
13+
/**
14+
* Returns the "most recent" checker class compatible with the provided runtime Java version.
15+
* For checkers, we have (optionally) version specific classes, each with a prefix (e.g. Java23).
16+
* The mapping cannot be automatic, as it depends on the actual presence of these classes in the final Jar (see
17+
* the various mainXX source sets).
18+
*/
19+
static Class<?> getVersionSpecificCheckerClass(Class<?> baseClass, int javaVersion) {
20+
String packageName = baseClass.getPackageName();
21+
String baseClassName = baseClass.getSimpleName();
22+
23+
final String classNamePrefix;
24+
if (javaVersion < 19) {
25+
// For older Java versions, the basic EntitlementChecker interface and implementation contains all the supported checks
26+
classNamePrefix = "";
27+
} else if (javaVersion < 23) {
28+
classNamePrefix = "Java" + javaVersion;
29+
} else {
30+
// All Java version from 23 onwards will be able to use che checks in the Java23EntitlementChecker interface and implementation
31+
classNamePrefix = "Java23";
32+
}
33+
34+
final String className = packageName + "." + classNamePrefix + baseClassName;
35+
Class<?> clazz;
36+
try {
37+
clazz = Class.forName(className);
38+
} catch (ClassNotFoundException e) {
39+
throw new AssertionError("entitlement lib cannot find entitlement class " + className, e);
40+
}
41+
return clazz;
42+
}
43+
}

libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementInitialization.java

+9-37
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ public static EntitlementChecker checker() {
6363
public static void initialize(Instrumentation inst) throws Exception {
6464
manager = initChecker();
6565

66+
var latestCheckerInterface = EntitlementCheckerUtils.getVersionSpecificCheckerClass(
67+
EntitlementChecker.class,
68+
Runtime.version().feature()
69+
);
6670
var verifyBytecode = Booleans.parseBoolean(System.getProperty("es.entitlements.verify_bytecode", "false"));
6771
if (verifyBytecode) {
6872
ensureClassesSensitiveToVerificationAreInitialized();
6973
}
7074

71-
DynamicInstrumentation.initialize(
72-
inst,
73-
getVersionSpecificCheckerClass(EntitlementChecker.class, Runtime.version().feature()),
74-
verifyBytecode
75-
);
75+
DynamicInstrumentation.initialize(inst, latestCheckerInterface, verifyBytecode);
7676
}
7777

7878
private static PolicyManager createPolicyManager() {
@@ -116,41 +116,13 @@ private static void ensureClassesSensitiveToVerificationAreInitialized() {
116116
}
117117
}
118118

119-
/**
120-
* Returns the "most recent" checker class compatible with the current runtime Java version.
121-
* For checkers, we have (optionally) version specific classes, each with a prefix (e.g. Java23).
122-
* The mapping cannot be automatic, as it depends on the actual presence of these classes in the final Jar (see
123-
* the various mainXX source sets).
124-
*/
125-
static Class<?> getVersionSpecificCheckerClass(Class<?> baseClass, int javaVersion) {
126-
String packageName = baseClass.getPackageName();
127-
String baseClassName = baseClass.getSimpleName();
128-
129-
final String classNamePrefix;
130-
if (javaVersion < 19) {
131-
// For older Java versions, the basic EntitlementChecker interface and implementation contains all the supported checks
132-
classNamePrefix = "";
133-
} else if (javaVersion < 23) {
134-
classNamePrefix = "Java" + javaVersion;
135-
} else {
136-
// All Java version from 23 onwards will be able to use che checks in the Java23EntitlementChecker interface and implementation
137-
classNamePrefix = "Java23";
138-
}
139-
140-
final String className = packageName + "." + classNamePrefix + baseClassName;
141-
Class<?> clazz;
142-
try {
143-
clazz = Class.forName(className);
144-
} catch (ClassNotFoundException e) {
145-
throw new AssertionError("entitlement lib cannot find entitlement class " + className, e);
146-
}
147-
return clazz;
148-
}
149-
150119
private static ElasticsearchEntitlementChecker initChecker() {
151120
final PolicyManager policyManager = createPolicyManager();
152121

153-
final Class<?> clazz = getVersionSpecificCheckerClass(ElasticsearchEntitlementChecker.class, Runtime.version().feature());
122+
final Class<?> clazz = EntitlementCheckerUtils.getVersionSpecificCheckerClass(
123+
ElasticsearchEntitlementChecker.class,
124+
Runtime.version().feature()
125+
);
154126

155127
Constructor<?> constructor;
156128
try {

0 commit comments

Comments
 (0)