View Javadoc

1   package org.eclipse.jetty.annotations;
2   
3   import java.util.HashSet;
4   import java.util.List;
5   import java.util.Set;
6   
7   import javax.servlet.ServletContextEvent;
8   import javax.servlet.ServletContextListener;
9   
10  import org.eclipse.jetty.annotations.AnnotationConfiguration;
11  import org.eclipse.jetty.plus.annotation.ContainerInitializer;
12  import org.eclipse.jetty.util.MultiMap;
13  import org.eclipse.jetty.webapp.WebAppContext;
14  
15  // ========================================================================
16  // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
17  // ------------------------------------------------------------------------
18  // All rights reserved. This program and the accompanying materials
19  // are made available under the terms of the Eclipse Public License v1.0
20  // and Apache License v2.0 which accompanies this distribution.
21  // The Eclipse Public License is available at 
22  // http://www.eclipse.org/legal/epl-v10.html
23  // The Apache License v2.0 is available at
24  // http://www.opensource.org/licenses/apache2.0.php
25  // You may elect to redistribute this code under either of these licenses. 
26  // ========================================================================
27  
28  /**
29   * ServletContainerInitializerListener
30   *
31   *
32   */
33  public class ServletContainerInitializerListener implements ServletContextListener
34  {
35      WebAppContext _context = null;
36      
37      
38      public void setWebAppContext (WebAppContext context)
39      {
40          _context = context;
41      }
42  
43      /** 
44       * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
45       */
46      public void contextInitialized(ServletContextEvent sce)
47      {
48          List<ContainerInitializer> initializers = (List<ContainerInitializer>)_context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZERS);
49          MultiMap classMap = (MultiMap)_context.getAttribute(AnnotationConfiguration.CLASS_INHERITANCE_MAP);
50          
51          if (initializers != null)
52          {
53              for (ContainerInitializer i : initializers)
54              {
55                  //We have already found the classes that directly have an annotation that was in the HandlesTypes
56                  //annotation of the ServletContainerInitializer. For each of those classes, walk the inheritance
57                  //hierarchy to find classes that extend or implement them.
58                  if (i.getAnnotatedTypeNames() != null)
59                  {
60                      Set<String> annotatedClassNames = new HashSet<String>(i.getAnnotatedTypeNames());
61                      for (String name : annotatedClassNames)
62                      {
63                          //add the class with the annotation
64                          i.addApplicableTypeName(name);
65                          //add the classes that inherit the annotation
66                          if (classMap != null)
67                          {
68                              List<String> implementsOrExtends = (List<String>)classMap.getValues(name);
69                              if (implementsOrExtends != null && !implementsOrExtends.isEmpty())
70                                  addInheritedTypes(classMap, i, implementsOrExtends);
71                          }
72                      }
73                  }
74  
75  
76                  //Now we need to look at the HandlesTypes classes that were not annotations. We need to
77                  //find all classes that extend or implement them.
78                  if (i.getInterestedTypes() != null)
79                  {
80                      for (Class c : i.getInterestedTypes())
81                      {
82                          if (!c.isAnnotation())
83                          {
84                              //add the classes that implement or extend the class.
85                              //TODO but not including the class itself?
86                              if (classMap != null)
87                              {
88                                  List<String> implementsOrExtends = (List<String>)classMap.getValues(c.getName());
89                                  if (implementsOrExtends != null && !implementsOrExtends.isEmpty())
90                                      addInheritedTypes(classMap, i, implementsOrExtends);
91                              }
92                          }
93                      }
94                  }
95  
96                  //instantiate ServletContainerInitializers, call doStart
97                  try
98                  {
99                      i.callStartup(_context);
100                 }
101                 catch (Exception e)
102                 {
103                     //OK, how do I throw an exception such that it really stops the startup sequence?
104                     e.printStackTrace();
105                 }
106             }
107 
108             //TODO Email from Jan Luehe 18 August: after all ServletContainerInitializers have been
109             //called, need to check to see if there are any ServletRegistrations remaining
110             //that are "preliminary" and fail the deployment if so.
111         } 
112         
113     }
114 
115     
116     void addInheritedTypes (MultiMap classMap, ContainerInitializer initializer, List<String> applicableTypes)
117     {
118         for (String s : applicableTypes)
119         {
120             //add the name of the class that extends or implements
121             initializer.addApplicableTypeName(s);
122             
123             //walk the hierarchy and find all types that extend or implement it
124             List<String> implementsOrExtends = (List<String>)classMap.getValues(s);
125             if (implementsOrExtends != null && !implementsOrExtends.isEmpty())
126                 addInheritedTypes (classMap, initializer, implementsOrExtends);
127         }
128     }
129     
130     
131     /** 
132      * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
133      */
134     public void contextDestroyed(ServletContextEvent sce)
135     {
136         // TODO Auto-generated method stub
137         
138     }
139 
140 }