|
2 | 2 |
|
3 | 3 | import static javax.faces.application.ViewVisitOption.RETURN_AS_MINIMAL_IMPLICIT_OUTCOME;
|
4 | 4 |
|
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +import javax.faces.application.Application; |
5 | 8 | import javax.faces.context.FacesContext;
|
| 9 | +import javax.faces.event.AbortProcessingException; |
| 10 | +import javax.faces.event.SystemEvent; |
| 11 | +import javax.faces.event.SystemEventListener; |
6 | 12 | import javax.faces.webapp.FacesServlet;
|
7 |
| -import javax.servlet.ServletContextEvent; |
8 |
| -import javax.servlet.ServletContextListener; |
9 |
| -import javax.servlet.annotation.WebListener; |
| 13 | +import javax.servlet.ServletContext; |
| 14 | +import javax.servlet.ServletRegistration; |
10 | 15 |
|
11 | 16 | /**
|
12 | 17 | *
|
13 | 18 | * @author Arjan Tijms
|
14 | 19 | */
|
15 |
| -@WebListener |
16 |
| -public class MappingInit implements ServletContextListener { |
| 20 | +public class MappingInit implements SystemEventListener { |
17 | 21 |
|
18 |
| - @Override |
19 |
| - public void contextInitialized(ServletContextEvent sce) { |
20 |
| - FacesContext context = FacesContext.getCurrentInstance(); |
| 22 | + |
| 23 | + @Override |
| 24 | + public void processEvent(SystemEvent event) throws AbortProcessingException { |
| 25 | + |
| 26 | + FacesContext facesContext = event.getFacesContext(); |
| 27 | + ServletContext sc = (ServletContext) facesContext.getExternalContext().getContext(); |
| 28 | + |
| 29 | + if (Boolean.valueOf((String) sc.getAttribute("mappingsAdded"))) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + Map<String, ? extends ServletRegistration> servletRegistrations = (Map<String, ? extends ServletRegistration>) sc.getAttribute("mappings"); |
21 | 34 |
|
22 |
| - sce.getServletContext() |
23 |
| - .getServletRegistrations() |
24 |
| - .values() |
25 |
| - .stream() |
26 |
| - .filter(e -> e.getClassName().equals(FacesServlet.class.getName())) |
27 |
| - .findAny() |
28 |
| - .ifPresent( |
29 |
| - reg -> context.getApplication() |
30 |
| - .getViewHandler() |
31 |
| - .getViews(context, "/", RETURN_AS_MINIMAL_IMPLICIT_OUTCOME) |
32 |
| - .forEach(e -> reg.addMapping(e))); |
| 35 | + if (servletRegistrations == null) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + MappingServletContextListener.addServletMappings(servletRegistrations, facesContext); |
33 | 40 | }
|
| 41 | + |
| 42 | + |
| 43 | + @Override |
| 44 | + public boolean isListenerForSource(Object source) { |
| 45 | + return source instanceof Application; |
| 46 | + } |
34 | 47 |
|
35 | 48 | }
|
0 commit comments