MYFACES-4442: avoid UnsupportedOperationException #292
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix for MYFACES-4442: https://issues.apache.org/jira/browse/MYFACES-4442
Current behavior:
MyFacesContainerInitializer.onStartup
a) Checks clazzes from @HandlesTypes
b) Checks if a faces-config.xml is present.
c) If a or b
- Check if there are current FacesServlet mappings
- If there is a FacesServlet defined set: FACES_SERVLET_FOUND(true)
- If there is not a FacesServlet defined, then dynamically add and set: FACES_SERVLET_ADDED_ATTRIBUTE(true)
StartupServletContextListener.contextInitialized
a) Call FacesInitializer.initFaces(ServletContext)
FacesInitializerImpl.initFaces
a) If !INITIALIZE_ALWAYS_STANDALONE
b) Use FacesServletMappingUtils to getFacesServletRegistration
c) if getFacesServletRegistration is null / 0 then perform other checks
d) Check MyFacesContainerInitializer.FACES_SERVLET_FOUND
c) If MyFacesContainerInitializer.FACES_SERVLET_FOUND is null/false then check FACES_SERVLET_ADDED_ATTRIBUTE
d) If FACES_SERVLET_ADDED_ATTRIBUTE is null / false then put out message: "No mappings of FacesServlet found. Abort
initializing MyFaces."
Proposed Solution:
MyFacesContainerInitializer.onStartup
a) set isFacesServletPresent if there is a current FacesServlet defined.
b) Checks clazzes from @HandlesTypes
c) Checks if a faces-config.xml is present.
d) If b or c
- If there is not a FacesServlet defined (!isFacesServletPresent), then dynamically add and set:
FACES_SERVLET_ADDED_ATTRIBUTE(true)
StartupServletContextListener.contextInitialized
a) Call FacesInitializer.initFaces(ServletContext)
FacesInitializerImpl.initFaces
a) If !INITIALIZE_ALWAYS_STANDALONE
d) Check MyFacesContainerInitializer.FACES_SERVLET_FOUND
c) If MyFacesContainerInitializer.FACES_SERVLET_FOUND is null/false then check FACES_SERVLET_ADDED_ATTRIBUTE
d) If FACES_SERVLET_ADDED_ATTRIBUTE is null / false then put out message: "No mappings of FacesServlet found. Abort
initializing MyFaces."
Notes:
I don't see any reason why we would need to parse the web.xml or look do another lookup for a FacesServlet in the FacesInitializerImpl when the FacesServlet was already looked up in the MyFacesContainerInitializer. Also if a FacesServlet is not defined and @HandlesTypes classes are passed into onStartup or there is a faces-config.xml available in the application
then the FacesServlet is added dynamically. So for any application that is "Faces enabled" we would either already
have a FacesServlet defined in the web app or we'd try to dynamically add a FacesServlet in onStartup.
To me this pre-Faces 4.0 web.xml parsing and additional lookup seem like old legacy code that perhaps has been around since before ServletContainerInitializers were available (before Servlet 3.0).
This will avoid the UnsupportedOperationException from initFaces for calling getServletRegistrations from a
programmatically added listener: StartupServletContextListener.