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  
15  package org.eclipse.jetty.annotations;
16  
17  
18  import java.util.HashSet;
19  import java.util.List;
20  import java.util.Set;
21  
22  import org.eclipse.jetty.plus.annotation.ContainerInitializer;
23  import org.eclipse.jetty.util.MultiMap;
24  import org.eclipse.jetty.webapp.Configuration;
25  import org.eclipse.jetty.webapp.WebAppContext;
26  
27  /**
28   * ContainerInitializerConfiguration
29   *
30   * Apply the ServletContainerInitializers. 
31   */
32  public class ContainerInitializerConfiguration  implements Configuration
33  {
34      public static final String CONTAINER_INITIALIZERS = "org.eclipse.jetty.containerInitializers";
35  
36      public void preConfigure(WebAppContext context) throws Exception
37      {  
38      }
39  
40      public void configure(WebAppContext context) throws Exception
41      {
42      }
43      
44      public void postConfigure(WebAppContext context) throws Exception
45      {
46          List<ContainerInitializer> initializers = (List<ContainerInitializer>)context.getAttribute(CONTAINER_INITIALIZERS);
47          MultiMap classMap = (MultiMap)context.getAttribute(AnnotationConfiguration.CLASS_INHERITANCE_MAP);
48          
49          if (initializers != null)
50          {
51              for (ContainerInitializer i : initializers)
52              {
53                  //We have already found the classes that directly have an annotation that was in the HandlesTypes
54                  //annotation of the ServletContainerInitializer. For each of those classes, walk the inheritance
55                  //hierarchy to find classes that extend or implement them.
56                  if (i.getAnnotatedTypeNames() != null)
57                  {
58                      Set<String> annotatedClassNames = new HashSet<String>(i.getAnnotatedTypeNames());
59                      for (String name : annotatedClassNames)
60                      {
61                          //add the class with the annotation
62                          i.addApplicableTypeName(name);
63                          //add the classes that inherit the annotation
64                          List<String> implementsOrExtends = (List<String>)classMap.getValues(name);
65                          if (implementsOrExtends != null && !implementsOrExtends.isEmpty())
66                              addInheritedTypes(classMap, i, implementsOrExtends);
67                      }
68                  }
69  
70  
71                  //Now we need to look at the HandlesTypes classes that were not annotations. We need to
72                  //find all classes that extend or implement them.
73                  if (i.getInterestedTypes() != null)
74                  {
75                      for (Class c : i.getInterestedTypes())
76                      {
77                          if (!c.isAnnotation())
78                          {
79                              //add the classes that implement or extend the class.
80                              //TODO but not including the class itself?
81                              List<String> implementsOrExtends = (List<String>)classMap.getValues(c.getName());
82                              if (implementsOrExtends != null && !implementsOrExtends.isEmpty())
83                                  addInheritedTypes(classMap, i, implementsOrExtends);
84                          }
85                      }
86                  }
87                  //instantiate ServletContainerInitializers, call doStart
88                  i.callStartup(context);
89              }
90  
91              //TODO Email from Jan Luehe 18 August: after all ServletContainerInitializers have been
92              //called, need to check to see if there are any ServletRegistrations remaining
93              //that are "preliminary" and fail the deployment if so.
94          }
95      }
96  
97      public void deconfigure(WebAppContext context) throws Exception
98      {  
99      }
100 
101     
102     void addInheritedTypes (MultiMap classMap, ContainerInitializer initializer, List<String> applicableTypes)
103     {
104         for (String s : applicableTypes)
105         {
106             //add the name of the class that extends or implements
107             initializer.addApplicableTypeName(s);
108             
109             //walk the hierarchy and find all types that extend or implement it
110             List<String> implementsOrExtends = (List<String>)classMap.getValues(s);
111             if (implementsOrExtends != null && !implementsOrExtends.isEmpty())
112                 addInheritedTypes (classMap, initializer, implementsOrExtends);
113         }
114     }
115    
116 }