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.webapp.DiscoveredAnnotation;
25  import org.eclipse.jetty.webapp.MetaData;
26  import org.eclipse.jetty.webapp.WebAppContext;
27  import org.eclipse.jetty.webapp.Origin;
28  
29  /**
30   * WebListenerAnnotation
31   *
32   *
33   */
34  public class WebListenerAnnotation extends DiscoveredAnnotation
35  {
36  
37      /**
38       * @param context
39       * @param className
40       */
41      public WebListenerAnnotation(WebAppContext context, String className)
42      {
43          super(context, className);
44      }
45  
46      /** 
47       * @see org.eclipse.jetty.annotations.ClassAnnotation#apply()
48       */
49      public void apply()
50      {
51          // TODO check algorithm against ordering rules for descriptors v annotations
52          
53          Class clazz = getTargetClass();
54          
55          if (clazz == null)
56          {
57              Log.warn(_className+" cannot be loaded");
58              return;
59          }
60  
61          try
62          {
63              if (ServletContextListener.class.isAssignableFrom(clazz) || 
64                      ServletContextAttributeListener.class.isAssignableFrom(clazz) ||
65                      ServletRequestListener.class.isAssignableFrom(clazz) ||
66                      ServletRequestAttributeListener.class.isAssignableFrom(clazz) ||
67                      HttpSessionListener.class.isAssignableFrom(clazz) ||
68                      HttpSessionAttributeListener.class.isAssignableFrom(clazz))
69              {
70                  java.util.EventListener listener = (java.util.EventListener)clazz.newInstance();
71                  MetaData metaData = _context.getMetaData();
72                  if (metaData.getOrigin(clazz.getName()+".listener") == Origin.NotSet)
73                      _context.addEventListener(listener);
74              }
75              else
76                  Log.warn(clazz.getName()+" does not implement one of the servlet listener interfaces");
77          }
78          catch (Exception e)
79          {
80              Log.warn(e);
81          }
82      }
83  }