Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 118   Methods: 7
NCLOC: 60   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
RequestLocaleManagerImpl.java 100% 94.7% 100% 96.7%
coverage 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.tapestry.services.impl;
 16   
 
 17   
 import java.util.Locale;
 18   
 
 19   
 import org.apache.hivemind.service.ThreadLocale;
 20   
 import org.apache.tapestry.TapestryConstants;
 21   
 import org.apache.tapestry.services.CookieSource;
 22   
 import org.apache.tapestry.services.RequestLocaleManager;
 23   
 import org.apache.tapestry.util.StringSplitter;
 24   
 import org.apache.tapestry.web.WebRequest;
 25   
 
 26   
 /**
 27   
  * Identifies the Locale provided by the client (either in a Tapestry-specific cookie, or
 28   
  * interpolated from the HTTP header. TODO: Add the ability to "filter down" Locales down to a
 29   
  * predifined set (specified using some form of HiveMInd configuration).
 30   
  * 
 31   
  * @author Howard Lewis Ship
 32   
  * @since 4.0
 33   
  */
 34   
 public class RequestLocaleManagerImpl implements RequestLocaleManager
 35   
 {
 36   
     private WebRequest _request;
 37   
 
 38   
     /**
 39   
      * Extracted at start of request, and used at end of request to see if locale has changed.
 40   
      * Because of this thread-specific state, the service must use the threaded service lifecycle
 41   
      * model.
 42   
      */
 43   
 
 44   
     private Locale _requestLocale;
 45   
 
 46   
     private CookieSource _cookieSource;
 47   
 
 48   
     private ThreadLocale _threadLocale;
 49   
 
 50  153
     public Locale extractLocaleForCurrentRequest()
 51   
     {
 52  153
         String localeName = _cookieSource.readCookieValue(TapestryConstants.LOCALE_COOKIE_NAME);
 53   
 
 54  153
         _requestLocale = (localeName != null) ? getLocale(localeName) : _request.getLocale();
 55   
 
 56  153
         _threadLocale.setLocale(_requestLocale);
 57   
 
 58  153
         return _requestLocale;
 59   
     }
 60   
 
 61  160
     public void persistLocale()
 62   
     {
 63  160
         Locale locale = _threadLocale.getLocale();
 64   
 
 65  160
         if (locale.equals(_requestLocale))
 66  147
             return;
 67   
 
 68  13
         _cookieSource.writeCookieValue(TapestryConstants.LOCALE_COOKIE_NAME, locale.toString());
 69   
     }
 70   
 
 71  12
     private Locale getLocale(String name)
 72   
     {
 73   
         // There used to be a cache of Locale (keyed on name), but since this service is
 74   
         // threaded, there's no point (short of making it static, which is too ugly for words).
 75   
         // Instead, we should have a LocaleCache service for that purpose. Have to balance
 76   
         // cost of invoking that service vs. the cost of creating new Locale instances all the time.
 77   
 
 78  12
         return constructLocale(name);
 79   
     }
 80   
 
 81  12
     private Locale constructLocale(String name)
 82   
     {
 83  12
         StringSplitter splitter = new StringSplitter('_');
 84  12
         String[] terms = splitter.splitToArray(name);
 85   
 
 86  12
         switch (terms.length)
 87   
         {
 88   
             case 1:
 89  10
                 return new Locale(terms[0], "");
 90   
 
 91   
             case 2:
 92  1
                 return new Locale(terms[0], terms[1]);
 93   
 
 94   
             case 3:
 95   
 
 96  1
                 return new Locale(terms[0], terms[1], terms[2]);
 97   
 
 98   
             default:
 99   
 
 100  0
                 throw new IllegalArgumentException();
 101   
         }
 102   
     }
 103   
 
 104  154
     public void setCookieSource(CookieSource source)
 105   
     {
 106  154
         _cookieSource = source;
 107   
     }
 108   
 
 109  150
     public void setRequest(WebRequest request)
 110   
     {
 111  150
         _request = request;
 112   
     }
 113   
 
 114  154
     public void setThreadLocale(ThreadLocale threadLocale)
 115   
     {
 116  154
         _threadLocale = threadLocale;
 117   
     }
 118   
 }