1 18 19 package org.apache.struts.util; 20 21 import javax.servlet.ServletContext ; 22 import javax.servlet.http.HttpServletRequest ; 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 37 public class ModuleUtils { 38 39 42 private static final ModuleUtils instance = new ModuleUtils(); 43 44 47 private static final Log log = LogFactory.getLog(ModuleUtils.class); 48 49 52 public static ModuleUtils getInstance() { 53 return instance; 54 } 55 56 59 protected ModuleUtils() { 60 super(); 61 } 62 63 75 public ModuleConfig getModuleConfig(HttpServletRequest request) { 76 return (ModuleConfig) request.getAttribute(Globals.MODULE_KEY); 77 } 78 79 88 public ModuleConfig getModuleConfig(String prefix, ServletContext context) { 89 return (ModuleConfig) context.getAttribute(Globals.MODULE_KEY + prefix); 90 } 91 92 102 public ModuleConfig getModuleConfig(String prefix, HttpServletRequest request, ServletContext context) { 103 ModuleConfig moduleConfig = null; 104 105 106 if(prefix != null) { 107 moduleConfig = this.getModuleConfig(prefix, context); 109 } 110 else { 111 moduleConfig = this.getModuleConfig(request, context); 113 } 114 return moduleConfig; 115 } 116 117 123 public ModuleConfig getModuleConfig( 124 HttpServletRequest request, 125 ServletContext 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 144 public String getModuleName( 145 HttpServletRequest request, 146 ServletContext context) { 147 148 String matchPath = 150 (String ) 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 165 public String getModuleName(String matchPath, ServletContext context) { 166 if (log.isDebugEnabled()) { 167 log.debug("Get module name for path " + matchPath); 168 } 169 170 String prefix = ""; String prefixes[] = getModulePrefixes(context); 172 int lastSlash = 0; 175 while (prefix.equals("") 176 && ((lastSlash = matchPath.lastIndexOf("/")) > 0)) { 177 178 matchPath = matchPath.substring(0, lastSlash); 180 181 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 206 public String [] getModulePrefixes(ServletContext context) { 207 return (String []) context.getAttribute(Globals.MODULE_PREFIXES_KEY); 208 } 209 210 217 public void selectModule(HttpServletRequest request, ServletContext context) { 218 String prefix = getModuleName(request, context); 220 221 this.selectModule(prefix, request, context); 223 224 } 225 226 234 public void selectModule( 235 String prefix, 236 HttpServletRequest request, 237 ServletContext context) { 238 239 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 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 |