java.lang.IllegalStateException: Unable to access CDI is thrown from SOAPHandler in EAP 8.0.3+ and 7.4.19+

Solution Verified - Updated

Environment

  • Red Hat JBoss Enterprise Application Server (EAP)
    • 7.4.19+
    • 8.0.3+

Issue

When CDI.current() is called from JAX-WS SOAPHandler, java.lang.IllegalStateException is thrown. It works until JBoss EAP 7.4.18 and 8.0.2.1, but we can see the following WARNING messages in server.log after applying patches 7.4.19+ or 8.0.3+.

EAP 8.0.3:

2024-10-22 15:35:43,181 WARNING [org.apache.cxf.jaxws.handler.HandlerChainInvoker] (default task-2) Handler execution raised a runtime exception.: java.lang.IllegalStateException: Unable to access CDI
	at jakarta.enterprise.api@4.0.1.redhat-00001//jakarta.enterprise.inject.spi.CDI.lambda$getCDIProvider$0(CDI.java:98)
	...

EAP 7.4.19:

2024-10-22 15:36:18,744 WARNING [org.apache.cxf.jaxws.handler.HandlerChainInvoker] (default task-2) Handler execution raised a runtime exception.: java.lang.IllegalStateException: WFLYWELD0039: Singleton not set for org.jboss.ws.common.utils.DelegateClassLoader@49134e6f. This means that you are trying to access a weld deployment with a Thread Context ClassLoader that is not associated with the deployment.
	at org.jboss.as.weld@7.4.19.GA-redhat-00002//org.jboss.as.weld.services.ModuleGroupSingletonProvider$TCCLSingleton.get(ModuleGroupSingletonProvider.java:77)
	...

Resolution

Update JBoss EAP 7.4.23+ or 8.0.8+. As a workaround, just before calling CDI.current(), set the thread's context class loader to the application module class loader:

ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
CDI.current();
Thread.currentThread().setContextClassLoader(old);

Root Cause

This issue is caused by the thread's context classloader(TCCL) not being the application module loader. The TCCL has been replaced with org.jboss.ws.common.utils.DelegateClassLoader in JBoss EAP 7.4.19, and org.jboss.wsf.stack.cxf.JAXPDelegateClassLoader in JBoss EAP 8.0.3 or later. The issue is filed as This content is not included.JBEAP-28311, This content is not included.JBEAP-28792 for EAP 7.4.z and This content is not included.JBEAP-28310, This content is not included.JBEAP-28791 for EAP 8.0.z. Both of issues are currently under investigation for future cumulative patch update.

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.