3737import java .nio .file .spi .FileSystemProvider ;
3838import java .util .ArrayList ;
3939import java .util .HashMap ;
40- import java .util .HashSet ;
4140import java .util .List ;
4241import java .util .Map ;
4342import java .util .Set ;
@@ -67,16 +66,9 @@ public static EntitlementChecker checker() {
6766 public static void initialize (Instrumentation inst ) throws Exception {
6867 manager = initChecker ();
6968
70- Map <MethodKey , CheckMethod > checkMethods = new HashMap <>();
71- int javaVersion = Runtime .version ().feature ();
72- Set <Class <?>> interfaces = new HashSet <>();
73- for (int i = 17 ; i <= javaVersion ; ++i ) {
74- interfaces .add (getVersionSpecificCheckerClass (i , "org.elasticsearch.entitlement.bridge" , "EntitlementChecker" ));
75- }
76- for (var checkerInterface : interfaces ) {
77- checkMethods .putAll (INSTRUMENTATION_SERVICE .lookupMethods (checkerInterface ));
78- }
69+ var latestCheckerInterface = getVersionSpecificCheckerClass (EntitlementChecker .class );
7970
71+ Map <MethodKey , CheckMethod > checkMethods = new HashMap <>(INSTRUMENTATION_SERVICE .lookupMethods (latestCheckerInterface ));
8072 var fileSystemProviderClass = FileSystems .getDefault ().provider ().getClass ();
8173 Stream .of (
8274 INSTRUMENTATION_SERVICE .lookupImplementationMethod (
@@ -90,11 +82,6 @@ public static void initialize(Instrumentation inst) throws Exception {
9082 )
9183 ).forEach (instrumentation -> checkMethods .put (instrumentation .targetMethod (), instrumentation .checkMethod ()));
9284
93- var latestCheckerInterface = getVersionSpecificCheckerClass (
94- javaVersion ,
95- "org.elasticsearch.entitlement.bridge" ,
96- "EntitlementChecker"
97- );
9885 var classesToTransform = checkMethods .keySet ().stream ().map (MethodKey ::className ).collect (Collectors .toSet ());
9986
10087 Instrumenter instrumenter = INSTRUMENTATION_SERVICE .newInstrumenter (latestCheckerInterface , checkMethods );
@@ -144,36 +131,27 @@ private static PolicyManager createPolicyManager() {
144131 return new PolicyManager (serverPolicy , agentEntitlements , pluginPolicies , resolver , AGENTS_PACKAGE_NAME , ENTITLEMENTS_MODULE );
145132 }
146133
147- private static ElasticsearchEntitlementChecker initChecker () {
148- final PolicyManager policyManager = createPolicyManager ();
149-
150- Class <?> clazz = getVersionSpecificCheckerClass (
151- Runtime .version ().feature (),
152- "org.elasticsearch.entitlement.runtime.api" ,
153- "ElasticsearchEntitlementChecker"
154- );
155- Constructor <?> constructor ;
156- try {
157- constructor = clazz .getConstructor (PolicyManager .class );
158- } catch (NoSuchMethodException e ) {
159- throw new AssertionError ("entitlement impl is missing no arg constructor" , e );
160- }
161- try {
162- return (ElasticsearchEntitlementChecker ) constructor .newInstance (policyManager );
163- } catch (IllegalAccessException | InvocationTargetException | InstantiationException e ) {
164- throw new AssertionError (e );
165- }
166- }
134+ /**
135+ * Returns the "most recent" checker class compatible with the current runtime Java version.
136+ * For checkers, we have (optionally) version specific classes, each with a prefix (e.g. Java23).
137+ * The mapping cannot be automatic, as it depends on the actual presence of these classes in the final Jar (see
138+ * the various mainXX source sets).
139+ */
140+ private static Class <?> getVersionSpecificCheckerClass (Class <?> baseClass ) {
141+ String packageName = baseClass .getPackageName ();
142+ String baseClassName = baseClass .getSimpleName ();
143+ int javaVersion = Runtime .version ().feature ();
167144
168- private static Class <?> getVersionSpecificCheckerClass (int javaVersion , String packageName , String baseClassName ) {
169145 final String classNamePrefix ;
170146 if (javaVersion == 21 ) {
171147 classNamePrefix = "Java21" ;
172148 } else if (javaVersion == 22 ) {
173149 classNamePrefix = "Java22" ;
174150 } else if (javaVersion >= 23 ) {
151+ // All Java version from 23 onwards will be able to use che checks in the Java23EntitlementChecker interface and implementation
175152 classNamePrefix = "Java23" ;
176153 } else {
154+ // For any other Java version, the basic EntitlementChecker interface and implementation contains all the supported checks
177155 classNamePrefix = "" ;
178156 }
179157 final String className = packageName + "." + classNamePrefix + baseClassName ;
@@ -186,6 +164,24 @@ private static Class<?> getVersionSpecificCheckerClass(int javaVersion, String p
186164 return clazz ;
187165 }
188166
167+ private static ElasticsearchEntitlementChecker initChecker () {
168+ final PolicyManager policyManager = createPolicyManager ();
169+
170+ final Class <?> clazz = getVersionSpecificCheckerClass (ElasticsearchEntitlementChecker .class );
171+
172+ Constructor <?> constructor ;
173+ try {
174+ constructor = clazz .getConstructor (PolicyManager .class );
175+ } catch (NoSuchMethodException e ) {
176+ throw new AssertionError ("entitlement impl is missing no arg constructor" , e );
177+ }
178+ try {
179+ return (ElasticsearchEntitlementChecker ) constructor .newInstance (policyManager );
180+ } catch (IllegalAccessException | InvocationTargetException | InstantiationException e ) {
181+ throw new AssertionError (e );
182+ }
183+ }
184+
189185 private static final InstrumentationService INSTRUMENTATION_SERVICE = new ProviderLocator <>(
190186 "entitlement" ,
191187 InstrumentationService .class ,
0 commit comments