Skip to content

Commit

Permalink
Fix InaccessibleObjectException from JPMS in ThreadLocal cleaner
Browse files Browse the repository at this point in the history
Logged a warning and stacktrace if java.lang not open on JDK 17+. 
This is expected. Logs only a simple warning, with instructoins how to open the package.

Also sets the context (appName) of the WebApp CL to the name of the app file as a fallback.
  • Loading branch information
OndroMih committed Jul 25, 2024
1 parent b6a2ca9 commit 546f175
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public static String getString(String key, Object... objects) {
level = "WARNING")
public static final String CHECK_THREAD_LOCALS_FOR_LEAKS_FAIL = PREFIX + "00011";

@LogMessageInfo(
message = "Finding ThreadLocal references for web application [{0}] is not supported by the JVM. Use '--add-opens java.base/java.lang=ALL-UNNAMED' java argument to enable it",
level = "FINE")
public static final String CHECK_THREAD_LOCALS_FOR_LEAKS_NOT_SUPPORTED = PREFIX + "00012";

@LogMessageInfo(
message = "The web application [{0}] created a ThreadLocal with key of [{1}]"
+ " but failed to remove it when the web application was stopped."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import static org.glassfish.web.loader.LogFacade.CHECK_THREAD_LOCALS_FOR_LEAKS_KEY;
import static org.glassfish.web.loader.LogFacade.getString;

import java.lang.reflect.InaccessibleObjectException;

class ReferenceCleaner {
private static final Logger LOG = LogFacade.getSysLogger(ReferenceCleaner.class);

Expand Down Expand Up @@ -143,6 +145,9 @@ private void checkThreadLocalsForLeaks() {
checkThreadLocalMapForLeaks(inheritableMap, tableField);
}
}
} catch (InaccessibleObjectException e) {
// module java.base does not "opens java.lang"
LOG.log(WARNING, getString(LogFacade.CHECK_THREAD_LOCALS_FOR_LEAKS_NOT_SUPPORTED, loader.getName()));
} catch (Exception e) {
LOG.log(WARNING, getString(LogFacade.CHECK_THREAD_LOCALS_FOR_LEAKS_FAIL, loader.getName()), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
import static org.glassfish.web.loader.LogFacade.UNSUPPORTED_VERSION;
import static org.glassfish.web.loader.LogFacade.getString;

import org.apache.naming.resources.BaseDirContext;

/**
* Specialized web application class loader.
* <p>
Expand Down Expand Up @@ -313,6 +315,9 @@ public void setResources(DirContext resources) {
dirCtx = proxyRes.getDirContext();
} else {
dirCtx = resources;
if (dirCtx instanceof BaseDirContext) {
contextName = new File(((BaseDirContext)dirCtx).getDocBase()).getName();
}
}
if (dirCtx instanceof WebDirContext) {
((WebDirContext) dirCtx).setJarFileResourcesProvider(this);
Expand Down

0 comments on commit 546f175

Please # to comment.