Clover coverage report - Code Coverage for tapestry release 4.0.2
Coverage timestamp: Thu Apr 13 2006 10:52:06 EDT
file stats: LOC: 197   Methods: 11
NCLOC: 106   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RequestLocaleManagerImpl.java 100% 95.3% 100% 97.1%
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.Arrays;
 18    import java.util.HashMap;
 19    import java.util.HashSet;
 20    import java.util.Locale;
 21    import java.util.Map;
 22    import java.util.Set;
 23   
 24    import org.apache.hivemind.service.ThreadLocale;
 25    import org.apache.tapestry.TapestryConstants;
 26    import org.apache.tapestry.TapestryUtils;
 27    import org.apache.tapestry.services.CookieSource;
 28    import org.apache.tapestry.services.RequestLocaleManager;
 29    import org.apache.tapestry.web.WebRequest;
 30   
 31    /**
 32    * Service tapestry.request.RequestLocaleManager. Identifies the Locale provided by the client
 33    * (either in a Tapestry-specific cookie, or interpolated from the HTTP header.
 34    *
 35    * @author Howard Lewis Ship
 36    * @since 4.0
 37    */
 38    public class RequestLocaleManagerImpl implements RequestLocaleManager
 39    {
 40    private WebRequest _request;
 41   
 42    /**
 43    * Extracted at start of request, and used at end of request to see if locale has changed.
 44    * Because of this thread-specific state, the service must use the threaded service lifecycle
 45    * model.
 46    */
 47   
 48    private Locale _requestLocale;
 49   
 50    private CookieSource _cookieSource;
 51   
 52    private ThreadLocale _threadLocale;
 53   
 54    /**
 55    * Set from symbol org.apache.tapestry.accepted-locales, a comma-seperated list of locale names.
 56    * The first name is the default for requests that can't be matched against the other locale
 57    * names. May also be blank, in which case, whatever locale was provided in the request is
 58    * accepted (which is Tapestry 3.0 behavior).
 59    */
 60   
 61    private String _acceptedLocales;
 62   
 63    private Locale _defaultLocale;
 64   
 65    /**
 66    * Set of locale names. Incoming requests will be matched to one of these locales.
 67    */
 68   
 69    private Set _acceptedLocaleNamesSet = new HashSet();
 70   
 71    /**
 72    * Cache of Locales, keyed on locale name.
 73    */
 74   
 75    private Map _localeCache = new HashMap();
 76   
 77  40 public void initializeService()
 78    {
 79  40 String[] names = TapestryUtils.split(_acceptedLocales);
 80   
 81  40 if (names.length == 0)
 82  39 return;
 83   
 84  1 _defaultLocale = getLocale(names[0]);
 85   
 86  1 _acceptedLocaleNamesSet.addAll(Arrays.asList(names));
 87   
 88    }
 89   
 90  128 public Locale extractLocaleForCurrentRequest()
 91    {
 92  128 String localeName = _cookieSource.readCookieValue(TapestryConstants.LOCALE_COOKIE_NAME);
 93   
 94  128 String requestedLocale = (localeName != null) ? localeName : _request.getLocale()
 95    .toString();
 96   
 97  128 _requestLocale = filterRequestedLocale(requestedLocale);
 98   
 99  128 _threadLocale.setLocale(_requestLocale);
 100   
 101  128 return _requestLocale;
 102    }
 103   
 104    /**
 105    * Converts the request locale name into a Locale instance; applies filters (based on
 106    * acceptedLocales) if enabled.
 107    */
 108   
 109  134 Locale filterRequestedLocale(String localeName)
 110    {
 111  134 if (_acceptedLocaleNamesSet.isEmpty())
 112  129 return getLocale(localeName);
 113   
 114  5 while (true)
 115    {
 116  9 if (_acceptedLocaleNamesSet.contains(localeName))
 117  4 return getLocale(localeName);
 118   
 119  5 localeName = stripTerm(localeName);
 120   
 121  5 if (localeName.length() == 0)
 122  1 return _defaultLocale;
 123    }
 124    }
 125   
 126  5 private String stripTerm(String localeName)
 127    {
 128  5 int scorex = localeName.lastIndexOf('_');
 129   
 130  5 return scorex < 0 ? "" : localeName.substring(0, scorex);
 131    }
 132   
 133  132 public void persistLocale()
 134    {
 135  132 Locale locale = _threadLocale.getLocale();
 136   
 137  132 if (locale.equals(_requestLocale))
 138  119 return;
 139   
 140  13 _cookieSource.writeCookieValue(TapestryConstants.LOCALE_COOKIE_NAME, locale.toString());
 141    }
 142   
 143  136 Locale getLocale(String name)
 144    {
 145  136 Locale result = (Locale) _localeCache.get(name);
 146   
 147  136 if (result == null)
 148    {
 149  51 result = constructLocale(name);
 150  51 _localeCache.put(name, result);
 151    }
 152   
 153  136 return result;
 154    }
 155   
 156  51 private Locale constructLocale(String name)
 157    {
 158  51 String[] terms = TapestryUtils.split(name, '_');
 159   
 160  51 switch (terms.length)
 161    {
 162  49 case 1:
 163  49 return new Locale(terms[0], "");
 164   
 165  1 case 2:
 166  1 return new Locale(terms[0], terms[1]);
 167   
 168  1 case 3:
 169   
 170  1 return new Locale(terms[0], terms[1], terms[2]);
 171   
 172  0 default:
 173   
 174  0 throw new IllegalArgumentException();
 175    }
 176    }
 177   
 178  45 public void setCookieSource(CookieSource source)
 179    {
 180  45 _cookieSource = source;
 181    }
 182   
 183  41 public void setRequest(WebRequest request)
 184    {
 185  41 _request = request;
 186    }
 187   
 188  45 public void setThreadLocale(ThreadLocale threadLocale)
 189    {
 190  45 _threadLocale = threadLocale;
 191    }
 192   
 193  40 public void setAcceptedLocales(String acceptedLocales)
 194    {
 195  40 _acceptedLocales = acceptedLocales;
 196    }
 197    }