Skip to content

Commit 8bda02d

Browse files
authored
Uniform main and backport code (#127766)
While backporting entitlement initialization refactorings, I realized there is a mismatch in getVersionSpecificCheckerClass signature, and also that this function in the backports is used in more places (DynamicInstrumentation), making it "strange" to have this in EntitlementInitialization. This PR extracts the function to a separate static class (package-private) and makes the signature uniform with backports. This will need to be backported manually to the 8.x branches, and will make the backported version of DynamicInstrumentation cleaner.
1 parent 1df4a90 commit 8bda02d

File tree

2 files changed

+50
-31
lines changed

2 files changed

+50
-31
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
/**
15+
* Returns the "most recent" checker class compatible with the provided runtime Java version.
16+
* For checkers, we have (optionally) version specific classes, each with a prefix (e.g. Java23).
17+
* The mapping cannot be automatic, as it depends on the actual presence of these classes in the final Jar (see
18+
* the various mainXX source sets).
19+
*/
20+
static Class<?> getVersionSpecificCheckerClass(Class<?> baseClass, int javaVersion) {
21+
String packageName = baseClass.getPackageName();
22+
String baseClassName = baseClass.getSimpleName();
23+
24+
final String classNamePrefix;
25+
if (javaVersion >= 23) {
26+
// All Java version from 23 onwards will be able to use che checks in the Java23EntitlementChecker interface and implementation
27+
classNamePrefix = "Java23";
28+
} else {
29+
// For any other Java version, the basic EntitlementChecker interface and implementation contains all the supported checks
30+
classNamePrefix = "";
31+
}
32+
final String className = packageName + "." + classNamePrefix + baseClassName;
33+
Class<?> clazz;
34+
try {
35+
clazz = Class.forName(className);
36+
} catch (ClassNotFoundException e) {
37+
throw new AssertionError("entitlement lib cannot find entitlement class " + className, e);
38+
}
39+
return clazz;
40+
}
41+
}

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

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ public static void initialize(Instrumentation inst) throws Exception {
6868
ensureClassesSensitiveToVerificationAreInitialized();
6969
}
7070

71-
DynamicInstrumentation.initialize(inst, getVersionSpecificCheckerClass(EntitlementChecker.class), verifyBytecode);
71+
DynamicInstrumentation.initialize(
72+
inst,
73+
EntitlementCheckerUtils.getVersionSpecificCheckerClass(EntitlementChecker.class, Runtime.version().feature()),
74+
verifyBytecode
75+
);
7276
}
7377

7478
private static PolicyManager createPolicyManager() {
@@ -108,39 +112,13 @@ private static void ensureClassesSensitiveToVerificationAreInitialized() {
108112
}
109113
}
110114

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

143-
final Class<?> clazz = getVersionSpecificCheckerClass(ElasticsearchEntitlementChecker.class);
118+
final Class<?> clazz = EntitlementCheckerUtils.getVersionSpecificCheckerClass(
119+
ElasticsearchEntitlementChecker.class,
120+
Runtime.version().feature()
121+
);
144122

145123
Constructor<?> constructor;
146124
try {

0 commit comments

Comments
 (0)