View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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;
20  import java.util.EventListener;
21  
22  import javax.annotation.Resource;
23  import javax.servlet.ServletContextAttributeEvent;
24  import javax.servlet.ServletContextAttributeListener;
25  import javax.servlet.ServletContextEvent;
26  import javax.servlet.ServletContextListener;
27  import javax.servlet.ServletRequestAttributeEvent;
28  import javax.servlet.ServletRequestAttributeListener;
29  import javax.servlet.ServletRequestEvent;
30  import javax.servlet.ServletRequestListener;
31  import javax.servlet.annotation.WebListener;
32  import javax.servlet.http.HttpSessionActivationListener;
33  import javax.servlet.http.HttpSessionAttributeListener;
34  import javax.servlet.http.HttpSessionBindingEvent;
35  import javax.servlet.http.HttpSessionEvent;
36  import javax.servlet.http.HttpSessionListener;
37  
38  
39  @Foo(1)
40  @WebListener
41  public class TestListener implements HttpSessionListener,  HttpSessionAttributeListener, HttpSessionActivationListener, ServletContextListener, ServletContextAttributeListener, ServletRequestListener, ServletRequestAttributeListener
42  {
43      public class NaughtyServletContextListener implements ServletContextListener
44      {
45  
46          @Override
47          public void contextInitialized(ServletContextEvent sce)
48          {
49              throw new IllegalStateException("Should not call NaughtServletContextListener.contextInitialized");
50          }
51  
52          @Override
53          public void contextDestroyed(ServletContextEvent sce)
54          {
55              throw new IllegalStateException("Should not call NaughtServletContextListener.contextDestroyed");
56          }
57      }
58      
59      public class InvalidListener implements EventListener
60      {
61          
62      }
63  
64      @Resource(mappedName="maxAmount")
65      private Double maxAmount;
66  
67      public void attributeAdded(HttpSessionBindingEvent se)
68      {
69          // System.err.println("attributedAdded "+se);
70      }
71  
72      public void attributeRemoved(HttpSessionBindingEvent se)
73      {
74          // System.err.println("attributeRemoved "+se);
75      }
76  
77      public void attributeReplaced(HttpSessionBindingEvent se)
78      {
79          // System.err.println("attributeReplaced "+se);
80      }
81  
82      public void sessionWillPassivate(HttpSessionEvent se)
83      {
84          // System.err.println("sessionWillPassivate "+se);
85      }
86  
87      public void sessionDidActivate(HttpSessionEvent se)
88      {
89          // System.err.println("sessionDidActivate "+se);
90      }
91  
92      public void contextInitialized(ServletContextEvent sce)
93      {
94          //Can't add a ServletContextListener from a ServletContextListener even if it is declared in web.xml
95          try
96          {
97              sce.getServletContext().addListener(new NaughtyServletContextListener());
98              sce.getServletContext().setAttribute("com.acme.AnnotationTest.sclFromSclRegoTest", Boolean.FALSE);
99          }
100         catch (IllegalArgumentException e)
101         {
102             sce.getServletContext().setAttribute("com.acme.AnnotationTest.sclFromSclRegoTest", Boolean.TRUE);
103         }
104         catch (Exception e)
105         {
106             sce.getServletContext().setAttribute("com.acme.AnnotationTest.sclFromSclRegoTest", Boolean.FALSE);
107         }
108         
109         
110         //Can't add an EventListener not part of the specified list for addListener()
111         try
112         {
113             sce.getServletContext().addListener(new InvalidListener());
114             sce.getServletContext().setAttribute("com.acme.AnnotationTest.invalidListenerRegoTest", Boolean.FALSE);
115         }
116         catch (IllegalArgumentException e)
117         {
118             sce.getServletContext().setAttribute("com.acme.AnnotationTest.invalidListenerRegoTest", Boolean.TRUE);
119         }
120         catch (Exception e)
121         {
122             sce.getServletContext().setAttribute("com.acme.AnnotationTest.invalidListenerRegoTest", Boolean.FALSE);
123         } 
124     }
125 
126     public void contextDestroyed(ServletContextEvent sce)
127     {
128         // System.err.println("contextDestroyed "+sce);
129     }
130 
131     public void attributeAdded(ServletContextAttributeEvent scab)
132     {
133         // System.err.println("attributeAdded "+scab);
134     }
135 
136     public void attributeRemoved(ServletContextAttributeEvent scab)
137     {
138         // System.err.println("attributeRemoved "+scab);
139     }
140 
141     public void attributeReplaced(ServletContextAttributeEvent scab)
142     {
143         // System.err.println("attributeReplaced "+scab);
144     }
145 
146     public void requestDestroyed(ServletRequestEvent sre)
147     {
148         // System.err.println("requestDestroyed "+sre);
149     }
150 
151     public void requestInitialized(ServletRequestEvent sre)
152     {
153         // System.err.println("requestInitialized "+sre);
154     }
155 
156     public void attributeAdded(ServletRequestAttributeEvent srae)
157     {
158         // System.err.println("attributeAdded "+srae);
159     }
160 
161     public void attributeRemoved(ServletRequestAttributeEvent srae)
162     {
163         // System.err.println("attributeRemoved "+srae);
164     }
165 
166     public void attributeReplaced(ServletRequestAttributeEvent srae)
167     {
168         // System.err.println("attributeReplaced "+srae);
169     }
170 
171     public void sessionCreated(HttpSessionEvent se)
172     {
173         // System.err.println("sessionCreated "+se);
174     }
175 
176     public void sessionDestroyed(HttpSessionEvent se)
177     {
178         // System.err.println("sessionDestroyed "+se);
179     }
180 
181     public void requestCompleted(ServletRequestEvent rre)
182     {
183         // TODO Auto-generated method stub
184         
185     }
186 
187     public void requestResumed(ServletRequestEvent rre)
188     {
189         // TODO Auto-generated method stub
190         
191     }
192 
193     public void requestSuspended(ServletRequestEvent rre)
194     {
195         // TODO Auto-generated method stub
196         
197     }
198 
199 }