1 /*
2 * $Id: AbstractSelectLocale.java 471754 2006-11-06 14:55:09Z husted $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21 package org.apache.struts.chain.commands;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.struts.chain.contexts.ActionContext;
26 import org.apache.struts.config.ModuleConfig;
27
28 import java.util.Locale;
29
30 /**
31 * <p>Select the <code>Locale</code> to be used for this request.</p>
32 *
33 * @version $Rev: 471754 $ $Date: 2005-11-12 13:01:44 -0500 (Sat, 12 Nov 2005)
34 * $
35 */
36 public abstract class AbstractSelectLocale extends ActionCommandBase {
37 // ------------------------------------------------------ Instance Variables
38
39 /**
40 * <p> Provide Commons Logging instance for this class. </p>
41 */
42 private static final Log LOG =
43 LogFactory.getLog(AbstractSelectLocale.class);
44
45 // ---------------------------------------------------------- Public Methods
46
47 /**
48 * <p>Select the <code>Locale</code> to be used for this request.</p>
49 *
50 * @param actionCtx The <code>Context</code> for the current request
51 * @return <code>false</code> so that processing continues
52 * @throws Exception if thrown by the Action class
53 */
54 public boolean execute(ActionContext actionCtx)
55 throws Exception {
56 // Are we configured to select Locale automatically?
57 LOG.trace("retrieve config...");
58
59 ModuleConfig moduleConfig = actionCtx.getModuleConfig();
60
61 if (!moduleConfig.getControllerConfig().getLocale()) {
62 if (LOG.isDebugEnabled()) {
63 LOG.debug("module is not configured for a specific locale; "
64 + "nothing to do");
65 }
66
67 return (false);
68 }
69
70 // Retrieve and cache appropriate Locale for this request
71 Locale locale = getLocale(actionCtx);
72
73 if (LOG.isDebugEnabled()) {
74 LOG.debug("set context locale to " + locale);
75 }
76
77 actionCtx.setLocale(locale);
78
79 return (false);
80 }
81
82 // ------------------------------------------------------- Protected Methods
83
84 /**
85 * <p>Return the <code>Locale</code> to be used for this request.</p>
86 *
87 * @param context The <code>Context</code> for this request
88 * @return The Locale to be used for this request
89 */
90 protected abstract Locale getLocale(ActionContext context);
91 }