View Javadoc

1   /*
2    * $Id: AbstractSelectModule.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.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: 471754 $ $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      public boolean execute(ActionContext actionCtx)
53          throws Exception {
54          String prefix = getPrefix(actionCtx);
55  
56          // Cache the corresponding ModuleConfig and MessageResources instances
57          ModuleConfig moduleConfig =
58              (ModuleConfig) actionCtx.getApplicationScope().get(Globals.MODULE_KEY
59                  + prefix);
60  
61          if (moduleConfig == null) {
62              throw new IllegalArgumentException("No module config for prefix '"
63                  + prefix + "'");
64          }
65  
66          actionCtx.setModuleConfig(moduleConfig);
67  
68          String key = Globals.MESSAGES_KEY + prefix;
69          MessageResources messageResources =
70              (MessageResources) actionCtx.getApplicationScope().get(key);
71  
72          if (messageResources == null) {
73              throw new IllegalArgumentException(
74                  "No message resources found in application scope under " + key);
75          }
76  
77          actionCtx.setMessageResources(messageResources);
78  
79          return (false);
80      }
81  
82      // ------------------------------------------------------- Protected Methods
83  
84      /**
85       * <p>Calculate and return the module prefix for the module to be selected
86       * for this request.</p>
87       *
88       * @param context The <code>Context</code> for this request
89       * @return Module prefix to be used with this request
90       * @throws IllegalArgumentException if no valid ModuleConfig or
91       *                                  MessageResources can be identified for
92       *                                  this request
93       */
94      protected abstract String getPrefix(ActionContext context);
95  }