KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > chain > AbstractSelectModule


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.struts.chain;
18
19
20 import org.apache.commons.chain.Command;
21 import org.apache.commons.chain.Context;
22 import org.apache.commons.chain.web.WebContext;
23 import org.apache.struts.Globals;
24 import org.apache.struts.chain.Constants;
25 import org.apache.struts.config.ModuleConfig;
26 import org.apache.struts.util.MessageResources;
27
28
29 /**
30  * <p>Cache the <code>ModuleConfig</code> and <code>MessageResources</code>
31  * instances for the sub-application module to be used for processing
32  * this request.</p>
33  *
34  * @author Craig R. McClanahan
35  * @version $Rev: 54933 $ $Date: 2004-10-16 18:04:52 +0100 (Sat, 16 Oct 2004) $
36  */

37
38 public abstract class AbstractSelectModule implements Command {
39
40
41     // ------------------------------------------------------ Instance Variables
42

43
44     private String JavaDoc messageResourcesKey = Constants.MESSAGE_RESOURCES_KEY;
45     private String JavaDoc moduleConfigKey = Constants.MODULE_CONFIG_KEY;
46
47
48     // -------------------------------------------------------------- Properties
49

50
51     /**
52      * <p>Return the context attribute key under which the default
53      * <code>MessageResources</code> for the currently selected application
54      * module will be stored.</p>
55      */

56     public String JavaDoc getMessageResourcesKey() {
57
58         return (this.messageResourcesKey);
59
60     }
61
62
63     /**
64      * <p>Set the context attribute key under which the default
65      * <code>MessageResources</code> for the currently selected application
66      * module will be stored.</p>
67      *
68      * @param messageResourcesKey The new context attribute key
69      */

70     public void setMessageResourcesKey(String JavaDoc messageResourcesKey) {
71
72         this.messageResourcesKey = messageResourcesKey;
73
74     }
75
76
77     /**
78      * <p>Return the context attribute key under which the
79      * <code>ModuleConfig</code> for the currently selected application
80      * module will be stored.</p>
81      */

82     public String JavaDoc getModuleConfigKey() {
83
84         return (this.moduleConfigKey);
85
86     }
87
88
89     /**
90      * <p>Set the context attribute key under which the
91      * <code>ModuleConfig</code> for the currently selected application
92      * module will be stored.</p>
93      *
94      * @param moduleConfigKey The new context attribute key
95      */

96     public void setModuleConfigKey(String JavaDoc moduleConfigKey) {
97
98         this.moduleConfigKey = moduleConfigKey;
99
100     }
101
102
103     // ---------------------------------------------------------- Public Methods
104

105
106     /**
107      * <p>Cache the <code>ModuleConfig</code> and <code>MessageResources</code>
108      * instances for the sub-application module to be used for processing
109      * this request.</p>
110      *
111      * @param context The <code>Context</code> for the current request
112      *
113      * @exception IllegalArgumentException if no valid
114      * ModuleConfig or MessageResources can be identified for this request
115      *
116      * @return <code>false</code> so that processing continues
117      */

118     public boolean execute(Context context) throws Exception JavaDoc {
119
120         // Identify the module prefix for the current module
121
String JavaDoc prefix = getPrefix(context);
122
123         // Cache the corresponding ModuleConfig and MessageResources instances
124
WebContext wcontext = (WebContext) context;
125         ModuleConfig moduleConfig = (ModuleConfig)
126             wcontext.getApplicationScope().get(Globals.MODULE_KEY + prefix);
127         if (moduleConfig == null) {
128             throw new IllegalArgumentException JavaDoc("No module config for prefix '" +
129                                                prefix + "'");
130         }
131         wcontext.put(getModuleConfigKey(), moduleConfig);
132         wcontext.getRequestScope().put(Globals.MODULE_KEY, moduleConfig);
133         MessageResources messageResources = (MessageResources)
134             wcontext.getApplicationScope().get(Globals.MESSAGES_KEY + prefix);
135         if (messageResources != null) {
136             wcontext.put(getMessageResourcesKey(),
137                                          messageResources);
138             wcontext.getRequestScope().put(Globals.MESSAGES_KEY,
139                                            messageResources);
140         }
141
142         return (false);
143
144     }
145
146
147     // ------------------------------------------------------- Protected Methods
148

149
150     /**
151      * <p>Calculate and return the module prefix for the module to be
152      * selected for this request.</p>
153      *
154      * @param context The <code>Context</code> for this request
155      *
156      * @exception IllegalArgumentException if no valid
157      * ModuleConfig or MessageResources can be identified for this request
158      */

159     protected abstract String JavaDoc getPrefix(Context context);
160
161
162 }
163
Popular Tags