1 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 38 public class RequestLocaleManagerImpl implements RequestLocaleManager 39 { 40 private WebRequest _request; 41 42 47 48 private Locale _requestLocale; 49 50 private CookieSource _cookieSource; 51 52 private ThreadLocale _threadLocale; 53 54 60 61 private String _acceptedLocales; 62 63 private Locale _defaultLocale; 64 65 68 69 private Set _acceptedLocaleNamesSet = new HashSet (); 70 71 74 75 private Map _localeCache = new HashMap (); 76 77 public void initializeService() 78 { 79 String [] names = TapestryUtils.split(_acceptedLocales); 80 81 if (names.length == 0) 82 return; 83 84 _defaultLocale = getLocale(names[0]); 85 86 _acceptedLocaleNamesSet.addAll(Arrays.asList(names)); 87 88 } 89 90 public Locale extractLocaleForCurrentRequest() 91 { 92 String localeName = _cookieSource.readCookieValue(TapestryConstants.LOCALE_COOKIE_NAME); 93 94 String requestedLocale = (localeName != null) ? localeName : _request.getLocale() 95 .toString(); 96 97 _requestLocale = filterRequestedLocale(requestedLocale); 98 99 _threadLocale.setLocale(_requestLocale); 100 101 return _requestLocale; 102 } 103 104 108 109 Locale filterRequestedLocale(String localeName) 110 { 111 if (_acceptedLocaleNamesSet.isEmpty()) 112 return getLocale(localeName); 113 114 while (true) 115 { 116 if (_acceptedLocaleNamesSet.contains(localeName)) 117 return getLocale(localeName); 118 119 localeName = stripTerm(localeName); 120 121 if (localeName.length() == 0) 122 return _defaultLocale; 123 } 124 } 125 126 private String stripTerm(String localeName) 127 { 128 int scorex = localeName.lastIndexOf('_'); 129 130 return scorex < 0 ? "" : localeName.substring(0, scorex); 131 } 132 133 public void persistLocale() 134 { 135 Locale locale = _threadLocale.getLocale(); 136 137 if (locale.equals(_requestLocale)) 138 return; 139 140 _cookieSource.writeCookieValue(TapestryConstants.LOCALE_COOKIE_NAME, locale.toString()); 141 } 142 143 Locale getLocale(String name) 144 { 145 Locale result = (Locale ) _localeCache.get(name); 146 147 if (result == null) 148 { 149 result = constructLocale(name); 150 _localeCache.put(name, result); 151 } 152 153 return result; 154 } 155 156 private Locale constructLocale(String name) 157 { 158 String [] terms = TapestryUtils.split(name, '_'); 159 160 switch (terms.length) 161 { 162 case 1: 163 return new Locale (terms[0], ""); 164 165 case 2: 166 return new Locale (terms[0], terms[1]); 167 168 case 3: 169 170 return new Locale (terms[0], terms[1], terms[2]); 171 172 default: 173 174 throw new IllegalArgumentException (); 175 } 176 } 177 178 public void setCookieSource(CookieSource source) 179 { 180 _cookieSource = source; 181 } 182 183 public void setRequest(WebRequest request) 184 { 185 _request = request; 186 } 187 188 public void setThreadLocale(ThreadLocale threadLocale) 189 { 190 _threadLocale = threadLocale; 191 } 192 193 public void setAcceptedLocales(String acceptedLocales) 194 { 195 _acceptedLocales = acceptedLocales; 196 } 197 } | Popular Tags |