KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > util > ModuleUtils


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

18
19 package org.apache.struts.util;
20
21 import javax.servlet.ServletContext JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.struts.Globals;
27 import org.apache.struts.action.RequestProcessor;
28 import org.apache.struts.config.ModuleConfig;
29 import org.apache.struts.config.MessageResourcesConfig;
30
31 /**
32  * General purpose utility methods related to module processing.
33  *
34  * @version $Rev: 54929 $
35  * @since Struts 1.2
36  */

37 public class ModuleUtils {
38
39     /**
40      * The Singleton instance.
41      */

42     private static final ModuleUtils instance = new ModuleUtils();
43
44     /**
45      * Commons logging instance.
46      */

47     private static final Log log = LogFactory.getLog(ModuleUtils.class);
48
49     /**
50      * Returns the Singleton instance of TagUtils.
51      */

52     public static ModuleUtils getInstance() {
53         return instance;
54     }
55
56     /**
57      * Constructor for ModuleUtils.
58      */

59     protected ModuleUtils() {
60         super();
61     }
62     
63     /**
64      * Return the current ModuleConfig object stored in request, if it exists,
65      * null otherwise.
66      * This method can be used by plugin to retrieve the current module config
67      * object. If no moduleConfig is found, this means that the request haven't
68      * hit the server throught the struts servlet. The appropriate module config
69      * can be set and found with
70      * <code>{@link RequestUtils#selectModule(HttpServletRequest, ServletContext)} </code>.
71      * @param request The servlet request we are processing
72      * @return the ModuleConfig object from request, or null if none is set in
73      * the request.
74      */

75     public ModuleConfig getModuleConfig(HttpServletRequest JavaDoc request) {
76         return (ModuleConfig) request.getAttribute(Globals.MODULE_KEY);
77     }
78     
79     /**
80      * Return the desired ModuleConfig object stored in context, if it exists,
81      * null otherwise.
82      *
83      * @param prefix The module prefix of the desired module
84      * @param context The ServletContext for this web application
85      * @return the ModuleConfig object specified, or null if not found in
86      * the context.
87      */

88     public ModuleConfig getModuleConfig(String JavaDoc prefix, ServletContext JavaDoc context) {
89         return (ModuleConfig) context.getAttribute(Globals.MODULE_KEY + prefix);
90     }
91     
92     /**
93      * Return the desired ModuleConfig object stored in context, if it exists,
94      * otherwise return the current ModuleConfig
95      *
96      * @param prefix The module prefix of the desired module
97      * @param request The servlet request we are processing
98      * @param context The ServletContext for this web application
99      * @return the ModuleConfig object specified, or null if not found in
100      * the context.
101      */

102     public ModuleConfig getModuleConfig(String JavaDoc prefix, HttpServletRequest JavaDoc request, ServletContext JavaDoc context) {
103         ModuleConfig moduleConfig = null;
104         
105         
106         if(prefix != null) {
107             //lookup module stored with the given prefix.
108
moduleConfig = this.getModuleConfig(prefix, context);
109         }
110         else {
111             //return the current module if no prefix was supplied.
112
moduleConfig = this.getModuleConfig(request, context);
113         }
114         return moduleConfig;
115     }
116
117     /**
118      * Return the ModuleConfig object is it exists, null otherwise.
119      * @param request The servlet request we are processing
120      * @param context The ServletContext for this web application
121      * @return the ModuleConfig object
122      */

123     public ModuleConfig getModuleConfig(
124         HttpServletRequest JavaDoc request,
125         ServletContext JavaDoc context) {
126
127         ModuleConfig moduleConfig = this.getModuleConfig(request);
128
129         if (moduleConfig == null) {
130             moduleConfig = this.getModuleConfig("", context);
131             request.setAttribute(Globals.MODULE_KEY, moduleConfig);
132         }
133
134         return moduleConfig;
135     }
136
137
138     /**
139      * Get the module name to which the specified request belong.
140      * @param request The servlet request we are processing
141      * @param context The ServletContext for this web application
142      * @return The module prefix or ""
143      */

144     public String JavaDoc getModuleName(
145         HttpServletRequest JavaDoc request,
146         ServletContext JavaDoc context) {
147
148         // Acquire the path used to compute the module
149
String JavaDoc matchPath =
150             (String JavaDoc) request.getAttribute(RequestProcessor.INCLUDE_SERVLET_PATH);
151
152         if (matchPath == null) {
153             matchPath = request.getServletPath();
154         }
155
156         return this.getModuleName(matchPath, context);
157     }
158
159     /**
160      * Get the module name to which the specified uri belong.
161      * @param matchPath The uri from which we want the module name.
162      * @param context The ServletContext for this web application
163      * @return The module prefix or ""
164      */

165     public String JavaDoc getModuleName(String JavaDoc matchPath, ServletContext JavaDoc context) {
166         if (log.isDebugEnabled()) {
167             log.debug("Get module name for path " + matchPath);
168         }
169
170         String JavaDoc prefix = ""; // Initialize prefix before we try lookup
171
String JavaDoc prefixes[] = getModulePrefixes(context);
172         // Get all other possible prefixes
173
int lastSlash = 0; // Initialize before loop
174

175         while (prefix.equals("")
176             && ((lastSlash = matchPath.lastIndexOf("/")) > 0)) {
177
178             // We may be in a non-default module. Try to get it's prefix.
179
matchPath = matchPath.substring(0, lastSlash);
180
181             // Match against the list of module prefixes
182
for (int i = 0; i < prefixes.length; i++) {
183                 if (matchPath.equals(prefixes[i])) {
184                     prefix = prefixes[i];
185                     break;
186                 }
187             }
188         }
189
190         if (log.isDebugEnabled()) {
191             log.debug(
192                 "Module name found: " + (prefix.equals("") ? "default" : prefix));
193         }
194
195         return prefix;
196     }
197
198     /**
199      * Return the list of module prefixes that are defined for
200      * this web application. <strong>NOTE</strong> -
201      * the "" prefix for the default module is not included in this list.
202      *
203      * @param context The ServletContext for this web application.
204      * @return An array of module prefixes.
205      */

206     public String JavaDoc[] getModulePrefixes(ServletContext JavaDoc context) {
207         return (String JavaDoc[]) context.getAttribute(Globals.MODULE_PREFIXES_KEY);
208     }
209
210     /**
211      * Select the module to which the specified request belongs, and
212      * add corresponding request attributes to this request.
213      *
214      * @param request The servlet request we are processing
215      * @param context The ServletContext for this web application
216      */

217     public void selectModule(HttpServletRequest JavaDoc request, ServletContext JavaDoc context) {
218         // Compute module name
219
String JavaDoc prefix = getModuleName(request, context);
220
221         // Expose the resources for this module
222
this.selectModule(prefix, request, context);
223
224     }
225
226     /**
227      * Select the module to which the specified request belongs, and
228      * add corresponding request attributes to this request.
229      *
230      * @param prefix The module prefix of the desired module
231      * @param request The servlet request we are processing
232      * @param context The ServletContext for this web application
233      */

234     public void selectModule(
235         String JavaDoc prefix,
236         HttpServletRequest JavaDoc request,
237         ServletContext JavaDoc context) {
238
239         // Expose the resources for this module
240
ModuleConfig config = getModuleConfig(prefix, context);
241
242         if (config != null) {
243             request.setAttribute(Globals.MODULE_KEY, config);
244         } else {
245             request.removeAttribute(Globals.MODULE_KEY);
246         }
247
248         MessageResourcesConfig[] mrConfig = config.findMessageResourcesConfigs();
249         for(int i = 0; i < mrConfig.length; i++) {
250           String JavaDoc key = mrConfig[i].getKey();
251           MessageResources resources =
252             (MessageResources) context.getAttribute(key + prefix);
253           if (resources != null) {
254               request.setAttribute(key, resources);
255           } else {
256               request.removeAttribute(key);
257           }
258         }
259     }
260 }
261
Popular Tags