Clover coverage report - Code Coverage for hivemind release 1.1-alpha-2
Coverage timestamp: Wed Feb 23 2005 09:59:04 EST
file stats: LOC: 252   Methods: 12
NCLOC: 123   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, 2005 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.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.internal.ser.ServiceSerializationHelper;
 38   
 import org.apache.hivemind.util.ContextResource;
 39   
 
 40   
 /**
 41   
  * Servlet filter that constructs the Registry at startup. It ensures that each request is properly
 42   
  * terminated with a call to
 43   
  * {@link org.apache.hivemind.service.ThreadEventNotifier#fireThreadCleanup()}. It also makes the
 44   
  * Registry available during the request by storing it as a request attribute.
 45   
  * 
 46   
  * @author Howard Lewis Ship
 47   
  */
 48   
 public class HiveMindFilter implements Filter
 49   
 {
 50   
     private static final Log LOG = LogFactory.getLog(HiveMindFilter.class);
 51   
 
 52   
     /**
 53   
      * Request attribute key that stores the Registry.
 54   
      */
 55   
 
 56   
     static final String REQUEST_KEY = "org.apache.hivemind.RequestRegistry";
 57   
 
 58   
     static final String REBUILD_REQUEST_KEY = "org.apache.hivemind.RebuildRegistry";
 59   
 
 60   
     /** @since 1.1 */
 61   
     static final String HIVE_MODULE_XML = "/WEB-INF/hivemodule.xml";
 62   
 
 63   
     private FilterConfig _filterConfig;
 64   
 
 65   
     private Registry _registry;
 66   
 
 67   
     /**
 68   
      * Constructs a {@link Registry}and stores it into the <code>ServletContext</code>. Any
 69   
      * exception throws is logged.
 70   
      */
 71  4
     public void init(FilterConfig config) throws ServletException
 72   
     {
 73  4
         _filterConfig = config;
 74   
 
 75  4
         initializeRegistry();
 76   
 
 77   
     }
 78   
 
 79  5
     private void initializeRegistry()
 80   
     {
 81  5
         long startTime = System.currentTimeMillis();
 82   
 
 83  5
         LOG.info(ServletMessages.filterInit());
 84   
 
 85  5
         try
 86   
         {
 87  5
             _registry = constructRegistry(_filterConfig);
 88   
 
 89  4
             LOG.info(ServletMessages.constructedRegistry(_registry, System.currentTimeMillis()
 90   
                     - startTime));
 91   
         }
 92   
         catch (Exception ex)
 93   
         {
 94  1
             LOG.error(ex.getMessage(), ex);
 95   
         }
 96   
     }
 97   
 
 98   
     /**
 99   
      * Invoked from {@link #init(FilterConfig)}to actually construct the Registry. Subclasses may
 100   
      * override if they have specific initialization needs, or have nonstandard rules for finding
 101   
      * HiveMind module deployment descriptors.
 102   
      */
 103  4
     protected Registry constructRegistry(FilterConfig config)
 104   
     {
 105  4
         RegistryBuilder builder = new RegistryBuilder();
 106   
 
 107  4
         ClassResolver resolver = new DefaultClassResolver();
 108   
 
 109  4
         builder.addModuleDescriptorProvider(getModuleDescriptorProvider(resolver));
 110   
 
 111  4
         addWebInfDescriptor(config.getServletContext(), resolver, builder);
 112   
 
 113   
         // This avoids an error message when replacing the SSS (really, the RegistryInfrastructure).
 114   
         // HiveMindFilter's reload-the-registry functionality is an exception to the rule
 115   
         // that there should just be one Registry per JVM.
 116   
 
 117  4
         ServiceSerializationHelper.setServiceSerializationSupport(null);
 118   
 
 119  4
         return builder.constructRegistry(getRegistryLocale());
 120   
     }
 121   
 
 122   
     /**
 123   
      * Invoked from {@link #constructRegistry(FilterConfig)}&nbsp; to add WEB-INF/hivemodule.xml to
 124   
      * the registry, if it exists.
 125   
      * 
 126   
      * @since 1.1
 127   
      */
 128   
 
 129  4
     protected void addWebInfDescriptor(ServletContext context, ClassResolver resolver,
 130   
             RegistryBuilder builder)
 131   
     {
 132  4
         ContextResource r = new ContextResource(context, HIVE_MODULE_XML);
 133   
 
 134  4
         if (r.getResourceURL() != null)
 135   
         {
 136  1
             ModuleDescriptorProvider provider = new XmlModuleDescriptorProvider(resolver, r);
 137   
 
 138  1
             builder.addModuleDescriptorProvider(provider);
 139   
         }
 140   
     }
 141   
 
 142   
     /**
 143   
      * Returns the default Locale. Subclasses may override to select a particular locale for the
 144   
      * Registry.
 145   
      */
 146  4
     protected Locale getRegistryLocale()
 147   
     {
 148  4
         return Locale.getDefault();
 149   
     }
 150   
 
 151   
     /**
 152   
      * Returns the {@link ModuleDescriptorProvider}to be used to construct the Registry. This
 153   
      * implementation returns the default {@link XmlModuleDescriptorProvider}. May be overridden by
 154   
      * subclasses.
 155   
      * 
 156   
      * @since 1.1
 157   
      */
 158  4
     protected ModuleDescriptorProvider getModuleDescriptorProvider(ClassResolver resolver)
 159   
     {
 160  4
         return new XmlModuleDescriptorProvider(resolver);
 161   
     }
 162   
 
 163   
     /**
 164   
      * Passes the request to the filter chain, but then invokes {@link Registry#cleanupThread()}
 165   
      * &nbsp; (from a finally block).
 166   
      */
 167  4
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
 168   
             throws IOException, ServletException
 169   
     {
 170  4
         try
 171   
         {
 172  4
             request.setAttribute(REQUEST_KEY, _registry);
 173   
 
 174  4
             chain.doFilter(request, response);
 175   
         }
 176   
         finally
 177   
         {
 178  4
             cleanupThread();
 179   
 
 180  4
             checkRegistryRebuild(request);
 181   
         }
 182   
     }
 183   
 
 184  4
     private synchronized void checkRegistryRebuild(ServletRequest request)
 185   
     {
 186  4
         if (request.getAttribute(REBUILD_REQUEST_KEY) == null)
 187  3
             return;
 188   
 
 189  1
         Registry oldRegistry = _registry;
 190   
 
 191   
         // Replace the old Registry with a new one. All other threads, but this
 192   
         // one, will begin using the new Registry. Hopefully, we didn't get
 193   
         // rebuild requests on multiple threads.
 194   
 
 195  1
         initializeRegistry();
 196   
 
 197   
         // Shutdown the old Registry. Perhaps we should sleep for a moment, first,
 198   
         // to help ensure that other threads have "cleared out". If not, we'll see some
 199   
         // instability at the instant we shutdown (i.e., all the proxies will get disabled).
 200   
         // Alternately, we should create a WeakReference based monitor that shuts down the
 201   
         // old registry when it is no longer used by any other threads. For the moment,
 202   
         // this functionality is limited to development-time only (not production), so it isn't
 203   
         // urgent.
 204   
 
 205  1
         oldRegistry.shutdown();
 206   
     }
 207   
 
 208   
     /**
 209   
      * Cleanup the thread, ignoring any exceptions that may be thrown.
 210   
      */
 211  4
     private void cleanupThread()
 212   
     {
 213  4
         try
 214   
         {
 215  4
             _registry.cleanupThread();
 216   
         }
 217   
         catch (Exception ex)
 218   
         {
 219  2
             LOG.error(ServletMessages.filterCleanupError(ex), ex);
 220   
         }
 221   
     }
 222   
 
 223   
     /**
 224   
      * Invokes {@link Registry#shutdown()}.
 225   
      */
 226  3
     public void destroy()
 227   
     {
 228  3
         if (_registry != null)
 229  1
             _registry.shutdown();
 230   
 
 231  3
         _filterConfig = null;
 232   
     }
 233   
 
 234   
     /**
 235   
      * Returns the {@link Registry}that was stored as a request attribute inside method
 236   
      * {@link #doFilter(ServletRequest, ServletResponse, FilterChain)}.
 237   
      */
 238  1
     public static Registry getRegistry(HttpServletRequest request)
 239   
     {
 240  1
         return (Registry) request.getAttribute(REQUEST_KEY);
 241   
     }
 242   
 
 243   
     /**
 244   
      * Sets a flag in the request that will cause the current Registry to be shutdown and replaced
 245   
      * with a new Registry (at the end of the current request).
 246   
      */
 247  1
     public static void rebuildRegistry(HttpServletRequest request)
 248   
     {
 249  1
         request.setAttribute(REBUILD_REQUEST_KEY, Boolean.TRUE);
 250   
     }
 251   
 
 252   
 }