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