phax / as2-lib

A generic Java AS2 library, servlet and server
107 stars 43 forks source link

AS2ReceiveServlet startup issue in Spring Boot application if servlet is configured programmatically #96

Closed yaskovdev closed 4 years ago

yaskovdev commented 4 years ago

The project that reproduces the issue is here. The error is the same as in this issue (No global scope object has been set!), however the AS2WebAppListener is in place.

The exception happens on AS2ReceiveServlet instantiation and is caused by this.m_aStatusMgr = ServletStatusManager.getInstance(); line from the AbstractXServlet. The issue is reproducible when the servlet is configured programmatically because in this case instantiation of the servlet happens before the AS2WebAppListener has a chance to do its job.

As a temporary solution I moved the problematic line of code from the constructor to the init method. Would it make more sense to move this line to init method permanently to make possible to use the servlet with Spring Boot?

phax commented 4 years ago

Thanks for the input. Unfortunately I am not an expert in Spring Boot, but I think I understand the issue. I moved the initialization stuff in the AS2ReceiveServlet to the GenericServlet.init() override method.

Additionally I added static init/destroy methods to AS2WebAppListener so that you can call them without having the need to use the ServletContextListener infrastructure.

~Based on this request I will also extend AbstractXServlet to save the ServletConfig from init so that you can access it later on from init()~ (it's already in GenericServlet :) )

Based on these changes you can create a derived Servlet that does AS2WebAppListener.staticInit (getServletConfig ().getServletContext ()) before super.init() and similar in shutdown.

phax commented 4 years ago

@yaskovdev do you mind I take your as2-sandbox as a spring boot template into the as2-lib?

yaskovdev commented 4 years ago

@phax, thank you for the quick response and fix. As for the template, I do not mind at all, feel free to use it wherever it may be useful.

yaskovdev commented 4 years ago

I believe that in order to fully resolve the issue changes in AbstractXServlet are needed. Because currently its constructor calls the ServletStatusManager.getInstance() method and the method requires the AS2WebAppListener.staticInit(getServletConfig().getServletContext()) to finish in order to do its job.

phax commented 4 years ago

Okay, I will check this out. Do I have the permission to publish your code under the Apache 2 license, stating you as the original author?

yaskovdev commented 4 years ago

Thank you. As for the publishing, of course you have. "Code" sounds too much for this example, to be honest, given that I just took the Spring Boot template and used the guide you created to configure the AS2ReceiveServlet. :)

phax commented 4 years ago

Initial version is in. An @AutoWired ServletContext sc; did the trick so no change was needed in the base config