View Javadoc

1   /* Copyright (2006-2007) 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.schibstedsok.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.query.token.RegExpEvaluatorFactory;
33  import no.sesat.search.site.Site;
34  import no.sesat.search.site.SiteContext;
35  import no.sesat.search.site.SiteKeyedFactory;
36  import no.sesat.search.view.velocity.VelocityEngineFactory;
37  import no.sesat.search.view.SearchTabFactory;
38  import org.apache.log4j.Logger;
39  
40  /** Utility class to remove factory instances for a given Site and its locale derivatives.
41   * The factory class to clean instances from is indicated by the value of ReloadArg.
42   * Also performs a System.gc() to clean out WeakReference caches.
43   *
44   *
45   * @version $Id$
46   */
47  public final class FactoryReloads {
48  
49  
50      public enum ReloadArg{
51          ALL,
52          SITE_CONFIGURATION,
53          SEARCH_TAB_FACTORY,
54          SEARCH_MODE_FACTORY,
55          ANALYSIS_RULES_FACTORY,
56          REG_EXP_EVALUATOR_FACTORY,
57          VELOCITY_ENGINE_FACTORY
58      }
59  
60      // Constants -----------------------------------------------------
61  
62      private static final Logger LOG = Logger.getLogger(FactoryReloads.class);
63  
64      private static final String WARN_CLEANED_1 = " on cleaning ";
65      private static final String WARN_CLEANED_2 = " (against all locales) for ";
66  
67      // Attributes ----------------------------------------------------
68  
69      // Static --------------------------------------------------------
70  
71      /** Remove factory instances for a given Site and its locale derivatives.
72       * The factory class to clean instances from is indicated by the value of ReloadArg.
73       * Also performs a System.gc() to clean out WeakReference caches.
74       **/
75      @SuppressWarnings("fallthrough")
76      public static void performReloads(
77              final SiteContext genericCxt,
78              final ReloadArg reload){
79  
80          final Site site = genericCxt.getSite();
81  
82          switch(reload){
83              case ALL:
84              case SITE_CONFIGURATION:
85  
86                  performReload(site, SiteConfiguration.instanceOf(
87                          ContextWrapper.wrap(SiteConfiguration.Context.class, genericCxt)));
88                  if(ReloadArg.ALL != reload){ break;}
89  
90              case SEARCH_TAB_FACTORY:
91  
92                  performReload(site, SearchTabFactory.instanceOf(
93                          ContextWrapper.wrap(SearchTabFactory.Context.class, genericCxt)));
94                  if(ReloadArg.ALL != reload){ break;}
95  
96              case SEARCH_MODE_FACTORY:
97  
98                  performReload(site, SearchModeFactory.instanceOf(
99                          ContextWrapper.wrap(SearchModeFactory.Context.class, genericCxt)));
100                 if( ReloadArg.ALL != reload){ break;}
101 
102             case ANALYSIS_RULES_FACTORY:
103 
104                 performReload(site, AnalysisRuleFactory.instanceOf(
105                         ContextWrapper.wrap(AnalysisRuleFactory.Context.class, genericCxt)));
106 
107             case REG_EXP_EVALUATOR_FACTORY:
108 
109                 performReload(site, RegExpEvaluatorFactory.instanceOf(
110                         ContextWrapper.wrap(RegExpEvaluatorFactory.Context.class, genericCxt)));
111                 if(ReloadArg.ALL != reload){ break;}
112 
113             case VELOCITY_ENGINE_FACTORY:
114 
115                 performReload(site, VelocityEngineFactory.instanceOf(
116                         ContextWrapper.wrap(VelocityEngineFactory.Context.class, genericCxt)));
117                 if(ReloadArg.ALL != reload){ break;}
118 
119         }
120 
121         // clean out WeakReference caches
122         System.gc();
123     }
124 
125     private static void performReload(
126             final Site site,
127             final SiteKeyedFactory factory){
128 
129         LOG.warn(removeAllLocalesFromSiteKeyedFactory(site, factory)
130                 + WARN_CLEANED_1 + factory.getClass().getSimpleName() + WARN_CLEANED_2 + site);
131     }
132 
133     private static int removeAllLocalesFromSiteKeyedFactory(
134             final Site site,
135             final SiteKeyedFactory factory){
136 
137         int cleaned = 0;
138         for(Locale l : Locale.getAvailableLocales()){
139             final Site s = Site.valueOf(null, site.getName(), l);
140             if(null != s && factory.remove(site)){
141                 ++cleaned;
142             }
143         }
144         return cleaned;
145     }
146 
147     // Constructors --------------------------------------------------
148 
149     private FactoryReloads(){}
150 
151 
152     // Public --------------------------------------------------------
153 
154     // Z implementation ----------------------------------------------
155 
156     // Y overrides ---------------------------------------------------
157 
158     // Package protected ---------------------------------------------
159 
160     // Protected -----------------------------------------------------
161 
162     // Private -------------------------------------------------------
163 
164     // Inner classes -------------------------------------------------
165 
166 }