1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.util;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.struts.Globals;
26 import org.apache.struts.action.RequestProcessor;
27 import org.apache.struts.config.MessageResourcesConfig;
28 import org.apache.struts.config.ModuleConfig;
29
30 import javax.servlet.ServletContext;
31 import javax.servlet.http.HttpServletRequest;
32
33
34
35
36
37
38
39 public class ModuleUtils {
40
41
42
43 private static final ModuleUtils instance = new ModuleUtils();
44
45
46
47
48 private static final Log log = LogFactory.getLog(ModuleUtils.class);
49
50
51
52
53 protected ModuleUtils() {
54 super();
55 }
56
57
58
59
60 public static ModuleUtils getInstance() {
61 return instance;
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public ModuleConfig getModuleConfig(HttpServletRequest request) {
77 return (ModuleConfig) request.getAttribute(Globals.MODULE_KEY);
78 }
79
80
81
82
83
84
85
86
87
88
89 public ModuleConfig getModuleConfig(String prefix, ServletContext context) {
90 if ((prefix == null) || "/".equals(prefix)) {
91 return (ModuleConfig) context.getAttribute(Globals.MODULE_KEY);
92 } else {
93 return (ModuleConfig) context.getAttribute(Globals.MODULE_KEY
94 + prefix);
95 }
96 }
97
98
99
100
101
102
103
104
105
106
107
108 public ModuleConfig getModuleConfig(String prefix,
109 HttpServletRequest request, ServletContext context) {
110 ModuleConfig moduleConfig = null;
111
112 if (prefix != null) {
113
114 moduleConfig = this.getModuleConfig(prefix, context);
115 } else {
116
117 moduleConfig = this.getModuleConfig(request, context);
118 }
119
120 return moduleConfig;
121 }
122
123
124
125
126
127
128
129
130 public ModuleConfig getModuleConfig(HttpServletRequest request,
131 ServletContext context) {
132 ModuleConfig moduleConfig = this.getModuleConfig(request);
133
134 if (moduleConfig == null) {
135 moduleConfig = this.getModuleConfig("", context);
136 request.setAttribute(Globals.MODULE_KEY, moduleConfig);
137 }
138
139 return moduleConfig;
140 }
141
142
143
144
145
146
147
148
149 public String getModuleName(HttpServletRequest request,
150 ServletContext context) {
151
152 String matchPath =
153 (String) request.getAttribute(RequestProcessor.INCLUDE_SERVLET_PATH);
154
155 if (matchPath == null) {
156 matchPath = request.getServletPath();
157 }
158
159 return this.getModuleName(matchPath, context);
160 }
161
162
163
164
165
166
167
168
169 public String getModuleName(String matchPath, ServletContext context) {
170 if (log.isDebugEnabled()) {
171 log.debug("Get module name for path " + matchPath);
172 }
173
174 String prefix = "";
175 String[] prefixes = getModulePrefixes(context);
176
177
178 int lastSlash = 0;
179
180 while (prefix.equals("")
181 && ((lastSlash = matchPath.lastIndexOf("/")) > 0)) {
182
183 matchPath = matchPath.substring(0, lastSlash);
184
185
186 for (int i = 0; i < prefixes.length; i++) {
187 if (matchPath.equals(prefixes[i])) {
188 prefix = prefixes[i];
189
190 break;
191 }
192 }
193 }
194
195 if (log.isDebugEnabled()) {
196 log.debug("Module name found: "
197 + (prefix.equals("") ? "default" : prefix));
198 }
199
200 return prefix;
201 }
202
203
204
205
206
207
208
209
210
211 public String[] getModulePrefixes(ServletContext context) {
212 return (String[]) context.getAttribute(Globals.MODULE_PREFIXES_KEY);
213 }
214
215
216
217
218
219
220
221
222 public void selectModule(HttpServletRequest request, ServletContext context) {
223
224 String prefix = getModuleName(request, context);
225
226
227 this.selectModule(prefix, request, context);
228 }
229
230
231
232
233
234
235
236
237
238 public void selectModule(String prefix, HttpServletRequest request,
239 ServletContext context) {
240
241 ModuleConfig config = getModuleConfig(prefix, context);
242
243 if (config != null) {
244 request.setAttribute(Globals.MODULE_KEY, config);
245
246 MessageResourcesConfig[] mrConfig =
247 config.findMessageResourcesConfigs();
248
249 for (int i = 0; i < mrConfig.length; i++) {
250 String key = mrConfig[i].getKey();
251 MessageResources resources =
252 (MessageResources) context.getAttribute(key + prefix);
253
254 if (resources != null) {
255 request.setAttribute(key, resources);
256 } else {
257 request.removeAttribute(key);
258 }
259 }
260 } else {
261 request.removeAttribute(Globals.MODULE_KEY);
262 }
263 }
264 }