piranhacloud / piranha

Piranha - a modern cloud runtime
https://piranha.cloud
BSD 3-Clause "New" or "Revised" License
194 stars 20 forks source link

Piranha Micro is failing to startup #1318

Closed Thihup closed 3 years ago

Thihup commented 3 years ago

Need to check why the Jakarta EE 8 Samples didn't catch this.

$ java -jar piranha-micro-21.1.0-SNAPSHOT.jar
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.IllegalStateException: 
        at cloud.piranha.micro.MicroOuterDeployer.deploy(MicroOuterDeployer.java:194)
        at cloud.piranha.micro.MicroPiranha.run(MicroPiranha.java:141)
        at cloud.piranha.micro.MicroPiranha.main(MicroPiranha.java:79)
Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:564)
        at cloud.piranha.micro.MicroOuterDeployer.deploy(MicroOuterDeployer.java:187)
        ... 2 more
Caused by: java.lang.NoClassDefFoundError: cloud/piranha/naming/thread/ThreadInitialContextFactory
        at cloud.piranha.micro.core.MicroInnerDeployer.start(MicroInnerDeployer.java:171)
        ... 7 more
Caused by: java.lang.ClassNotFoundException: Unable to load class: cloud.piranha.naming.thread.ThreadInitialContextFactory
        at cloud.piranha.resource.DefaultResourceManagerClassLoader.loadClass(DefaultResourceManagerClassLoader.java:130)
        at cloud.piranha.resource.shrinkwrap.IsolatingResourceManagerClassLoader.loadClass(IsolatingResourceManagerClassLoader.java:87)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 8 more
manorrock commented 3 years ago

Mmm, we'll need a test to make sure that does not happen again.

Thihup commented 3 years ago

I dig a little into this problem and this exception is caused because the piranha-naming-thread is declared as provided in the micro-extension module. Changing to compile, it fails because I guess it needs a global context to store something:

Dec 19, 2020 10:55:28 AM cloud.piranha.webapp.impl.DefaultWebApplication initializeInitializers
WARNING: Initializer org.jboss.weld.environment.servlet.EnhancedListener failing onStartup
java.lang.IllegalStateException: WELD-ENV-001019: Error creating JNDI context
        at org.jboss.weld.environment.servlet.services.ServletResourceInjectionServices.<init>(ServletResourceInjectionServices.java:19)
        at org.jboss.weld.environment.servlet.WeldServletLifecycle$1.<init>(WeldServletLifecycle.java:168)
        at org.jboss.weld.environment.servlet.WeldServletLifecycle.initialize(WeldServletLifecycle.java:168)
        at org.jboss.weld.environment.servlet.EnhancedListener.onStartup(EnhancedListener.java:62)
        at cloud.piranha.webapp.impl.DefaultWebApplication.initializeInitializers(DefaultWebApplication.java:1398)
        at cloud.piranha.webapp.impl.DefaultWebApplication.initialize(DefaultWebApplication.java:1322)
        at cloud.piranha.micro.core.MicroInnerDeployer.start(MicroInnerDeployer.java:227)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:564)
        at cloud.piranha.micro.MicroOuterDeployer.deploy(MicroOuterDeployer.java:187)
        at cloud.piranha.micro.MicroPiranha.run(MicroPiranha.java:141)
        at cloud.piranha.micro.MicroPiranha.main(MicroPiranha.java:79)
Caused by: javax.naming.NamingException: Initial context not available for thread: Thread[main,5,main]
        at cloud.piranha.naming.thread.ThreadInitialContextFactory.getInitialContext(ThreadInitialContextFactory.java:60)
        at java.naming/javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:719)
        at java.naming/javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:305)
        at java.naming/javax.naming.InitialContext.init(InitialContext.java:236)
        at java.naming/javax.naming.InitialContext.<init>(InitialContext.java:184)
        at org.jboss.weld.environment.servlet.services.ServletResourceInjectionServices.<init>(ServletResourceInjectionServices.java:17)
        ... 13 more
manorrock commented 3 years ago

Looks like it is missing a call to ThreadInitialContextFactory.setInitialContext and ThreadInitialContextFactory.removeInitialContext. See ServerPiranha for how it should look like.

arjantijms commented 3 years ago

Tried running 20.12.0 using Arquillian, and it results in this:

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running org.javaee7.servlet.metadata.complete.SimpleServletTest
Jan 02, 2021 4:14:41 PM cloud.piranha.arquillian.server.PiranhaServerDeployableContainer deploy
INFO: Starting Piranha Micro
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Jan 02, 2021 4:14:42 PM cloud.piranha.arquillian.server.PiranhaServerDeployableContainer undeploy
INFO: Stopping Piranha Micro
[WARNING] Corrupted STDOUT by directly writing to native stream in forked JVM 1. See FAQ web page and the dump file /opt/jakartaee-samples/ee7/servlet/simple-servlet/target/surefire-reports/2021-01-02T16-14-41_067-jvmRun1.dumpstream
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.16 s - in org.javaee7.servlet.metadata.complete.SimpleServletTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

It's not caught by the samples seemingly since it doesn't start to run any tests, and somehow that's okay.

arjantijms commented 3 years ago

It's a long story, but it seems caused by an exception during handling of the failure, so the failure doesn't get set in junit, and it doesn't report it. The corrupted STDOUT is something Surefire 3.0.0 will/should fix. For now the exception can be fixed by using the latest Arquillian and Surefire versions.

The Junit test than fails as expected:

[INFO] Running org.javaee7.servlet.servlet_libs.empty.app.EmptyAppWithLibTest
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Jan 03, 2021 12:33:50 AM cloud.piranha.arquillian.server.PiranhaServerDeployableContainer deploy
INFO: Starting Piranha Micro
Jan 03, 2021 12:33:56 AM cloud.piranha.arquillian.server.PiranhaServerDeployableContainer undeploy
INFO: Stopping Piranha Micro
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 7.55 s <<< FAILURE! - in org.javaee7.servlet.servlet_libs.empty.app.EmptyAppWithLibTest
[ERROR] org.javaee7.servlet.servlet_libs.empty.app.EmptyAppWithLibTest  Time elapsed: 7.535 s  <<< ERROR!
java.lang.IllegalStateException: 
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.NoClassDefFoundError: cloud/piranha/naming/thread/ThreadInitialContextFactory
Caused by: java.lang.ClassNotFoundException: Unable to load class: cloud.piranha.naming.thread.ThreadInitialContextFactory
arjantijms commented 3 years ago

Looks like it is missing a call to ThreadInitialContextFactory.setInitialContext and ThreadInitialContextFactory.removeInitialContext. See ServerPiranha for how it should look like.

Nope, it's there:

        ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(webApplication.getClassLoader());
            ThreadInitialContextFactory.setInitialContext(webApplication.getNamingManager().getContext());
            webApplication.service(copyMapToApplicationRequest(requestMap), copyMapToApplicationResponse(requestMap));

        } catch (ServletException | IOException e) {
            e.printStackTrace();
        } finally {
            Thread.currentThread().setContextClassLoader(oldClassLoader);
            ThreadInitialContextFactory.removeInitialContext();
        }

This seems to be problematic though:

java.lang.ClassNotFoundException: Unable to load class: cloud.piranha.naming.thread.ThreadInitialContextFactory
Thihup commented 3 years ago

This is happening due to the naming-thread being marked as provided in the pom.xml of the micro-core. When resolving the dependencies of the micro-core, it won't include the naming-thread module, thus, the ClassNotFoundException

arjantijms commented 3 years ago

@Thihup Indeed! Unfortunately, after removing the provided, the startup fails with a next error:

INFO: Starting Piranha Micro
Jan 03, 2021 3:09:10 PM cloud.piranha.micro.core.MicroInnerDeployer start
INFO: Starting web application 51203f39-6e5a-4057-aa7f-3e0851550a4b.war on Piranha Micro 20.12.0
Jan 03, 2021 3:09:10 PM org.jboss.weld.environment.servlet.Listener contextInitialized
INFO: WELD-ENV-001007: Initialize Weld using ServletContextListener
Jan 03, 2021 3:09:10 PM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 3.1.5 (SP1)
Jan 03, 2021 3:09:10 PM org.jboss.weld.environment.deployment.discovery.DiscoveryStrategyFactory create
INFO: WELD-ENV-000020: Using jandex for bean discovery
Jan 03, 2021 3:09:10 PM cloud.piranha.webapp.impl.DefaultWebApplication initializeInitializers
WARNING: Initializer cloud.piranha.security.jakarta.JakartaSecurityAllInitializer failing onStartup
java.lang.IllegalStateException: WELD-ENV-001019: Error creating JNDI context
    at org.jboss.weld.environment.servlet.services.ServletResourceInjectionServices.<init>(ServletResourceInjectionServices.java:19)
    at org.jboss.weld.environment.servlet.WeldServletLifecycle$1.<init>(WeldServletLifecycle.java:168)
    at org.jboss.weld.environment.servlet.WeldServletLifecycle.initialize(WeldServletLifecycle.java:168)
    at org.jboss.weld.environment.servlet.Listener.contextInitialized(Listener.java:120)
    at cloud.piranha.cdi.weld.WeldInitializer.onStartup(WeldInitializer.java:64)
    at cloud.piranha.security.jakarta.JakartaSecurityAllInitializer.onStartup(JakartaSecurityAllInitializer.java:99)
    at cloud.piranha.webapp.impl.DefaultWebApplication.initializeInitializers(DefaultWebApplication.java:1398)
    at cloud.piranha.webapp.impl.DefaultWebApplication.initialize(DefaultWebApplication.java:1322)
    at cloud.piranha.micro.core.MicroInnerDeployer.start(MicroInnerDeployer.java:227)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at cloud.piranha.micro.MicroOuterDeployer.deploy(MicroOuterDeployer.java:187)
    at cloud.piranha.arquillian.server.PiranhaServerDeployableContainer.deploy(PiranhaServerDeployableContainer.java:82)
    at org.jboss.arquillian.container.impl.client.container.ContainerDeployController$3.call(ContainerDeployController.java:151)
    at org.jboss.arquillian.container.impl.client.container.ContainerDeployController$3.call(ContainerDeployController.java:118)
    at org.jboss.arquillian.container.impl.client.container.ContainerDeployController.executeOperation(ContainerDeployController.java:239)
    at org.jboss.arquillian.container.impl.client.container.ContainerDeployController.deploy(ContainerDeployController.java:118)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:86)
    at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:103)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:90)
    at org.jboss.arquillian.container.impl.client.container.DeploymentExceptionHandler.verifyExpectedExceptionDuringDeploy(DeploymentExceptionHandler.java:47)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:86)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:95)
    at org.jboss.arquillian.container.impl.client.ContainerDeploymentContextHandler.createDeploymentContext(ContainerDeploymentContextHandler.java:71)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:86)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:95)
    at org.jboss.arquillian.container.impl.client.ContainerDeploymentContextHandler.createContainerContext(ContainerDeploymentContextHandler.java:54)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:86)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:95)
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:133)
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:105)
    at org.jboss.arquillian.core.impl.EventImpl.fire(EventImpl.java:62)
    at org.jboss.arquillian.container.impl.client.container.ContainerDeployController$1.perform(ContainerDeployController.java:92)
    at org.jboss.arquillian.container.impl.client.container.ContainerDeployController$1.perform(ContainerDeployController.java:77)
    at org.jboss.arquillian.container.impl.client.container.ContainerDeployController.forEachDeployment(ContainerDeployController.java:232)
    at org.jboss.arquillian.container.impl.client.container.ContainerDeployController.forEachManagedDeployment(ContainerDeployController.java:212)
    at org.jboss.arquillian.container.impl.client.container.ContainerDeployController.deployManaged(ContainerDeployController.java:77)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:86)
    at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:103)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:90)
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:133)
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:105)
    at org.jboss.arquillian.core.impl.EventImpl.fire(EventImpl.java:62)
    at org.jboss.arquillian.container.test.impl.client.ContainerEventController.execute(ContainerEventController.java:96)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:86)
    at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:103)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:90)
    at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:83)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:86)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:95)
    at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:69)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:86)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:95)
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:133)
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:105)
    at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.beforeClass(EventTestRunnerAdaptor.java:89)
    at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:163)
    at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:350)
    at org.jboss.arquillian.junit.Arquillian.access$200(Arquillian.java:54)
    at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:177)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:115)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:364)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:272)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:237)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:158)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:428)
    at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:162)
    at org.apache.maven.surefire.booter.ForkedBooter.run(ForkedBooter.java:562)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:548)
Caused by: javax.naming.NamingException: Initial context not available for thread: Thread[main,5,main]
    at cloud.piranha.naming.thread.ThreadInitialContextFactory.getInitialContext(ThreadInitialContextFactory.java:60)
    at java.naming/javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:719)
    at java.naming/javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:305)
    at java.naming/javax.naming.InitialContext.init(InitialContext.java:236)
    at java.naming/javax.naming.InitialContext.<init>(InitialContext.java:184)
    at org.jboss.weld.environment.servlet.services.ServletResourceInjectionServices.<init>(ServletResourceInjectionServices.java:17)
    ... 104 more

This means new threads that are created do not have have the JNDI context set.

Thihup commented 3 years ago

From what I can tell, this error happens due the ThreadNamingContext not being initialized during the invocation of the initializers. It is only set in a request, but not during the initialization of the application.

Thihup commented 3 years ago

While trying the JPMS I have tried fix it with: https://github.com/Thihup/piranha/commit/f90aadf45570cd959f8086d9cbac5c169ba16171#diff-d5a026c9ddf85b36068ab66206faa84aa9bbf55e275cf43a69a04db8ff1001ad And https://github.com/Thihup/piranha/commit/f90aadf45570cd959f8086d9cbac5c169ba16171#diff-a99f82342dad7da3f6158765e35a984d16c8c2d3b11efa6186a28572531187f5

Needs more testing, though.

arjantijms commented 3 years ago

I wonder if that really fixes it, as it would need to be set for every thread the application creates, or every thread it grabs from a thread pool, etc.

I wonder if using thread ID as the key in this map is such a good idea. JNDI is logically per application. Perhaps using a unique identifier for the application is a better idea?

arjantijms commented 3 years ago

With "this map" I mean the one used to store JNDI contexts:

public class ThreadInitialContextFactory implements InitialContextFactory {

    /**
     * Stores the initial contexts by thread id.
     */
    private static final HashMap<Long, Context> INITIAL_CONTEXTS = new HashMap<>(1);

    /**
     * Get the initial context.
     *
     * @param environment the environment.
     * @return the initial context.
     * @throws NamingException when a naming error occurs.
     */
    @Override
    public Context getInitialContext(Hashtable<?, ?> environment) throws NamingException {
        if (INITIAL_CONTEXTS.containsKey(Thread.currentThread().getId())) {
            return INITIAL_CONTEXTS.get(Thread.currentThread().getId());
        }
        throw new NamingException("Initial context not available for thread: " + Thread.currentThread());
    }

    /**
     * Remove the initial context.
     */
    public static void removeInitialContext() {
        INITIAL_CONTEXTS.remove(Thread.currentThread().getId());
    }

    /**
     * Set the initial context.
     *
     * @param initialContext the initial context.
     */
    public static void setInitialContext(Context initialContext) {
        INITIAL_CONTEXTS.put(Thread.currentThread().getId(), initialContext);
    }
}
manorrock commented 3 years ago

It needs to set the context before each request/initialization executes.

arjantijms commented 3 years ago

It needs to set the context before each request/initialization executes.

It also needs to set the context before each thread created/taken from a pool.

manorrock commented 3 years ago

Which pool are you referring to?

arjantijms commented 3 years ago

Which pool are you referring to?

For instance the Jakarta Concurrency pool when/if we have support for that and/or any other server application pool when/if we have that.

But also several libraries, again Mojarra and Weld, create their own pool.

manorrock commented 3 years ago

OK, for now lets concentrate on the current issue