View Javadoc

1   /*
2    * $Id: AbstractSelectModule.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.struts.Globals;
21  import org.apache.struts.chain.contexts.ActionContext;
22  import org.apache.struts.config.ModuleConfig;
23  import org.apache.struts.util.MessageResources;
24  
25  /***
26   * <p>Cache the <code>ModuleConfig</code> and <code>MessageResources</code>
27   * instances for the sub-application module to be used for processing this
28   * 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 AbstractSelectModule extends ActionCommandBase {
34      // ------------------------------------------------------ Instance Variables
35      // ---------------------------------------------------------- Public Methods
36  
37      /***
38       * <p>Cache the <code>ModuleConfig</code> and <code>MessageResources</code>
39       * instances for the sub-application module to be used for processing this
40       * request.</p>
41       *
42       * @param actionCtx The <code>Context</code> for the current request
43       * @return <code>false</code> so that processing continues
44       * @throws IllegalArgumentException if no valid ModuleConfig or
45       *                                  MessageResources can be identified for
46       *                                  this request
47       * @throws Exception                if thrown by the Action class
48       */
49      public boolean execute(ActionContext actionCtx)
50          throws Exception {
51          String prefix = getPrefix(actionCtx);
52  
53          // Cache the corresponding ModuleConfig and MessageResources instances
54          ModuleConfig moduleConfig =
55              (ModuleConfig) actionCtx.getApplicationScope().get(Globals.MODULE_KEY
56                  + prefix);
57  
58          if (moduleConfig == null) {
59              throw new IllegalArgumentException("No module config for prefix '"
60                  + prefix + "'");
61          }
62  
63          actionCtx.setModuleConfig(moduleConfig);
64  
65          String key = Globals.MESSAGES_KEY + prefix;
66          MessageResources messageResources =
67              (MessageResources) actionCtx.getApplicationScope().get(key);
68  
69          if (messageResources == null) {
70              throw new IllegalArgumentException(
71                  "No message resources found in application scope under " + key);
72          }
73  
74          actionCtx.setMessageResources(messageResources);
75  
76          return (false);
77      }
78  
79      // ------------------------------------------------------- Protected Methods
80  
81      /***
82       * <p>Calculate and return the module prefix for the module to be selected
83       * for this request.</p>
84       *
85       * @param context The <code>Context</code> for this request
86       * @return Module prefix to be used with this request
87       * @throws IllegalArgumentException if no valid ModuleConfig or
88       *                                  MessageResources can be identified for
89       *                                  this request
90       */
91      protected abstract String getPrefix(ActionContext context);
92  }