Coverage Report - no.sesat.search.http.servlet.FactoryReloads
 
Classes in this File Line Coverage Branch Coverage Complexity
FactoryReloads
0%
0/23
0%
0/21
0
FactoryReloads$1
0%
0/1
N/A
0
FactoryReloads$ReloadArg
0%
0/7
N/A
0
 
 1  
 /* Copyright (2006-2008) Schibsted Søk AS
 2  
  * This file is part of SESAT.
 3  
  *
 4  
  *   SESAT is free software: you can redistribute it and/or modify
 5  
  *   it under the terms of the GNU Affero General Public License as published by
 6  
  *   the Free Software Foundation, either version 3 of the License, or
 7  
  *   (at your option) any later version.
 8  
  *
 9  
  *   SESAT is distributed in the hope that it will be useful,
 10  
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  
  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  
  *   GNU Affero General Public License for more details.
 13  
  *
 14  
  *   You should have received a copy of the GNU Affero General Public License
 15  
  *   along with SESAT.  If not, see <http://www.gnu.org/licenses/>.
 16  
 
 17  
  */
 18  
 /*
 19  
  * FactoryReloads.java
 20  
  *
 21  
  * Created on 5 May 2006, 07:58
 22  
  *
 23  
  */
 24  
 
 25  
 package no.sesat.search.http.servlet;
 26  
 
 27  
 import java.util.Locale;
 28  
 import no.sesat.commons.ioc.ContextWrapper;
 29  
 import no.sesat.search.mode.SearchModeFactory;
 30  
 import no.sesat.search.site.config.SiteConfiguration;
 31  
 import no.sesat.search.query.analyser.AnalysisRuleFactory;
 32  
 import no.sesat.search.site.Site;
 33  
 import no.sesat.search.site.SiteContext;
 34  
 import no.sesat.search.site.SiteKeyedFactory;
 35  
 import no.sesat.search.view.velocity.VelocityEngineFactory;
 36  
 import no.sesat.search.view.SearchTabFactory;
 37  
 import org.apache.log4j.Logger;
 38  
 
 39  
 /** Utility class to remove factory instances for a given Site and its locale derivatives.
 40  
  * The factory class to clean instances from is indicated by the value of ReloadArg.
 41  
  * Also performs a System.gc() to clean out WeakReference caches.
 42  
  *
 43  
  *
 44  
  * @version $Id$
 45  
  */
 46  
 public final class FactoryReloads {
 47  
 
 48  
 
 49  0
     public enum ReloadArg{
 50  0
         ALL,
 51  0
         SITE_CONFIGURATION,
 52  0
         SEARCH_TAB_FACTORY,
 53  0
         SEARCH_MODE_FACTORY,
 54  0
         ANALYSIS_RULES_FACTORY,
 55  0
         VELOCITY_ENGINE_FACTORY
 56  
     }
 57  
 
 58  
     // Constants -----------------------------------------------------
 59  
 
 60  0
     private static final Logger LOG = Logger.getLogger(FactoryReloads.class);
 61  
 
 62  
     private static final String WARN_CLEANED_1 = " on cleaning ";
 63  
     private static final String WARN_CLEANED_2 = " (against all locales) for ";
 64  
 
 65  
     // Attributes ----------------------------------------------------
 66  
 
 67  
     // Static --------------------------------------------------------
 68  
 
 69  
     /** Remove factory instances for a given Site and its locale derivatives.
 70  
      * The factory class to clean instances from is indicated by the value of ReloadArg.
 71  
      * Also performs a System.gc() to clean out WeakReference caches.
 72  
      **/
 73  
     @SuppressWarnings("fallthrough")
 74  
     public static void performReloads(
 75  
             final SiteContext genericCxt,
 76  
             final ReloadArg reload){
 77  
 
 78  0
         final Site site = genericCxt.getSite();
 79  
 
 80  0
         switch(reload){
 81  
             case ALL:
 82  
             case SITE_CONFIGURATION:
 83  
 
 84  0
                 performReload(site, SiteConfiguration.instanceOf(
 85  
                         ContextWrapper.wrap(SiteConfiguration.Context.class, genericCxt)));
 86  0
                 if(ReloadArg.ALL != reload){ break;}
 87  
 
 88  
             case SEARCH_TAB_FACTORY:
 89  
 
 90  0
                 performReload(site, SearchTabFactory.instanceOf(
 91  
                         ContextWrapper.wrap(SearchTabFactory.Context.class, genericCxt)));
 92  0
                 if(ReloadArg.ALL != reload){ break;}
 93  
 
 94  
             case SEARCH_MODE_FACTORY:
 95  
 
 96  0
                 performReload(site, SearchModeFactory.instanceOf(
 97  
                         ContextWrapper.wrap(SearchModeFactory.Context.class, genericCxt)));
 98  0
                 if( ReloadArg.ALL != reload){ break;}
 99  
 
 100  
             case ANALYSIS_RULES_FACTORY:
 101  
 
 102  0
                 performReload(site, AnalysisRuleFactory.instanceOf(
 103  
                         ContextWrapper.wrap(AnalysisRuleFactory.Context.class, genericCxt)));
 104  
 
 105  
             case VELOCITY_ENGINE_FACTORY:
 106  
 
 107  0
                 performReload(site, VelocityEngineFactory.instanceOf(
 108  
                         ContextWrapper.wrap(VelocityEngineFactory.Context.class, genericCxt)));
 109  0
                 if(ReloadArg.ALL != reload){ break;}
 110  
 
 111  
         }
 112  
 
 113  
         // clean out WeakReference caches
 114  0
         System.gc();
 115  0
     }
 116  
 
 117  
     private static void performReload(
 118  
             final Site site,
 119  
             final SiteKeyedFactory factory){
 120  
 
 121  0
         LOG.warn(removeAllLocalesFromSiteKeyedFactory(site, factory)
 122  
                 + WARN_CLEANED_1 + factory.getClass().getSimpleName() + WARN_CLEANED_2 + site);
 123  0
     }
 124  
 
 125  
     private static int removeAllLocalesFromSiteKeyedFactory(
 126  
             final Site site,
 127  
             final SiteKeyedFactory factory){
 128  
 
 129  0
         int cleaned = 0;
 130  0
         for(Locale l : Locale.getAvailableLocales()){
 131  0
             final Site s = Site.valueOf(null, site.getName(), l);
 132  0
             if(null != s && factory.remove(s)){
 133  0
                 ++cleaned;
 134  
             }
 135  
         }
 136  0
         return cleaned;
 137  
     }
 138  
 
 139  
     // Constructors --------------------------------------------------
 140  
 
 141  0
     private FactoryReloads(){}
 142  
 
 143  
 
 144  
     // Public --------------------------------------------------------
 145  
 
 146  
     // Z implementation ----------------------------------------------
 147  
 
 148  
     // Y overrides ---------------------------------------------------
 149  
 
 150  
     // Package protected ---------------------------------------------
 151  
 
 152  
     // Protected -----------------------------------------------------
 153  
 
 154  
     // Private -------------------------------------------------------
 155  
 
 156  
     // Inner classes -------------------------------------------------
 157  
 
 158  
 }