Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fed2b6a4fc107c187e864b427eb9f321489ef4b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
//  ========================================================================
//  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.annotations;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.plus.annotation.ContainerInitializer;
import org.eclipse.jetty.util.MultiMap;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.webapp.WebAppContext;

/**
 * ServletContainerInitializerListener
 *
 *
 */
public class ServletContainerInitializerListener implements ServletContextListener
{
    private static final Logger LOG = Log.getLogger(ServletContainerInitializerListener.class);
    protected WebAppContext _context = null;
    
    
    public void setWebAppContext (WebAppContext context)
    {
        _context = context;
    }

    /** 
     * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent sce)
    {
        List<ContainerInitializer> initializers = (List<ContainerInitializer>)_context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZERS);
        MultiMap classMap = (MultiMap)_context.getAttribute(AnnotationConfiguration.CLASS_INHERITANCE_MAP);
        
        if (initializers != null)
        {
            for (ContainerInitializer i : initializers)
            {
                //We have already found the classes that directly have an annotation that was in the HandlesTypes
                //annotation of the ServletContainerInitializer. For each of those classes, walk the inheritance
                //hierarchy to find classes that extend or implement them.
                if (i.getAnnotatedTypeNames() != null)
                {
                    Set<String> annotatedClassNames = new HashSet<String>(i.getAnnotatedTypeNames());
                    for (String name : annotatedClassNames)
                    {
                        //add the class with the annotation
                        i.addApplicableTypeName(name);
                        //add the classes that inherit the annotation
                        if (classMap != null)
                        {
                            List<String> implementsOrExtends = (List<String>)classMap.getValues(name);
                            if (implementsOrExtends != null && !implementsOrExtends.isEmpty())
                                addInheritedTypes(classMap, i, implementsOrExtends);
                        }
                    }
                }


                //Now we need to look at the HandlesTypes classes that were not annotations. We need to
                //find all classes that extend or implement them.
                if (i.getInterestedTypes() != null)
                {
                    for (Class c : i.getInterestedTypes())
                    {
                        if (!c.isAnnotation())
                        {
                            //add the classes that implement or extend the class.
                            //TODO but not including the class itself?
                            if (classMap != null)
                            {
                                List<String> implementsOrExtends = (List<String>)classMap.getValues(c.getName());
                                if (implementsOrExtends != null && !implementsOrExtends.isEmpty())
                                    addInheritedTypes(classMap, i, implementsOrExtends);
                            }
                        }
                    }
                }

                //instantiate ServletContainerInitializers, call doStart
                try
                {
                    i.callStartup(_context);
                }
                catch (Exception e)
                {
                    LOG.warn(e);
                    throw new RuntimeException(e);
                }
            }
        }       
    }

    
    void addInheritedTypes (MultiMap classMap, ContainerInitializer initializer, List<String> applicableTypes)
    {
        for (String s : applicableTypes)
        {
            //add the name of the class that extends or implements
            initializer.addApplicableTypeName(s);
            
            //walk the hierarchy and find all types that extend or implement it
            List<String> implementsOrExtends = (List<String>)classMap.getValues(s);
            if (implementsOrExtends != null && !implementsOrExtends.isEmpty())
                addInheritedTypes (classMap, initializer, implementsOrExtends);
        }
    }
    
    
    /** 
     * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent sce)
    {
       
    }

}

Back to the top