11/*
2- * Copyright (c) 2021 Oracle and/or its affiliates.
2+ * Copyright (c) 2021, 2024 Oracle and/or its affiliates.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2525import java .util .Enumeration ;
2626import java .util .HashMap ;
2727import java .util .HashSet ;
28+ import java .util .LinkedList ;
2829import java .util .List ;
2930import java .util .Map ;
3031import java .util .Set ;
6970import org .jboss .jandex .ClassInfo ;
7071import org .jboss .jandex .CompositeIndex ;
7172import org .jboss .jandex .DotName ;
73+ import org .jboss .jandex .Index ;
7274import org .jboss .jandex .IndexReader ;
7375import org .jboss .jandex .IndexView ;
7476import org .jboss .jandex .Indexer ;
@@ -94,7 +96,8 @@ public class LraCdiExtension implements Extension {
9496
9597 private final Set <Class <?>> beanTypesWithCdiLRAMethods = new HashSet <>();
9698 private final Map <Class <?>, Bean <?>> lraCdiBeanReferences = new HashMap <>();
97- private final Indexer indexer ;
99+ // Do not clear indexes. #index requires a strong reference to these.
100+ private final List <IndexView > indexes = new LinkedList <>();
98101 private final ClassLoader classLoader ;
99102 private IndexView index ;
100103
@@ -103,7 +106,6 @@ public class LraCdiExtension implements Extension {
103106 * Initialize MicroProfile Long Running Actions CDI extension.
104107 */
105108 public LraCdiExtension () {
106- indexer = new Indexer ();
107109 classLoader = Thread .currentThread ().getContextClassLoader ();
108110 // Needs to be always indexed
109111 Set .of (LRA .class ,
@@ -119,7 +121,9 @@ public LraCdiExtension() {
119121 try {
120122 indexFiles = findIndexFiles ("META-INF/jandex.idx" );
121123 if (!indexFiles .isEmpty ()) {
122- index = CompositeIndex .create (indexer .complete (), existingIndexFileReader (indexFiles ));
124+ indexes .add (existingIndexFileReader (indexFiles ));
125+ index = CompositeIndex .create (indexes );
126+ logIndexedClasses (index );
123127 } else {
124128 index = null ;
125129 }
@@ -137,7 +141,8 @@ private void index(
137141 LRA .class ,
138142 AfterLRA .class , Compensate .class , Complete .class , Forget .class , Status .class
139143 }) ProcessAnnotatedType <?> pat ) {
140- // compile time bilt index
144+ // compile time built index
145+ // If META-INF/jandex.idx exists we don't explore more
141146 if (index != null ) return ;
142147 // create runtime index when pre-built index is not available
143148 runtimeIndex (DotName .createSimple (pat .getAnnotatedType ().getJavaClass ().getName ()));
@@ -227,7 +232,8 @@ private void ready(
227232
228233 if (index == null ) {
229234 // compile time built index
230- index = indexer .complete ();
235+ index = CompositeIndex .create (indexes );
236+ logIndexedClasses (index );
231237 }
232238
233239 // ------------- Validate LRA participant methods -------------
@@ -255,13 +261,16 @@ Map<Class<?>, Bean<?>> lraCdiBeanReferences() {
255261 void runtimeIndex (DotName fqdn ) {
256262 if (fqdn == null ) return ;
257263 LOGGER .fine ("Indexing " + fqdn );
258- ClassInfo classInfo ;
259264 try {
260- classInfo = indexer .index (classLoader .getResourceAsStream (fqdn .toString ().replace ('.' , '/' ) + ".class" ));
265+ Indexer newIndexer = new Indexer ();
266+ newIndexer .index (classLoader .getResourceAsStream (fqdn .toString ().replace ('.' , '/' ) + ".class" ));
267+ Index index = newIndexer .complete ();
268+ indexes .add (index );
269+ ClassInfo classInfo = index .getClassByName (fqdn );
261270 // look also for extended classes
262271 runtimeIndex (classInfo .superName ());
263272 // and implemented interfaces
264- classInfo .interfaceNames ().forEach (this :: runtimeIndex );
273+ classInfo .interfaceNames ().forEach (dotName -> runtimeIndex ( dotName ) );
265274 } catch (IOException e ) {
266275 LOGGER .log (Level .SEVERE , "Unable to index referenced class." , e );
267276 }
@@ -313,4 +322,13 @@ static <T> T lookup(Bean<?> bean, BeanManager beanManager) {
313322 }
314323 return (T ) instance ;
315324 }
325+
326+ private void logIndexedClasses (IndexView index ) {
327+ if (LOGGER .isLoggable (Level .FINE )) {
328+ LOGGER .fine (index .getKnownClasses ().size () + " indexed classes." );
329+ index .getKnownClasses ().forEach (classInfo -> {
330+ LOGGER .fine ("Indexed class - " + classInfo .name ().toString ());
331+ });
332+ }
333+ }
316334}
0 commit comments