You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by "Simon Hill (Jira)" <ji...@apache.org> on 2022/06/22 16:17:00 UTC

[jira] [Created] (TOMEE-3984) ServletContainerInitializer implementation class loading breaks web fragments order logic

Simon Hill created TOMEE-3984:
---------------------------------

             Summary: ServletContainerInitializer implementation class loading breaks web fragments order logic
                 Key: TOMEE-3984
                 URL: https://issues.apache.org/jira/browse/TOMEE-3984
             Project: TomEE
          Issue Type: Bug
          Components: TomEE Core Server
    Affects Versions: 8.0.12
         Environment: Windows 10.0
            Reporter: Simon Hill


The behavior I'm describing was detected on a applications running on Tomee 8 (Apache Tomcat (TomEE)/9.0.63 (8.0.12)).

From my understanding and from what I observed Tomee proceeds in two main steps to support {{ServletContainerInitializer}} implementations :
 # Instanciation of every {{ServletContainerInitializer}} implementations found,
 # Call of the {{onStartup}} method for each {{{}ServletContainerInitializer{}}}.

From my point of view it is an issue when web fragments ordering has been configured because if a {{ServletContainerInitializer}} implementation class is loaded and instantiated it might trigger some static fields initialisation and static code execution. Therefore this code will be called before any of the {{onStartup}} method and break the configured order.

[https://github.com/apache/tomcat/blob/1bef182ffa6ddbe750fe04acb830473be9b1bcac/java/org/apache/catalina/startup/WebappServiceLoader.java#L221]
{code:java}
      for (String serviceClass : servicesFound) {
            try {
                Class<?> clazz = Class.forName(serviceClass, true, loader);
                services.add(serviceType.cast(clazz.getConstructor().newInstance()));
            } catch (ReflectiveOperationException | ClassCastException e) {
                throw new IOException(e);
            }
        }
{code}
h2. Example

For example {{log4j2-web}} contains the web-fragment below in order to make sure log4j is initialized before any other components that might attempt to log something:
{code:xml}
<web-fragment ...>
   [...]
    <ordering>
        <before>
            <others />
        </before>
    </ordering>
</web-fragment>
{code}
But another library we use (Camunda) also contains it own {{ServletContainerInitializer}} implementation class with a static field, triggering static code, leading to log4j initialization before log4j's {{ServletContainerInitializer.onStartup}} method call.

And this is an issue because in that case {{log4j}} configuration relies on data from the {{ServletContext}} which has not been provided to {{log4j}} yet.
h2. Expected behavior

I might be naive but I would expect every {{ServletContainerInitializer}} implementation to be instantiated respecting the configured order and its {{onStartup}} method being called immediately after (before any other {{ServletContainerInitializer}} implementation instantiation).



--
This message was sent by Atlassian Jira
(v8.20.7#820007)