1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.servlet; |
16 |
| |
17 |
| import java.io.IOException; |
18 |
| import java.util.Locale; |
19 |
| |
20 |
| import javax.servlet.Filter; |
21 |
| import javax.servlet.FilterChain; |
22 |
| import javax.servlet.FilterConfig; |
23 |
| import javax.servlet.ServletContext; |
24 |
| import javax.servlet.ServletException; |
25 |
| import javax.servlet.ServletRequest; |
26 |
| import javax.servlet.ServletResponse; |
27 |
| import javax.servlet.http.HttpServletRequest; |
28 |
| |
29 |
| import org.apache.commons.logging.Log; |
30 |
| import org.apache.commons.logging.LogFactory; |
31 |
| import org.apache.hivemind.ClassResolver; |
32 |
| import org.apache.hivemind.ModuleDescriptorProvider; |
33 |
| import org.apache.hivemind.Registry; |
34 |
| import org.apache.hivemind.impl.DefaultClassResolver; |
35 |
| import org.apache.hivemind.impl.RegistryBuilder; |
36 |
| import org.apache.hivemind.impl.XmlModuleDescriptorProvider; |
37 |
| import org.apache.hivemind.util.ContextResource; |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| public class HiveMindFilter implements Filter |
48 |
| { |
49 |
| private static final Log LOG = LogFactory.getLog(HiveMindFilter.class); |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| static final String REQUEST_KEY = "org.apache.hivemind.RequestRegistry"; |
56 |
| |
57 |
| static final String REBUILD_REQUEST_KEY = "org.apache.hivemind.RebuildRegistry"; |
58 |
| |
59 |
| |
60 |
| static final String HIVE_MODULE_XML = "/WEB-INF/hivemodule.xml"; |
61 |
| |
62 |
| private FilterConfig _filterConfig; |
63 |
| |
64 |
| private Registry _registry; |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
4
| public void init(FilterConfig config) throws ServletException
|
71 |
| { |
72 |
4
| _filterConfig = config;
|
73 |
| |
74 |
4
| initializeRegistry();
|
75 |
| |
76 |
| } |
77 |
| |
78 |
5
| private void initializeRegistry()
|
79 |
| { |
80 |
5
| long startTime = System.currentTimeMillis();
|
81 |
| |
82 |
5
| LOG.info(ServletMessages.filterInit());
|
83 |
| |
84 |
5
| try
|
85 |
| { |
86 |
5
| _registry = constructRegistry(_filterConfig);
|
87 |
| |
88 |
4
| LOG.info(ServletMessages.constructedRegistry(_registry, System.currentTimeMillis()
|
89 |
| - startTime)); |
90 |
| } |
91 |
| catch (Exception ex) |
92 |
| { |
93 |
1
| LOG.error(ex.getMessage(), ex);
|
94 |
| } |
95 |
| } |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
4
| protected Registry constructRegistry(FilterConfig config)
|
103 |
| { |
104 |
4
| RegistryBuilder builder = new RegistryBuilder();
|
105 |
| |
106 |
4
| ClassResolver resolver = new DefaultClassResolver();
|
107 |
| |
108 |
4
| builder.addModuleDescriptorProvider(getModuleDescriptorProvider(resolver));
|
109 |
| |
110 |
4
| addWebInfDescriptor(config.getServletContext(), resolver, builder);
|
111 |
| |
112 |
4
| return builder.constructRegistry(getRegistryLocale());
|
113 |
| } |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
4
| protected void addWebInfDescriptor(ServletContext context, ClassResolver resolver,
|
123 |
| RegistryBuilder builder) |
124 |
| { |
125 |
4
| ContextResource r = new ContextResource(context, HIVE_MODULE_XML);
|
126 |
| |
127 |
4
| if (r.getResourceURL() != null)
|
128 |
| { |
129 |
1
| ModuleDescriptorProvider provider = new XmlModuleDescriptorProvider(resolver, r);
|
130 |
| |
131 |
1
| builder.addModuleDescriptorProvider(provider);
|
132 |
| } |
133 |
| } |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
4
| protected Locale getRegistryLocale()
|
140 |
| { |
141 |
4
| return Locale.getDefault();
|
142 |
| } |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
4
| protected ModuleDescriptorProvider getModuleDescriptorProvider(ClassResolver resolver)
|
152 |
| { |
153 |
4
| return new XmlModuleDescriptorProvider(resolver);
|
154 |
| } |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
4
| public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
161 |
| throws IOException, ServletException |
162 |
| { |
163 |
4
| try
|
164 |
| { |
165 |
| |
166 |
| |
167 |
4
| if (_registry != null)
|
168 |
2
| _registry.setupThread();
|
169 |
| |
170 |
4
| request.setAttribute(REQUEST_KEY, _registry);
|
171 |
| |
172 |
4
| chain.doFilter(request, response);
|
173 |
| } |
174 |
| finally |
175 |
| { |
176 |
4
| cleanupThread();
|
177 |
| |
178 |
4
| checkRegistryRebuild(request);
|
179 |
| } |
180 |
| } |
181 |
| |
182 |
4
| private synchronized void checkRegistryRebuild(ServletRequest request)
|
183 |
| { |
184 |
4
| if (request.getAttribute(REBUILD_REQUEST_KEY) == null)
|
185 |
3
| return;
|
186 |
| |
187 |
1
| Registry oldRegistry = _registry;
|
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
1
| initializeRegistry();
|
194 |
| |
195 |
| |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
1
| oldRegistry.shutdown();
|
204 |
| } |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
4
| private void cleanupThread()
|
210 |
| { |
211 |
4
| try
|
212 |
| { |
213 |
4
| _registry.cleanupThread();
|
214 |
| } |
215 |
| catch (Exception ex) |
216 |
| { |
217 |
2
| LOG.error(ServletMessages.filterCleanupError(ex), ex);
|
218 |
| } |
219 |
| } |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
3
| public void destroy()
|
225 |
| { |
226 |
3
| if (_registry != null)
|
227 |
1
| _registry.shutdown();
|
228 |
| |
229 |
3
| _filterConfig = null;
|
230 |
| } |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
| |
236 |
1
| public static Registry getRegistry(HttpServletRequest request)
|
237 |
| { |
238 |
1
| return (Registry) request.getAttribute(REQUEST_KEY);
|
239 |
| } |
240 |
| |
241 |
| |
242 |
| |
243 |
| |
244 |
| |
245 |
1
| public static void rebuildRegistry(HttpServletRequest request)
|
246 |
| { |
247 |
1
| request.setAttribute(REBUILD_REQUEST_KEY, Boolean.TRUE);
|
248 |
| } |
249 |
| |
250 |
| } |