|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
HiveMindFilter.java | 100% | 100% | 100% | 100% |
|
1 |
// Copyright 2004 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
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.ServletException;
|
|
24 |
import javax.servlet.ServletRequest;
|
|
25 |
import javax.servlet.ServletResponse;
|
|
26 |
import javax.servlet.http.HttpServletRequest;
|
|
27 |
|
|
28 |
import org.apache.commons.logging.Log;
|
|
29 |
import org.apache.commons.logging.LogFactory;
|
|
30 |
import org.apache.hivemind.ClassResolver;
|
|
31 |
import org.apache.hivemind.Registry;
|
|
32 |
import org.apache.hivemind.impl.DefaultClassResolver;
|
|
33 |
import org.apache.hivemind.impl.RegistryBuilder;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* Servlet filter that constructs the Registry at startup. It ensures that each request is
|
|
37 |
* properly terminated with a call to
|
|
38 |
* {@link org.apache.hivemind.service.ThreadEventNotifier#fireThreadCleanup()}.
|
|
39 |
* It also makes the Registry available during the request by
|
|
40 |
* storing it as a request attribute.
|
|
41 |
*
|
|
42 |
* @author Howard Lewis Ship
|
|
43 |
*/
|
|
44 |
public class HiveMindFilter implements Filter |
|
45 |
{ |
|
46 |
private static final Log LOG = LogFactory.getLog(HiveMindFilter.class); |
|
47 |
|
|
48 |
/**
|
|
49 |
* ServletContext attribute key that stores the Registry.
|
|
50 |
*/
|
|
51 |
public static final String CONTEXT_KEY = "org.apache.hivemind.DefaultRegistry"; |
|
52 |
|
|
53 |
/**
|
|
54 |
* Request attribute key that stores the Registry.
|
|
55 |
*/
|
|
56 |
|
|
57 |
public static final String REQUEST_KEY = "org.apache.hivemind.RequestRegistry"; |
|
58 |
|
|
59 |
private Registry _registry;
|
|
60 |
|
|
61 |
/**
|
|
62 |
* Constructs a {@link Registry} and stores it into the
|
|
63 |
* <code>ServletContext</code>. Any exception throws is logged.
|
|
64 |
*/
|
|
65 | 2 |
public void init(FilterConfig config) throws ServletException |
66 |
{ |
|
67 | 2 |
LOG.info(ServletMessages.filterInit()); |
68 |
|
|
69 | 2 |
try
|
70 |
{ |
|
71 | 2 |
ClassResolver resolver = new DefaultClassResolver();
|
72 | 2 |
RegistryBuilder builder = new RegistryBuilder();
|
73 |
|
|
74 | 2 |
builder.processModules(resolver); |
75 |
|
|
76 | 2 |
_registry = builder.constructRegistry(getRegistryLocale()); |
77 |
|
|
78 | 2 |
config.getServletContext().setAttribute(CONTEXT_KEY, _registry); |
79 |
|
|
80 | 1 |
LOG.info(ServletMessages.storedRegistry(_registry, CONTEXT_KEY)); |
81 |
} |
|
82 |
catch (Exception ex)
|
|
83 |
{ |
|
84 | 1 |
LOG.error(ex.getMessage(), ex); |
85 |
} |
|
86 |
} |
|
87 |
|
|
88 |
/**
|
|
89 |
* Returns the default Locale. Subclasses may override to select a particular
|
|
90 |
* locale for the Registry.
|
|
91 |
*/
|
|
92 | 2 |
protected Locale getRegistryLocale()
|
93 |
{ |
|
94 | 2 |
return Locale.getDefault();
|
95 |
} |
|
96 |
|
|
97 |
/**
|
|
98 |
* Passes the request to the filter chain, but then invokes
|
|
99 |
* {@link ThreadEventNotifier#fireThreadCleanup()} (from a finally block).
|
|
100 |
*/
|
|
101 | 3 |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
102 |
throws IOException, ServletException
|
|
103 |
{ |
|
104 | 3 |
try
|
105 |
{ |
|
106 | 3 |
request.setAttribute(REQUEST_KEY, _registry); |
107 |
|
|
108 | 3 |
chain.doFilter(request, response); |
109 |
} |
|
110 |
finally
|
|
111 |
{ |
|
112 | 3 |
cleanupThread(); |
113 |
} |
|
114 |
|
|
115 |
} |
|
116 |
|
|
117 |
/**
|
|
118 |
* Cleanup the thread, ignoring any exceptions that may be thrown.
|
|
119 |
*/
|
|
120 | 3 |
private void cleanupThread() |
121 |
{ |
|
122 | 3 |
try
|
123 |
{ |
|
124 | 3 |
_registry.cleanupThread(); |
125 |
} |
|
126 |
catch (Exception ex)
|
|
127 |
{ |
|
128 | 1 |
LOG.error(ServletMessages.filterCleanupError(ex), ex); |
129 |
} |
|
130 |
} |
|
131 |
|
|
132 |
/**
|
|
133 |
* Invokes {@link Registry#shutdown()}.
|
|
134 |
*/
|
|
135 | 3 |
public void destroy() |
136 |
{ |
|
137 | 3 |
if (_registry != null) |
138 | 2 |
_registry.shutdown(); |
139 |
} |
|
140 |
|
|
141 |
/**
|
|
142 |
* Returns the {@link Registry} that was stored as a request attribute
|
|
143 |
* in {@link #doFilter(ServletRequest, ServletResponse, FilterChain)}.
|
|
144 |
*/
|
|
145 | 1 |
public static Registry getRegistry(HttpServletRequest request) |
146 |
{ |
|
147 | 1 |
return (Registry) request.getAttribute(REQUEST_KEY);
|
148 |
} |
|
149 |
|
|
150 |
} |
|
151 |
|
|