1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 javax.servlet.ServletContextEvent;
26 import javax.servlet.ServletContextListener;
27
28 import org.eclipse.jetty.plus.annotation.ContainerInitializer;
29 import org.eclipse.jetty.util.MultiMap;
30 import org.eclipse.jetty.util.log.Log;
31 import org.eclipse.jetty.util.log.Logger;
32 import org.eclipse.jetty.webapp.WebAppContext;
33
34
35
36
37
38
39 public class ServletContainerInitializerListener implements ServletContextListener
40 {
41 private static final Logger LOG = Log.getLogger(ServletContainerInitializerListener.class);
42 protected WebAppContext _context = null;
43
44
45 public void setWebAppContext (WebAppContext context)
46 {
47 _context = context;
48 }
49
50
51
52
53 public void contextInitialized(ServletContextEvent sce)
54 {
55 List<ContainerInitializer> initializers = (List<ContainerInitializer>)_context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZERS);
56 MultiMap classMap = (MultiMap)_context.getAttribute(AnnotationConfiguration.CLASS_INHERITANCE_MAP);
57
58 if (initializers != null)
59 {
60 for (ContainerInitializer i : initializers)
61 {
62
63
64
65 if (i.getAnnotatedTypeNames() != null)
66 {
67 Set<String> annotatedClassNames = new HashSet<String>(i.getAnnotatedTypeNames());
68 for (String name : annotatedClassNames)
69 {
70
71 i.addApplicableTypeName(name);
72
73 if (classMap != null)
74 {
75 List<String> implementsOrExtends = (List<String>)classMap.getValues(name);
76 if (implementsOrExtends != null && !implementsOrExtends.isEmpty())
77 addInheritedTypes(classMap, i, implementsOrExtends);
78 }
79 }
80 }
81
82
83
84
85 if (i.getInterestedTypes() != null)
86 {
87 for (Class c : i.getInterestedTypes())
88 {
89 if (!c.isAnnotation())
90 {
91
92
93 if (classMap != null)
94 {
95 List<String> implementsOrExtends = (List<String>)classMap.getValues(c.getName());
96 if (implementsOrExtends != null && !implementsOrExtends.isEmpty())
97 addInheritedTypes(classMap, i, implementsOrExtends);
98 }
99 }
100 }
101 }
102
103
104 try
105 {
106 i.callStartup(_context);
107 }
108 catch (Exception e)
109 {
110 LOG.warn(e);
111 throw new RuntimeException(e);
112 }
113 }
114 }
115
116 }
117
118
119 void addInheritedTypes (MultiMap classMap, ContainerInitializer initializer, List<String> applicableTypes)
120 {
121 for (String s : applicableTypes)
122 {
123
124 initializer.addApplicableTypeName(s);
125
126
127 List<String> implementsOrExtends = (List<String>)classMap.getValues(s);
128 if (implementsOrExtends != null && !implementsOrExtends.isEmpty())
129 addInheritedTypes (classMap, initializer, implementsOrExtends);
130 }
131 }
132
133
134
135
136
137 public void contextDestroyed(ServletContextEvent sce)
138 {
139
140 }
141
142 }