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.annotations.AnnotationConfiguration;
29 import org.eclipse.jetty.plus.annotation.ContainerInitializer;
30 import org.eclipse.jetty.util.MultiMap;
31 import org.eclipse.jetty.util.log.Log;
32 import org.eclipse.jetty.util.log.Logger;
33 import org.eclipse.jetty.webapp.WebAppContext;
34
35
36
37
38
39
40 public class ServletContainerInitializerListener implements ServletContextListener
41 {
42 private static final Logger LOG = Log.getLogger(ServletContainerInitializerListener.class);
43 protected WebAppContext _context = null;
44
45
46 public void setWebAppContext (WebAppContext context)
47 {
48 _context = context;
49 }
50
51
52
53
54 public void contextInitialized(ServletContextEvent sce)
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
64
65
66 if (i.getAnnotatedTypeNames() != null)
67 {
68 Set<String> annotatedClassNames = new HashSet<String>(i.getAnnotatedTypeNames());
69 for (String name : annotatedClassNames)
70 {
71
72 i.addApplicableTypeName(name);
73
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
85
86 if (i.getInterestedTypes() != null)
87 {
88 for (Class c : i.getInterestedTypes())
89 {
90 if (!c.isAnnotation())
91 {
92
93
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
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 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 }