View Javadoc

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