Clover coverage report - Code Coverage for hivemind release 1.0-rc-2
Coverage timestamp: Sat Sep 11 2004 09:09:48 EDT
file stats: LOC: 220   Methods: 11
NCLOC: 108   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   
      * Request attribute key that stores the Registry.
 50   
      */
 51   
 
 52   
     static final String REQUEST_KEY = "org.apache.hivemind.RequestRegistry";
 53   
 
 54   
     static final String REBUILD_REQUEST_KEY = "org.apache.hivemind.RebuildRegistry";
 55   
 
 56   
     private FilterConfig _filterConfig;
 57   
     private Registry _registry;
 58   
 
 59   
     /**
 60   
      * Package private method used for testing.
 61   
      */
 62  2
     Registry getRegistry()
 63   
     {
 64  2
         return _registry;
 65   
     }
 66   
 
 67   
     /**
 68   
      * Constructs a {@link Registry} and stores it into the
 69   
      * <code>ServletContext</code>. Any exception throws is logged.
 70   
      */
 71  3
     public void init(FilterConfig config) throws ServletException
 72   
     {
 73  3
         _filterConfig = config;
 74   
 
 75  3
         initializeRegistry();
 76   
 
 77   
     }
 78   
 
 79  4
     private void initializeRegistry()
 80   
     {
 81  4
         long startTime = System.currentTimeMillis();
 82   
 
 83  4
         LOG.info(ServletMessages.filterInit());
 84   
 
 85  4
         try
 86   
         {
 87  4
             _registry = constructRegistry(_filterConfig);
 88   
 
 89  3
             LOG.info(
 90   
                 ServletMessages.constructedRegistry(
 91   
                     _registry,
 92   
                     System.currentTimeMillis() - startTime));
 93   
         }
 94   
         catch (Exception ex)
 95   
         {
 96  1
             LOG.error(ex.getMessage(), ex);
 97   
         }
 98   
     }
 99   
 
 100   
     /**
 101   
      * Invoked from {@link #init(FilterConfig)} to actually
 102   
      * construct the Registry.  Subclasses may override if
 103   
      * they have specific initialization needs, or have nonstandard
 104   
      * rules for finding HiveMind module deployment descriptors.
 105   
      */
 106  3
     protected Registry constructRegistry(FilterConfig config)
 107   
     {
 108  3
         ClassResolver resolver = new DefaultClassResolver();
 109  3
         RegistryBuilder builder = new RegistryBuilder();
 110   
 
 111  3
         builder.processModules(resolver);
 112   
 
 113  3
         return builder.constructRegistry(getRegistryLocale());
 114   
     }
 115   
 
 116   
     /**
 117   
      * Returns the default Locale.  Subclasses may override to select a particular
 118   
      * locale for the Registry.
 119   
      */
 120  3
     protected Locale getRegistryLocale()
 121   
     {
 122  3
         return Locale.getDefault();
 123   
     }
 124   
 
 125   
     /**
 126   
      * Passes the request to the filter chain, but then invokes
 127   
      * {@link ThreadEventNotifier#fireThreadCleanup()} (from a finally block).
 128   
      */
 129  4
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
 130   
         throws IOException, ServletException
 131   
     {
 132  4
         try
 133   
         {
 134  4
             request.setAttribute(REQUEST_KEY, _registry);
 135   
 
 136  4
             chain.doFilter(request, response);
 137   
         }
 138   
         finally
 139   
         {
 140  4
             cleanupThread();
 141   
             
 142  4
             checkRegistryRebuild(request);
 143   
         }
 144   
     }
 145   
 
 146  4
     private synchronized void checkRegistryRebuild(ServletRequest request)
 147   
     {
 148  4
         if (request.getAttribute(REBUILD_REQUEST_KEY) == null)
 149  3
             return;
 150   
 
 151   
         // This is so not threadsafe, but you don't do this very often.
 152   
         // This method is synchronized, but other threads may be actively using
 153   
         // the Registry we're shutting down.
 154   
         
 155   
         // What we really need is some good concurrence support to track the number
 156   
         // of threads that may be using the old registry, set a write lock, and wait
 157   
         // for that number to drop to one (this thread).  Instead, we'll just swap in the
 158   
         // new Registry and hope for the best.
 159   
 
 160  1
         Registry oldRegistry = _registry;
 161   
 
 162   
         // Replace the old Registry with a new one.  All other threads, but this
 163   
         // one, will begin using the new Registry. Hopefully, we didn't get
 164   
         // rebuild requests on multiple threads.
 165   
 
 166  1
         initializeRegistry();
 167   
 
 168   
         // Shutdown the old Registry. Perhaps we should sleep for a moment, first,
 169   
         // to help ensure that other threads have "cleared out". If not, we'll see some
 170   
         // instability at the instant we shutdown (i.e., all the proxies will get disabled).
 171   
 
 172  1
         oldRegistry.shutdown();
 173   
     }
 174   
 
 175   
     /**
 176   
      * Cleanup the thread, ignoring any exceptions that may be thrown.
 177   
      */
 178  4
     private void cleanupThread()
 179   
     {
 180  4
         try
 181   
         {
 182  4
             _registry.cleanupThread();
 183   
         }
 184   
         catch (Exception ex)
 185   
         {
 186  2
             LOG.error(ServletMessages.filterCleanupError(ex), ex);
 187   
         }
 188   
     }
 189   
 
 190   
     /**
 191   
      * Invokes {@link Registry#shutdown()}.
 192   
      */
 193  3
     public void destroy()
 194   
     {
 195  3
         if (_registry != null)
 196  1
             _registry.shutdown();
 197   
 
 198  3
         _filterConfig = null;
 199   
     }
 200   
 
 201   
     /**
 202   
      * Returns the {@link Registry} that was stored as a request attribute
 203   
      * inside method {@link #doFilter(ServletRequest, ServletResponse, FilterChain)}.
 204   
      */
 205  1
     public static Registry getRegistry(HttpServletRequest request)
 206   
     {
 207  1
         return (Registry) request.getAttribute(REQUEST_KEY);
 208   
     }
 209   
 
 210   
     /**
 211   
      * Sets a flag in the request that will cause the current Registry to be shutdown
 212   
      * and replaced with a new Registry (at the end of the current request).
 213   
      */
 214  1
     public static void rebuildRegistry(HttpServletRequest request)
 215   
     {
 216  1
         request.setAttribute(REBUILD_REQUEST_KEY, Boolean.TRUE);
 217   
     }
 218   
 
 219   
 }
 220