Skip to content

Commit

Permalink
TOMEE-4357 - Add Jandex Index cache - 9.x (#1230)
Browse files Browse the repository at this point in the history
* Add Index cache

* some changes

* index cleanup

* Make indexCache concurrent
  • Loading branch information
fataOT authored Jul 5, 2024
1 parent f235c68 commit bf4602d
Showing 1 changed file with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import io.smallrye.opentracing.SmallRyeTracingDynamicFeature;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletRegistration;
import org.apache.openejb.assembler.classic.AppInfo;
import org.apache.openejb.assembler.classic.WebAppInfo;
import org.apache.openejb.assembler.classic.event.AssemblerBeforeApplicationDestroyed;
import org.apache.openejb.config.NewLoaderLogic;
import org.apache.openejb.config.event.EnhanceScannableUrlsEvent;
import org.apache.openejb.loader.Files;
Expand All @@ -40,13 +42,16 @@
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.InputStream;
import java.net.URL;
import java.security.CodeSource;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
Expand All @@ -66,6 +71,8 @@ public class TomEEMicroProfileListener {
"io.smallrye.faulttolerance.FaultToleranceExtension",
};

private final Map<AppInfo, Index> indexCache = new ConcurrentHashMap<>();

@SuppressWarnings("Duplicates")
public void enhanceScannableUrls(@Observes final EnhanceScannableUrlsEvent enhanceScannableUrlsEvent) {

Expand Down Expand Up @@ -98,6 +105,11 @@ public void enhanceScannableUrls(@Observes final EnhanceScannableUrlsEvent enhan
SystemInstance.get().setProperty("openejb.cxf-rs.cache-application", "false");
}

public void afterApplicationDeployed(@Observes final BeforeEvent<AssemblerBeforeApplicationDestroyed> event) {
//Just in case, remove the index from the cache if it still exits
indexCache.remove(event.getEvent().getApp());
}

public void processApplication(@Observes final BeforeEvent<AfterApplicationCreated> afterApplicationCreated) {
final ServletContext context = afterApplicationCreated.getEvent().getContext();
final WebAppInfo webApp = afterApplicationCreated.getEvent().getWeb();
Expand All @@ -122,10 +134,22 @@ public void processApplication(@Observes final BeforeEvent<AfterApplicationCreat
// index only once and pass it everywhere it's needed. Also in order to build the index, we need the entire
// application so doing it here from the AppInfo is way simpler
try {
final Index index = of(afterApplicationCreated.getEvent().getApp().libs);
final AppInfo app = afterApplicationCreated.getEvent().getApp();
final Index index = indexCache.computeIfAbsent(app, k -> {
try {
return of(app.libs);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
MicroProfileOpenApiRegistration.registerOpenApiServlet(context, index);

} catch (final IOException e) {
//Check if this was the last webapp to be processed and if so, remove it from the cache
int lastIndex = app.webApps.size() - 1;
if(app.webApps.indexOf(afterApplicationCreated.getEvent().getWeb()) == lastIndex) {
indexCache.remove(app);
}
} catch (final UncheckedIOException e) {
throw new IllegalStateException("Can't build Jandex index for application " + webApp.contextRoot, e);
}

Expand Down

0 comments on commit bf4602d

Please sign in to comment.