View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.annotations;
15  
16  import javax.servlet.ServletContextAttributeListener;
17  import javax.servlet.ServletContextListener;
18  import javax.servlet.ServletRequestAttributeListener;
19  import javax.servlet.ServletRequestListener;
20  import javax.servlet.http.HttpSessionAttributeListener;
21  import javax.servlet.http.HttpSessionListener;
22  
23  import org.eclipse.jetty.util.log.Log;
24  import org.eclipse.jetty.util.log.Logger;
25  import org.eclipse.jetty.webapp.DiscoveredAnnotation;
26  import org.eclipse.jetty.webapp.MetaData;
27  import org.eclipse.jetty.webapp.WebAppContext;
28  import org.eclipse.jetty.webapp.Origin;
29  
30  /**
31   * WebListenerAnnotation
32   *
33   *
34   */
35  public class WebListenerAnnotation extends DiscoveredAnnotation
36  {
37      private static final Logger LOG = Log.getLogger(WebListenerAnnotation.class);
38  
39      /**
40       * @param context
41       * @param className
42       */
43      public WebListenerAnnotation(WebAppContext context, String className)
44      {
45          super(context, className);
46      }
47  
48      /** 
49       * @see org.eclipse.jetty.annotations.ClassAnnotation#apply()
50       */
51      public void apply()
52      {
53          // TODO check algorithm against ordering rules for descriptors v annotations
54          
55          Class clazz = getTargetClass();
56          
57          if (clazz == null)
58          {
59              LOG.warn(_className+" cannot be loaded");
60              return;
61          }
62  
63          try
64          {
65              if (ServletContextListener.class.isAssignableFrom(clazz) || 
66                      ServletContextAttributeListener.class.isAssignableFrom(clazz) ||
67                      ServletRequestListener.class.isAssignableFrom(clazz) ||
68                      ServletRequestAttributeListener.class.isAssignableFrom(clazz) ||
69                      HttpSessionListener.class.isAssignableFrom(clazz) ||
70                      HttpSessionAttributeListener.class.isAssignableFrom(clazz))
71              {
72                  java.util.EventListener listener = (java.util.EventListener)clazz.newInstance();
73                  MetaData metaData = _context.getMetaData();
74                  if (metaData.getOrigin(clazz.getName()+".listener") == Origin.NotSet)
75                      _context.addEventListener(listener);
76              }
77              else
78                  LOG.warn(clazz.getName()+" does not implement one of the servlet listener interfaces");
79          }
80          catch (Exception e)
81          {
82              LOG.warn(e);
83          }
84      }
85  }