Debian Codegraph @

jetty-ee9/jetty-ee9-demos/jetty-ee9-demo-spec/jetty-ee9-demo-container-initializer/src/main/java/org/example/initializer/FooInitializer.java

raw
1 //
2 // ========================================================================
3 // Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
4 //
5 // This program and the accompanying materials are made available under the
6 // terms of the Eclipse Public License v. 2.0 which is available at
7 // https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
8 // which is available at https://www.apache.org/licenses/LICENSE-2.0.
9 //
10 // SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
11 // ========================================================================
12 //
13
14 package org.example.initializer;
15
16 import java.util.ArrayList;
17 import java.util.Set;
18
19 import jakarta.servlet.ServletContainerInitializer;
20 import jakarta.servlet.ServletContext;
21 import jakarta.servlet.ServletContextEvent;
22 import jakarta.servlet.ServletContextListener;
23 import jakarta.servlet.ServletRegistration;
24 import jakarta.servlet.annotation.HandlesTypes;
25
26 @HandlesTypes({jakarta.servlet.Servlet.class, Foo.class})
27 public class FooInitializer implements ServletContainerInitializer
28 {
29 public static class BarListener implements ServletContextListener
30 {
31
32 @Override
33 public void contextInitialized(ServletContextEvent sce)
34 {
35 throw new IllegalStateException("BAR LISTENER CALLED!");
36 }
37
38 @Override
39 public void contextDestroyed(ServletContextEvent sce)
40 {
41
42 }
43 }
44
45 public static class FooListener implements ServletContextListener
46 {
47
48 @Override
49 public void contextInitialized(ServletContextEvent sce)
50 {
51 if (sce.getServletContext().getAttribute("org.example.AnnotationTest.listenerTest") != null)
52 throw new IllegalStateException("FooListener already initialized");
53
54 //Can add a ServletContextListener from a ServletContainerInitializer
55 sce.getServletContext().setAttribute("org.example.AnnotationTest.listenerTest", Boolean.TRUE);
56
57 //Can't add a ServletContextListener from a ServletContextListener
58 try
59 {
60 sce.getServletContext().addListener(new BarListener());
61 sce.getServletContext().setAttribute("org.example.AnnotationTest.listenerRegoTest", Boolean.FALSE);
62 }
63 catch (UnsupportedOperationException e)
64 {
65 sce.getServletContext().setAttribute("org.example.AnnotationTest.listenerRegoTest", Boolean.TRUE);
66 }
67 catch (Exception e)
68 {
69 sce.getServletContext().setAttribute("org.example.AnnotationTest.listenerRegoTest", Boolean.FALSE);
70 }
71 }
72
73 @Override
74 public void contextDestroyed(ServletContextEvent sce)
75 {
76
77 }
78 }
79
80 @Override
81 public void onStartup(Set<Class<?>> classes, ServletContext context)
82 {
83 if (context.getAttribute("org.example.Foo") != null)
84 throw new IllegalStateException("FooInitializer on Startup already called");
85
86 context.setAttribute("org.example.Foo", new ArrayList<Class>(classes));
87 ServletRegistration.Dynamic reg = context.addServlet("AnnotationTest", "org.example.AnnotationTest");
88 context.setAttribute("org.example.AnnotationTest.complete", (reg == null));
89 context.addListener(new FooListener());
90
91 //test adding jsp file dynamically
92 ServletRegistration.Dynamic jspFile = context.addJspFile("dynamic.jsp", "/dynamic.jsp");
93 context.setAttribute("org.example.jsp.file", (jspFile != null));
94 jspFile.addMapping("/dynamicjsp/*");
95 }
96 }