KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > RequestLocaleManagerImpl


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 JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
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 JavaDoc _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 JavaDoc _acceptedLocales;
62
63     private Locale JavaDoc _defaultLocale;
64
65     /**
66      * Set of locale names. Incoming requests will be matched to one of these locales.
67      */

68
69     private Set JavaDoc _acceptedLocaleNamesSet = new HashSet JavaDoc();
70
71     /**
72      * Cache of Locales, keyed on locale name.
73      */

74
75     private Map JavaDoc _localeCache = new HashMap JavaDoc();
76
77     public void initializeService()
78     {
79         String JavaDoc[] 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 JavaDoc extractLocaleForCurrentRequest()
91     {
92         String JavaDoc localeName = _cookieSource.readCookieValue(TapestryConstants.LOCALE_COOKIE_NAME);
93
94         String JavaDoc 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     /**
105      * Converts the request locale name into a Locale instance; applies filters (based on
106      * acceptedLocales) if enabled.
107      */

108
109     Locale JavaDoc filterRequestedLocale(String JavaDoc 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 JavaDoc stripTerm(String JavaDoc 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 JavaDoc 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 JavaDoc getLocale(String JavaDoc name)
144     {
145         Locale JavaDoc result = (Locale JavaDoc) _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 JavaDoc constructLocale(String JavaDoc name)
157     {
158         String JavaDoc[] terms = TapestryUtils.split(name, '_');
159
160         switch (terms.length)
161         {
162             case 1:
163                 return new Locale JavaDoc(terms[0], "");
164
165             case 2:
166                 return new Locale JavaDoc(terms[0], terms[1]);
167
168             case 3:
169
170                 return new Locale JavaDoc(terms[0], terms[1], terms[2]);
171
172             default:
173
174                 throw new IllegalArgumentException JavaDoc();
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 JavaDoc acceptedLocales)
194     {
195         _acceptedLocales = acceptedLocales;
196     }
197 }
Popular Tags