View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package com.acme.initializer;
20  
21  import java.util.ArrayList;
22  import java.util.Set;
23  
24  import javax.servlet.ServletContainerInitializer;
25  import javax.servlet.ServletContext;
26  import javax.servlet.ServletContextEvent;
27  import javax.servlet.ServletContextListener;
28  import javax.servlet.ServletRegistration;
29  import javax.servlet.annotation.HandlesTypes;
30  
31  @HandlesTypes ({javax.servlet.Servlet.class, Foo.class})
32  public class FooInitializer implements ServletContainerInitializer
33  {
34      public static class BarListener implements ServletContextListener
35      {
36  
37          /** 
38           * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
39           */
40          @Override
41          public void contextInitialized(ServletContextEvent sce)
42          {
43              throw new IllegalStateException("BAR LISTENER CALLED!");
44          }
45  
46          /** 
47           * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
48           */
49          @Override
50          public void contextDestroyed(ServletContextEvent sce)
51          {
52              
53          }
54          
55      }
56  
57      public static class FooListener implements ServletContextListener
58      {
59  
60          /** 
61           * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
62           */
63          @Override
64          public void contextInitialized(ServletContextEvent sce)
65          {
66              //Can add a ServletContextListener from a ServletContainerInitializer
67              sce.getServletContext().setAttribute("com.acme.AnnotationTest.listenerTest", Boolean.TRUE);
68              
69              //Can't add a ServletContextListener from a ServletContextListener
70              try
71              {
72                  sce.getServletContext().addListener(new BarListener());
73                  sce.getServletContext().setAttribute("com.acme.AnnotationTest.listenerRegoTest", Boolean.FALSE);
74              }
75              catch (UnsupportedOperationException e)
76              {
77                  sce.getServletContext().setAttribute("com.acme.AnnotationTest.listenerRegoTest", Boolean.TRUE);
78              }
79              catch (Exception e)
80              {
81                  sce.getServletContext().setAttribute("com.acme.AnnotationTest.listenerRegoTest", Boolean.FALSE);
82              }
83          }
84  
85          /** 
86           * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
87           */
88          @Override
89          public void contextDestroyed(ServletContextEvent sce)
90          {
91              
92          }
93          
94      }
95      public void onStartup(Set<Class<?>> classes, ServletContext context)
96      {
97          context.setAttribute("com.acme.Foo", new ArrayList<Class>(classes));
98          ServletRegistration.Dynamic reg = context.addServlet("AnnotationTest", "com.acme.AnnotationTest");
99          context.setAttribute("com.acme.AnnotationTest.complete", (reg == null));
100         context.addListener(new FooListener());
101     }
102 }