1 /*
2 * $Id$
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 java.util.Locale;
24
25 import org.apache.struts.chain.contexts.ActionContext;
26 import org.apache.struts.config.ModuleConfig;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31 * <p>Select the <code>Locale</code> to be used for this request.</p>
32 *
33 * @version $Rev$ $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 * The {@code Log} instance for this class.
41 */
42 private final Logger log =
43 LoggerFactory.getLogger(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 @Override
55 protected boolean execute_(ActionContext actionCtx)
56 throws Exception {
57 // Are we configured to select Locale automatically?
58 log.trace("retrieve config...");
59
60 ModuleConfig moduleConfig = actionCtx.getModuleConfig();
61
62 if (!moduleConfig.getControllerConfig().getLocale()) {
63 log.debug("module is not configured for a specific locale; "
64 + "nothing to do");
65
66 return CONTINUE_PROCESSING;
67 }
68
69 // Retrieve and cache appropriate Locale for this request
70 Locale locale = getLocale(actionCtx);
71
72 log.debug("set context locale to {}", locale);
73
74 actionCtx.setLocale(locale);
75
76 return CONTINUE_PROCESSING;
77 }
78
79 // ------------------------------------------------------- Protected Methods
80
81 /**
82 * <p>Return the <code>Locale</code> to be used for this request.</p>
83 *
84 * @param context The <code>Context</code> for this request
85 * @return The Locale to be used for this request
86 */
87 protected abstract Locale getLocale(ActionContext context);
88 }