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