View Javadoc

1   /*
2    * $Id: AbstractSelectLocale.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 2003-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  package org.apache.struts.chain.commands;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.struts.chain.contexts.ActionContext;
23  import org.apache.struts.config.ModuleConfig;
24  
25  import java.util.Locale;
26  
27  /***
28   * <p>Select the <code>Locale</code> to be used for this request.</p>
29   *
30   * @version $Rev: 421119 $ $Date: 2005-11-12 13:01:44 -0500 (Sat, 12 Nov 2005)
31   *          $
32   */
33  public abstract class AbstractSelectLocale extends ActionCommandBase {
34      // ------------------------------------------------------ Instance Variables
35  
36      /***
37       * <p> Provide Commons Logging instance for this class. </p>
38       */
39      private static final Log LOG =
40          LogFactory.getLog(AbstractSelectLocale.class);
41  
42      // ---------------------------------------------------------- Public Methods
43  
44      /***
45       * <p>Select the <code>Locale</code> to be used for this request.</p>
46       *
47       * @param actionCtx The <code>Context</code> for the current request
48       * @return <code>false</code> so that processing continues
49       * @throws Exception if thrown by the Action class
50       */
51      public boolean execute(ActionContext actionCtx)
52          throws Exception {
53          // Are we configured to select Locale automatically?
54          LOG.trace("retrieve config...");
55  
56          ModuleConfig moduleConfig = actionCtx.getModuleConfig();
57  
58          if (!moduleConfig.getControllerConfig().getLocale()) {
59              if (LOG.isDebugEnabled()) {
60                  LOG.debug("module is not configured for a specific locale; "
61                      + "nothing to do");
62              }
63  
64              return (false);
65          }
66  
67          // Retrieve and cache appropriate Locale for this request
68          Locale locale = getLocale(actionCtx);
69  
70          if (LOG.isDebugEnabled()) {
71              LOG.debug("set context locale to " + locale);
72          }
73  
74          actionCtx.setLocale(locale);
75  
76          return (false);
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  }