KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > actions > LocaleAction


1 /*
2  * $Id: LocaleAction.java 168243 2005-05-05 02:35:30Z niallp $
3  *
4  * Copyright 2000-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19
20 package org.apache.struts.actions;
21
22 import java.util.Locale JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27
28 import org.apache.commons.beanutils.PropertyUtils;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.struts.Globals;
32 import org.apache.struts.action.Action;
33 import org.apache.struts.action.ActionForm;
34 import org.apache.struts.action.ActionForward;
35 import org.apache.struts.action.ActionMapping;
36
37
38 /**
39  * Implementation of <strong>Action</strong> that changes the user's
40  * {@link java.util.Locale} and forwards to a page, based on request level
41  * parameters that are set (language, country, &amp; page).
42  *
43 */

44 public final class LocaleAction extends Action {
45
46     /**
47      * Commons Logging instance.
48     */

49     private Log log = LogFactory.getFactory().getInstance(this.getClass().getName());
50
51     /**
52      * <p>
53      * Change the user's {@link java.util.Locale} based on {@link ActionForm}
54      * properties.
55      * </p>
56      * <p>
57      * This <code>Action</code> looks for <code>language</code> and
58      * <code>country</code> properties on the given form, constructs an
59      * appropriate Locale object, and sets it as the Struts Locale for this
60      * user's session.
61      * Any <code>ActionForm</code>, including a
62      * {@link org.apache.struts.action.DynaActionForm}, may be used.
63      * </p>
64      * <p>
65      * If a <code>page</code> property is also provided, then after
66      * setting the Locale, control is forwarded to that URI path.
67      * Otherwise, control is forwarded to "success".
68      * </p>
69      *
70      * @param mapping The ActionMapping used to select this instance
71      * @param form The optional ActionForm bean for this request (if any)
72      * @param request The HTTP request we are processing
73      * @param response The HTTP response we are creating
74      *
75      * @return Action to forward to
76      * @exception java.lang.Exception if an input/output error or servlet exception occurs
77      */

78     public ActionForward execute(ActionMapping mapping,
79                  ActionForm form,
80                  HttpServletRequest JavaDoc request,
81                  HttpServletResponse JavaDoc response)
82     throws Exception JavaDoc {
83
84     // Extract attributes we will need
85
HttpSession JavaDoc session = request.getSession();
86     Locale JavaDoc locale = getLocale(request);
87
88     String JavaDoc language = null;
89     String JavaDoc country = null;
90     String JavaDoc page = null;
91
92     try {
93             language = (String JavaDoc)
94               PropertyUtils.getSimpleProperty(form, "language");
95             country = (String JavaDoc)
96               PropertyUtils.getSimpleProperty(form, "country");
97             page = (String JavaDoc)
98               PropertyUtils.getSimpleProperty(form, "page");
99         } catch (Exception JavaDoc e) {
100            log.error(e.getMessage(), e);
101         }
102
103         boolean isLanguage = (language != null && language.length() > 0);
104         boolean isCountry = (country != null && country.length() > 0);
105
106         if ((isLanguage) && (isCountry)) {
107            locale = new java.util.Locale JavaDoc(language, country);
108         } else if (isLanguage) {
109            locale = new java.util.Locale JavaDoc(language, "");
110     }
111
112         session.setAttribute(Globals.LOCALE_KEY, locale);
113
114         if (null==page) return mapping.findForward("success");
115         else return new ActionForward(page);
116
117     }
118
119 }
120
Popular Tags