Author: mickw
Date: 2006-05-09 12:25:51 +0200 (Tue, 09 May 2006)
New Revision: 2878

Modified:
   
trunk/src/java/no/schibstedsok/front/searchportal/filters/SiteLocatorFilter.java
   trunk/src/java/no/schibstedsok/front/searchportal/util/SearchConstants.java
   trunk/src/java/no/schibstedsok/front/searchportal/view/i18n/TextMessages.java
Log:
Only put into the request a Site object that contains a supported locale.
 Should seriously limit the number of 'live' Site keys in the JVM, and memory 
consumption by the factories.


Modified: 
trunk/src/java/no/schibstedsok/front/searchportal/filters/SiteLocatorFilter.java
===================================================================
--- 
trunk/src/java/no/schibstedsok/front/searchportal/filters/SiteLocatorFilter.java
    2006-05-09 09:38:20 UTC (rev 2877)
+++ 
trunk/src/java/no/schibstedsok/front/searchportal/filters/SiteLocatorFilter.java
    2006-05-09 10:25:51 UTC (rev 2878)
@@ -46,12 +46,14 @@
     private static final String ERR_NOT_FOUND = "Failed to find resource ";
     private static final String ERR_UNCAUGHT_RUNTIME_EXCEPTION
             = "Following runtime exception was let loose in tomcat\n";
+
+    private static final String INFO_USING_DEFAULT_LOCALE = " is falling back 
to the default locale ";    
     private static final String DEBUG_REQUESTED_VHOST = "Virtual host is ";
     private static final String DEBUG_REDIRECTING_TO = " redirect to ";
 
     private static final String HTTP = "http://";;
-
     private static final String PUBLISH_DIR = "/img/";
+    private static final String SITE_LOCALE_SUPPORTED = 
"site.locale.supported";
 
     /** Changes to this list must also change the ProxyPass|ProxyPassReverse 
configuration in httpd.conf **/
     private static final Collection<String> EXTERNAL_DIRS =
@@ -225,7 +227,7 @@
     }
 
     /** The method to obtain the correct Site from the request.
-     *
+     * It only returns a site with a locale supported by that site.
      **/
     public static Site getSite(final ServletRequest servletRequest) {
         // find the current site. Since we are behind a ajp13 connection 
request.getServerName() won't work!
@@ -237,17 +239,40 @@
             // falls back to this when not behind Apache. (Development 
machine).
             : servletRequest.getServerName() + ":" + 
servletRequest.getServerPort();
 
-        // Just because many norwegians have their computers installed in 
english mode
-        //  we can't presume they want their webpages in english.
-        //  Therefore we must always initially replace english locales with 
norwegian.
         final Locale locale = servletRequest.getLocale();
-//        final Locale locale = "en".equals(requestLocale.getLanguage())
-//                ? Locale.getDefault()
-//                : requestLocale;
 
         LOG.trace(DEBUG_REQUESTED_VHOST + vhost);
 
-        return Site.valueOf(vhost, locale);
+        final Site result = Site.valueOf(vhost, locale);
+        
+        final String[] locales = 
SiteConfiguration.valueOf(result).getProperty(SITE_LOCALE_SUPPORTED).split(",");
+        for(String l : locales ){
+            if( locale.toString().equals(l) ){
+                return result;
+            }
+        }
+        
+        final String[] prefLocale = SiteConfiguration.valueOf(result)
+                .getProperty(SearchConstants.SITE_LOCALE_DEFAULT)
+                .split("_");
+        
+        switch(prefLocale.length){
+            case 3:
+                LOG.info(result+INFO_USING_DEFAULT_LOCALE + prefLocale[0] 
+                        + '_' + prefLocale[1] + '_' + prefLocale[2]);
+                return Site.valueOf(vhost, new Locale(prefLocale[0], 
prefLocale[1], prefLocale[2]));
+            case 2:
+                LOG.info(result+INFO_USING_DEFAULT_LOCALE 
+                        + prefLocale[0] + '_' + prefLocale[1]);
+                return Site.valueOf(vhost, new Locale(prefLocale[0], 
prefLocale[1]));
+            case 1:
+            default:
+                LOG.info(result+INFO_USING_DEFAULT_LOCALE 
+                        + prefLocale[0]);
+                return Site.valueOf(vhost, new Locale(prefLocale[0]));
+        }
+        
+        
     }
 
 }

Modified: 
trunk/src/java/no/schibstedsok/front/searchportal/util/SearchConstants.java
===================================================================
--- trunk/src/java/no/schibstedsok/front/searchportal/util/SearchConstants.java 
2006-05-09 09:38:20 UTC (rev 2877)
+++ trunk/src/java/no/schibstedsok/front/searchportal/util/SearchConstants.java 
2006-05-09 10:25:51 UTC (rev 2878)
@@ -13,7 +13,8 @@
  */
 public final class SearchConstants {
 
-    //Static properties file variables
+    public static final String SITE_LOCALE_DEFAULT = "site.locale.default";
+    
     public static final String FAST_PROPERTYFILE = "fast.properties";          
      //the properties file for configuration of FAST search and templates
     public static final String SENSIS_PROPERTYFILE = "sensis.properties";      
      //the properties file for configuration of FAST search and templates
     public static final String REGEXP_EVALUATOR_XMLFILE = 
"RegularExpressionEvaluators.xml";    //the xml file for regexp patterns

Modified: 
trunk/src/java/no/schibstedsok/front/searchportal/view/i18n/TextMessages.java
===================================================================
--- 
trunk/src/java/no/schibstedsok/front/searchportal/view/i18n/TextMessages.java   
    2006-05-09 09:38:20 UTC (rev 2877)
+++ 
trunk/src/java/no/schibstedsok/front/searchportal/view/i18n/TextMessages.java   
    2006-05-09 10:25:51 UTC (rev 2878)
@@ -15,6 +15,7 @@
 
 import no.schibstedsok.front.searchportal.site.Site;
 import no.schibstedsok.front.searchportal.site.SiteContext;
+import no.schibstedsok.front.searchportal.util.SearchConstants;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -39,7 +40,7 @@
     private static final Map<Site,TextMessages> INSTANCES = new 
HashMap<Site,TextMessages>();
     private static final ReentrantReadWriteLock INSTANCES_LOCK = new 
ReentrantReadWriteLock();
     
-    private static final String SITE_LOCALE_DEFAULT = "site.locale.default";
+    
 
     private static final String DEBUG_LOADING_WITH_LOCALE = "Looking for 
"+MESSAGE_RESOURCE+"_";
     private static final String INFO_USING_DEFAULT_LOCALE = " is falling back 
to the default locale ";
@@ -92,7 +93,7 @@
 
         // import messages from site's preferred locale [will not override 
already loaded messages]
         final String[] prefLocale = 
SiteConfiguration.valueOf(ContextWrapper.wrap(SiteConfiguration.Context.class, 
cxt))
-                .getProperty(SITE_LOCALE_DEFAULT).split("_");
+                .getProperty(SearchConstants.SITE_LOCALE_DEFAULT).split("_");
         
         
         switch(prefLocale.length){
@@ -143,26 +144,30 @@
     }
 
     public String getMessage(final String key) {
-        return getMessage(key, new Object[]{});
+        return getMessageImpl(key);
     }
 
     public String getMessage(final String key, final Object arg0) {
-        return getMessage(key, new Object[]{arg0});
+        return getMessageImpl(key, arg0);
     }
 
     public String getMessage(final String key, final Object arg0, final Object 
arg1) {
-        return getMessage(key, new Object[]{arg0, arg1});
+        return getMessageImpl(key, arg0, arg1);
     }
 
     public String getMessage(final String key, final Object arg0, final Object 
arg1, final Object arg2) {
-        return getMessage(key, new Object[]{arg0, arg1, arg2});
+        return getMessageImpl(key, arg0, arg1, arg2);
     }
 
     public String getMessage(final String key, final Object arg0, final Object 
arg1, final Object arg2, final Object arg3) {
-        return getMessage(key, new Object[]{arg0, arg1, arg3});
+        return getMessageImpl(key, arg0, arg1, arg3);
     }
 
     public String getMessage(final String key, final Object... arguments){
+        return getMessageImpl(key, arguments);
+    }
+    
+    private String getMessageImpl(final String key, final Object... arguments){
         // XXX Struts caches the MessageFormats. Is constructing a 
MessageFormat really slower than the synchronization?
         final MessageFormat format = new 
MessageFormat(keys.getProperty(key),context.getSite().getLocale());
         return format.format(arguments);

_______________________________________________
Kernel-commits mailing list
[email protected]
http://sesat.no/mailman/listinfo/kernel-commits

Reply via email to