01: /*
02: * $Id: AbstractSelectModule.java 471754 2006-11-06 14:55:09Z husted $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "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) throws Exception {
53: String prefix = getPrefix(actionCtx);
54:
55: // Cache the corresponding ModuleConfig and MessageResources instances
56: ModuleConfig moduleConfig = (ModuleConfig) actionCtx
57: .getApplicationScope().get(Globals.MODULE_KEY + prefix);
58:
59: if (moduleConfig == null) {
60: throw new IllegalArgumentException(
61: "No module config for prefix '" + prefix + "'");
62: }
63:
64: actionCtx.setModuleConfig(moduleConfig);
65:
66: String key = Globals.MESSAGES_KEY + prefix;
67: MessageResources messageResources = (MessageResources) actionCtx
68: .getApplicationScope().get(key);
69:
70: if (messageResources == null) {
71: throw new IllegalArgumentException(
72: "No message resources found in application scope under "
73: + key);
74: }
75:
76: actionCtx.setMessageResources(messageResources);
77:
78: return (false);
79: }
80:
81: // ------------------------------------------------------- Protected Methods
82:
83: /**
84: * <p>Calculate and return the module prefix for the module to be selected
85: * for this request.</p>
86: *
87: * @param context The <code>Context</code> for this request
88: * @return Module prefix to be used with this request
89: * @throws IllegalArgumentException if no valid ModuleConfig or
90: * MessageResources can be identified for
91: * this request
92: */
93: protected abstract String getPrefix(ActionContext context);
94: }
|