Clover coverage report - Code Coverage for hivemind release 1.0-beta-2
Coverage timestamp: Sun Aug 1 2004 14:03:45 EDT
file stats: LOC: 162   Methods: 7
NCLOC: 81   Classes: 1
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%
coverage
 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
             _registry = constructRegistry(config);
 72   
 
 73  2
             config.getServletContext().setAttribute(CONTEXT_KEY, _registry);
 74   
 
 75  1
             LOG.info(ServletMessages.storedRegistry(_registry, CONTEXT_KEY));
 76   
         }
 77   
         catch (Exception ex)
 78   
         {
 79  1
             LOG.error(ex.getMessage(), ex);
 80   
         }
 81   
     }
 82   
 
 83   
     /**
 84   
      * Invoked from {@link #init(FilterConfig)} to actually
 85   
      * construct the Registry.  Subclasses may override if
 86   
      * they have specific initialization needs, or have nonstandard
 87   
      * rules for finding HiveMind module deployment descriptors.
 88   
      */
 89  2
     protected Registry constructRegistry(FilterConfig config)
 90   
     {
 91  2
         ClassResolver resolver = new DefaultClassResolver();
 92  2
         RegistryBuilder builder = new RegistryBuilder();
 93   
 
 94  2
         builder.processModules(resolver);
 95   
 
 96  2
         return builder.constructRegistry(getRegistryLocale());
 97   
     }
 98   
 
 99   
     /**
 100   
      * Returns the default Locale.  Subclasses may override to select a particular
 101   
      * locale for the Registry.
 102   
      */
 103  2
     protected Locale getRegistryLocale()
 104   
     {
 105  2
         return Locale.getDefault();
 106   
     }
 107   
 
 108   
     /**
 109   
      * Passes the request to the filter chain, but then invokes
 110   
      * {@link ThreadEventNotifier#fireThreadCleanup()} (from a finally block).
 111   
      */
 112  3
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
 113   
         throws IOException, ServletException
 114   
     {
 115  3
         try
 116   
         {
 117  3
             request.setAttribute(REQUEST_KEY, _registry);
 118   
 
 119  3
             chain.doFilter(request, response);
 120   
         }
 121   
         finally
 122   
         {
 123  3
             cleanupThread();
 124   
         }
 125   
 
 126   
     }
 127   
 
 128   
     /**
 129   
      * Cleanup the thread, ignoring any exceptions that may be thrown.
 130   
      */
 131  3
     private void cleanupThread()
 132   
     {
 133  3
         try
 134   
         {
 135  3
             _registry.cleanupThread();
 136   
         }
 137   
         catch (Exception ex)
 138   
         {
 139  1
             LOG.error(ServletMessages.filterCleanupError(ex), ex);
 140   
         }
 141   
     }
 142   
 
 143   
     /**
 144   
      * Invokes {@link Registry#shutdown()}.
 145   
      */
 146  3
     public void destroy()
 147   
     {
 148  3
         if (_registry != null)
 149  2
             _registry.shutdown();
 150   
     }
 151   
 
 152   
     /**
 153   
      * Returns the {@link Registry} that was stored as a request attribute
 154   
      * in {@link #doFilter(ServletRequest, ServletResponse, FilterChain)}.
 155   
      */
 156  1
     public static Registry getRegistry(HttpServletRequest request)
 157   
     {
 158  1
         return (Registry) request.getAttribute(REQUEST_KEY);
 159   
     }
 160   
 
 161   
 }
 162