001: /*
002: * $Id: ModuleUtils.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.util;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.struts.Globals;
026: import org.apache.struts.action.RequestProcessor;
027: import org.apache.struts.config.MessageResourcesConfig;
028: import org.apache.struts.config.ModuleConfig;
029:
030: import javax.servlet.ServletContext;
031: import javax.servlet.http.HttpServletRequest;
032:
033: /**
034: * General purpose utility methods related to module processing.
035: *
036: * @version $Rev: 471754 $
037: * @since Struts 1.2
038: */
039: public class ModuleUtils {
040: /**
041: * The Singleton instance.
042: */
043: private static final ModuleUtils instance = new ModuleUtils();
044:
045: /**
046: * Commons logging instance.
047: */
048: private static final Log log = LogFactory.getLog(ModuleUtils.class);
049:
050: /**
051: * Constructor for ModuleUtils.
052: */
053: protected ModuleUtils() {
054: super ();
055: }
056:
057: /**
058: * Returns the Singleton instance of TagUtils.
059: */
060: public static ModuleUtils getInstance() {
061: return instance;
062: }
063:
064: /**
065: * Return the current ModuleConfig object stored in request, if it exists,
066: * null otherwise. This method can be used by plugin to retrieve the
067: * current module config object. If no moduleConfig is found, this means
068: * that the request haven't hit the server throught the struts servlet.
069: * The appropriate module config can be set and found with <code>{@link
070: * ModuleUtils#selectModule(HttpServletRequest, ServletContext)} </code>.
071: *
072: * @param request The servlet request we are processing
073: * @return the ModuleConfig object from request, or null if none is set in
074: * the request.
075: */
076: public ModuleConfig getModuleConfig(HttpServletRequest request) {
077: return (ModuleConfig) request.getAttribute(Globals.MODULE_KEY);
078: }
079:
080: /**
081: * Return the desired ModuleConfig object stored in context, if it exists,
082: * null otherwise.
083: *
084: * @param prefix The module prefix of the desired module
085: * @param context The ServletContext for this web application
086: * @return the ModuleConfig object specified, or null if not found in the
087: * context.
088: */
089: public ModuleConfig getModuleConfig(String prefix,
090: ServletContext context) {
091: if ((prefix == null) || "/".equals(prefix)) {
092: return (ModuleConfig) context
093: .getAttribute(Globals.MODULE_KEY);
094: } else {
095: return (ModuleConfig) context
096: .getAttribute(Globals.MODULE_KEY + prefix);
097: }
098: }
099:
100: /**
101: * Return the desired ModuleConfig object stored in context, if it exists,
102: * otherwise return the current ModuleConfig
103: *
104: * @param prefix The module prefix of the desired module
105: * @param request The servlet request we are processing
106: * @param context The ServletContext for this web application
107: * @return the ModuleConfig object specified, or null if not found in the
108: * context.
109: */
110: public ModuleConfig getModuleConfig(String prefix,
111: HttpServletRequest request, ServletContext context) {
112: ModuleConfig moduleConfig = null;
113:
114: if (prefix != null) {
115: //lookup module stored with the given prefix.
116: moduleConfig = this .getModuleConfig(prefix, context);
117: } else {
118: //return the current module if no prefix was supplied.
119: moduleConfig = this .getModuleConfig(request, context);
120: }
121:
122: return moduleConfig;
123: }
124:
125: /**
126: * Return the ModuleConfig object is it exists, null otherwise.
127: *
128: * @param request The servlet request we are processing
129: * @param context The ServletContext for this web application
130: * @return the ModuleConfig object
131: */
132: public ModuleConfig getModuleConfig(HttpServletRequest request,
133: ServletContext context) {
134: ModuleConfig moduleConfig = this .getModuleConfig(request);
135:
136: if (moduleConfig == null) {
137: moduleConfig = this .getModuleConfig("", context);
138: request.setAttribute(Globals.MODULE_KEY, moduleConfig);
139: }
140:
141: return moduleConfig;
142: }
143:
144: /**
145: * Get the module name to which the specified request belong.
146: *
147: * @param request The servlet request we are processing
148: * @param context The ServletContext for this web application
149: * @return The module prefix or ""
150: */
151: public String getModuleName(HttpServletRequest request,
152: ServletContext context) {
153: // Acquire the path used to compute the module
154: String matchPath = (String) request
155: .getAttribute(RequestProcessor.INCLUDE_SERVLET_PATH);
156:
157: if (matchPath == null) {
158: matchPath = request.getServletPath();
159: }
160:
161: return this .getModuleName(matchPath, context);
162: }
163:
164: /**
165: * Get the module name to which the specified uri belong.
166: *
167: * @param matchPath The uri from which we want the module name.
168: * @param context The ServletContext for this web application
169: * @return The module prefix or ""
170: */
171: public String getModuleName(String matchPath, ServletContext context) {
172: if (log.isDebugEnabled()) {
173: log.debug("Get module name for path " + matchPath);
174: }
175:
176: String prefix = ""; // Initialize prefix before we try lookup
177: String[] prefixes = getModulePrefixes(context);
178:
179: // Get all other possible prefixes
180: int lastSlash = 0; // Initialize before loop
181:
182: while (prefix.equals("")
183: && ((lastSlash = matchPath.lastIndexOf("/")) > 0)) {
184: // We may be in a non-default module. Try to get it's prefix.
185: matchPath = matchPath.substring(0, lastSlash);
186:
187: // Match against the list of module prefixes
188: for (int i = 0; i < prefixes.length; i++) {
189: if (matchPath.equals(prefixes[i])) {
190: prefix = prefixes[i];
191:
192: break;
193: }
194: }
195: }
196:
197: if (log.isDebugEnabled()) {
198: log.debug("Module name found: "
199: + (prefix.equals("") ? "default" : prefix));
200: }
201:
202: return prefix;
203: }
204:
205: /**
206: * Return the list of module prefixes that are defined for this web
207: * application. <strong>NOTE</strong> - the "" prefix for the default
208: * module is not included in this list.
209: *
210: * @param context The ServletContext for this web application.
211: * @return An array of module prefixes.
212: */
213: public String[] getModulePrefixes(ServletContext context) {
214: return (String[]) context
215: .getAttribute(Globals.MODULE_PREFIXES_KEY);
216: }
217:
218: /**
219: * Select the module to which the specified request belongs, and add
220: * corresponding request attributes to this request.
221: *
222: * @param request The servlet request we are processing
223: * @param context The ServletContext for this web application
224: */
225: public void selectModule(HttpServletRequest request,
226: ServletContext context) {
227: // Compute module name
228: String prefix = getModuleName(request, context);
229:
230: // Expose the resources for this module
231: this .selectModule(prefix, request, context);
232: }
233:
234: /**
235: * Select the module to which the specified request belongs, and add
236: * corresponding request attributes to this request.
237: *
238: * @param prefix The module prefix of the desired module
239: * @param request The servlet request we are processing
240: * @param context The ServletContext for this web application
241: */
242: public void selectModule(String prefix, HttpServletRequest request,
243: ServletContext context) {
244: // Expose the resources for this module
245: ModuleConfig config = getModuleConfig(prefix, context);
246:
247: if (config != null) {
248: request.setAttribute(Globals.MODULE_KEY, config);
249:
250: MessageResourcesConfig[] mrConfig = config
251: .findMessageResourcesConfigs();
252:
253: for (int i = 0; i < mrConfig.length; i++) {
254: String key = mrConfig[i].getKey();
255: MessageResources resources = (MessageResources) context
256: .getAttribute(key + prefix);
257:
258: if (resources != null) {
259: request.setAttribute(key, resources);
260: } else {
261: request.removeAttribute(key);
262: }
263: }
264: } else {
265: request.removeAttribute(Globals.MODULE_KEY);
266: }
267: }
268: }
|