// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.hivemind.servlet;
import java.io.IOException;
import java.util.Locale;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hivemind.ClassResolver;
import org.apache.hivemind.Registry;
import org.apache.hivemind.impl.DefaultClassResolver;
import org.apache.hivemind.impl.RegistryBuilder;
/**
* Servlet filter that constructs the Registry at startup. It ensures that each request is
* properly terminated with a call to
* {@link org.apache.hivemind.service.ThreadEventNotifier#fireThreadCleanup()}.
* It also makes the Registry available during the request by
* storing it as a request attribute.
*
* @author Howard Lewis Ship
*/
public class HiveMindFilter implements Filter
{
private static final Log LOG = LogFactory.getLog(HiveMindFilter.class);
/**
* ServletContext attribute key that stores the Registry.
*/
public static final String CONTEXT_KEY = "org.apache.hivemind.DefaultRegistry";
/**
* Request attribute key that stores the Registry.
*/
public static final String REQUEST_KEY = "org.apache.hivemind.RequestRegistry";
private Registry _registry;
/**
* Constructs a {@link Registry} and stores it into the
* <code>ServletContext</code>. Any exception throws is logged.
*/
public void init(FilterConfig config) throws ServletException
{
LOG.info(ServletMessages.filterInit());
try
{
_registry = constructRegistry(config);
config.getServletContext().setAttribute(CONTEXT_KEY, _registry);
LOG.info(ServletMessages.storedRegistry(_registry, CONTEXT_KEY));
}
catch (Exception ex)
{
LOG.error(ex.getMessage(), ex);
}
}
/**
* Invoked from {@link #init(FilterConfig)} to actually
* construct the Registry. Subclasses may override if
* they have specific initialization needs, or have nonstandard
* rules for finding HiveMind module deployment descriptors.
*/
protected Registry constructRegistry(FilterConfig config)
{
ClassResolver resolver = new DefaultClassResolver();
RegistryBuilder builder = new RegistryBuilder();
builder.processModules(resolver);
return builder.constructRegistry(getRegistryLocale());
}
/**
* Returns the default Locale. Subclasses may override to select a particular
* locale for the Registry.
*/
protected Locale getRegistryLocale()
{
return Locale.getDefault();
}
/**
* Passes the request to the filter chain, but then invokes
* {@link ThreadEventNotifier#fireThreadCleanup()} (from a finally block).
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
try
{
request.setAttribute(REQUEST_KEY, _registry);
chain.doFilter(request, response);
}
finally
{
cleanupThread();
}
}
/**
* Cleanup the thread, ignoring any exceptions that may be thrown.
*/
private void cleanupThread()
{
try
{
_registry.cleanupThread();
}
catch (Exception ex)
{
LOG.error(ServletMessages.filterCleanupError(ex), ex);
}
}
/**
* Invokes {@link Registry#shutdown()}.
*/
public void destroy()
{
if (_registry != null)
_registry.shutdown();
}
/**
* Returns the {@link Registry} that was stored as a request attribute
* in {@link #doFilter(ServletRequest, ServletResponse, FilterChain)}.
*/
public static Registry getRegistry(HttpServletRequest request)
{
return (Registry) request.getAttribute(REQUEST_KEY);
}
}