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 java.util.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  import javax.servlet.ServletContextEvent;
26  import javax.servlet.ServletContextListener;
27  
28  import org.eclipse.jetty.plus.annotation.ContainerInitializer;
29  import org.eclipse.jetty.util.MultiMap;
30  import org.eclipse.jetty.util.log.Log;
31  import org.eclipse.jetty.util.log.Logger;
32  import org.eclipse.jetty.webapp.WebAppContext;
33  
34  /**
35   * ServletContainerInitializerListener
36   *
37   *
38   */
39  public class ServletContainerInitializerListener implements ServletContextListener
40  {
41      private static final Logger LOG = Log.getLogger(ServletContainerInitializerListener.class);
42      protected WebAppContext _context = null;
43  
44  
45      public void setWebAppContext (WebAppContext context)
46      {
47          _context = context;
48      }
49  
50      /**
51       * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
52       */
53      public void contextInitialized(ServletContextEvent sce)
54      {
55          List<ContainerInitializer> initializers = (List<ContainerInitializer>)_context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZERS);
56          MultiMap classMap = (MultiMap)_context.getAttribute(AnnotationConfiguration.CLASS_INHERITANCE_MAP);
57  
58          if (initializers != null)
59          {
60              for (ContainerInitializer i : initializers)
61              {
62                  //We have already found the classes that directly have an annotation that was in the HandlesTypes
63                  //annotation of the ServletContainerInitializer. For each of those classes, walk the inheritance
64                  //hierarchy to find classes that extend or implement them.
65                  if (i.getAnnotatedTypeNames() != null)
66                  {
67                      Set<String> annotatedClassNames = new HashSet<String>(i.getAnnotatedTypeNames());
68                      for (String name : annotatedClassNames)
69                      {
70                          //add the class with the annotation
71                          i.addApplicableTypeName(name);
72                          //add the classes that inherit the annotation
73                          if (classMap != null)
74                          {
75                              List<String> implementsOrExtends = (List<String>)classMap.getValues(name);
76                              if (implementsOrExtends != null && !implementsOrExtends.isEmpty())
77                                  addInheritedTypes(classMap, i, implementsOrExtends);
78                          }
79                      }
80                  }
81  
82  
83                  //Now we need to look at the HandlesTypes classes that were not annotations. We need to
84                  //find all classes that extend or implement them.
85                  if (i.getInterestedTypes() != null)
86                  {
87                      for (Class c : i.getInterestedTypes())
88                      {
89                          if (!c.isAnnotation())
90                          {
91                              //add the classes that implement or extend the class.
92                              //TODO but not including the class itself?
93                              if (classMap != null)
94                              {
95                                  List<String> implementsOrExtends = (List<String>)classMap.getValues(c.getName());
96                                  if (implementsOrExtends != null && !implementsOrExtends.isEmpty())
97                                      addInheritedTypes(classMap, i, implementsOrExtends);
98                              }
99                          }
100                     }
101                 }
102 
103                 //instantiate ServletContainerInitializers, call doStart
104                 try
105                 {
106                     i.callStartup(_context);
107                 }
108                 catch (Exception e)
109                 {
110                     LOG.warn(e);
111                     throw new RuntimeException(e);
112                 }
113             }
114         }
115 
116     }
117 
118 
119     void addInheritedTypes (MultiMap classMap, ContainerInitializer initializer, List<String> applicableTypes)
120     {
121         for (String s : applicableTypes)
122         {
123             //add the name of the class that extends or implements
124             initializer.addApplicableTypeName(s);
125 
126             //walk the hierarchy and find all types that extend or implement it
127             List<String> implementsOrExtends = (List<String>)classMap.getValues(s);
128             if (implementsOrExtends != null && !implementsOrExtends.isEmpty())
129                 addInheritedTypes (classMap, initializer, implementsOrExtends);
130         }
131     }
132 
133 
134     /**
135      * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
136      */
137     public void contextDestroyed(ServletContextEvent sce)
138     {
139 
140     }
141 
142 }