Skip to content

Commit 546f175

Browse files
committed
Fix InaccessibleObjectException from JPMS in ThreadLocal cleaner
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.
1 parent b6a2ca9 commit 546f175

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

appserver/web/war-util/src/main/java/org/glassfish/web/loader/LogFacade.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ public static String getString(String key, Object... objects) {
104104
level = "WARNING")
105105
public static final String CHECK_THREAD_LOCALS_FOR_LEAKS_FAIL = PREFIX + "00011";
106106

107+
@LogMessageInfo(
108+
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",
109+
level = "FINE")
110+
public static final String CHECK_THREAD_LOCALS_FOR_LEAKS_NOT_SUPPORTED = PREFIX + "00012";
111+
107112
@LogMessageInfo(
108113
message = "The web application [{0}] created a ThreadLocal with key of [{1}]"
109114
+ " but failed to remove it when the web application was stopped."

appserver/web/war-util/src/main/java/org/glassfish/web/loader/ReferenceCleaner.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import static org.glassfish.web.loader.LogFacade.CHECK_THREAD_LOCALS_FOR_LEAKS_KEY;
4545
import static org.glassfish.web.loader.LogFacade.getString;
4646

47+
import java.lang.reflect.InaccessibleObjectException;
48+
4749
class ReferenceCleaner {
4850
private static final Logger LOG = LogFacade.getSysLogger(ReferenceCleaner.class);
4951

@@ -143,6 +145,9 @@ private void checkThreadLocalsForLeaks() {
143145
checkThreadLocalMapForLeaks(inheritableMap, tableField);
144146
}
145147
}
148+
} catch (InaccessibleObjectException e) {
149+
// module java.base does not "opens java.lang"
150+
LOG.log(WARNING, getString(LogFacade.CHECK_THREAD_LOCALS_FOR_LEAKS_NOT_SUPPORTED, loader.getName()));
146151
} catch (Exception e) {
147152
LOG.log(WARNING, getString(LogFacade.CHECK_THREAD_LOCALS_FOR_LEAKS_FAIL, loader.getName()), e);
148153
}

appserver/web/war-util/src/main/java/org/glassfish/web/loader/WebappClassLoader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
import static org.glassfish.web.loader.LogFacade.UNSUPPORTED_VERSION;
8686
import static org.glassfish.web.loader.LogFacade.getString;
8787

88+
import org.apache.naming.resources.BaseDirContext;
89+
8890
/**
8991
* Specialized web application class loader.
9092
* <p>
@@ -313,6 +315,9 @@ public void setResources(DirContext resources) {
313315
dirCtx = proxyRes.getDirContext();
314316
} else {
315317
dirCtx = resources;
318+
if (dirCtx instanceof BaseDirContext) {
319+
contextName = new File(((BaseDirContext)dirCtx).getDocBase()).getName();
320+
}
316321
}
317322
if (dirCtx instanceof WebDirContext) {
318323
((WebDirContext) dirCtx).setJarFileResourcesProvider(this);

0 commit comments

Comments
 (0)