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