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