1 18 19 package org.apache.struts.actions; 20 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Locale ; 24 import java.util.Map ; 25 26 import javax.servlet.ServletException ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.apache.struts.Globals; 31 import org.apache.struts.action.ActionForm; 32 import org.apache.struts.action.ActionForward; 33 import org.apache.struts.action.ActionMapping; 34 import org.apache.struts.config.MessageResourcesConfig; 35 import org.apache.struts.config.ModuleConfig; 36 import org.apache.struts.util.MessageResources; 37 38 109 public abstract class LookupDispatchAction extends DispatchAction { 110 111 114 protected Map localeMap = new HashMap (); 115 116 119 protected Map keyMethodMap = null; 120 121 123 137 public ActionForward execute( 138 ActionMapping mapping, 139 ActionForm form, 140 HttpServletRequest request, 141 HttpServletResponse response) 142 throws Exception { 143 144 if (isCancelled(request)) { 145 ActionForward af = cancelled(mapping, form, request, response); 146 if (af != null) { 147 return af; 148 } 149 } 150 String parameter = mapping.getParameter(); 152 if (parameter == null) { 153 String message = messages.getMessage("dispatch.handler", mapping.getPath()); 154 throw new ServletException (message); 155 } 156 157 String methodName = getMethodName(mapping, form, request, response, parameter); 159 160 return dispatchMethod(mapping, form, request, response, methodName); 161 162 } 163 164 169 private Map initLookupMap(HttpServletRequest request, Locale userLocale) { 170 Map lookupMap = new HashMap (); 171 this.keyMethodMap = this.getKeyMethodMap(); 172 173 ModuleConfig moduleConfig = 174 (ModuleConfig) request.getAttribute(Globals.MODULE_KEY); 175 176 MessageResourcesConfig[] mrc = moduleConfig.findMessageResourcesConfigs(); 177 178 for (int i = 0; i < mrc.length; i++) { 180 MessageResources resources = this.getResources(request, mrc[i].getKey()); 181 182 Iterator iter = this.keyMethodMap.keySet().iterator(); 184 while (iter.hasNext()) { 185 String key = (String ) iter.next(); 186 String text = resources.getMessage(userLocale, key); 187 188 if ((text != null) && !lookupMap.containsKey(text)) { 190 lookupMap.put(text, key); 191 } 192 } 193 } 194 195 return lookupMap; 196 } 197 198 203 protected abstract Map getKeyMethodMap(); 204 205 216 protected String getLookupMapName( 217 HttpServletRequest request, 218 String keyName, 219 ActionMapping mapping) 220 throws ServletException { 221 222 Map lookupMap = null; 224 225 synchronized(localeMap) { 226 Locale userLocale = this.getLocale(request); 227 lookupMap = (Map ) this.localeMap.get(userLocale); 228 229 if (lookupMap == null) { 230 lookupMap = this.initLookupMap(request, userLocale); 231 this.localeMap.put(userLocale, lookupMap); 232 } 233 } 234 235 String key = (String ) lookupMap.get(keyName); 237 if (key == null) { 238 String message = messages.getMessage( 239 "dispatch.resource", mapping.getPath(), keyName); 240 throw new ServletException (message); 241 } 242 243 String methodName = (String ) keyMethodMap.get(key); 245 if (methodName == null) { 246 String message = messages.getMessage( 247 "dispatch.lookup", mapping.getPath(), key); 248 throw new ServletException (message); 249 } 250 251 return methodName; 252 } 253 254 266 protected String getMethodName( 267 ActionMapping mapping, 268 ActionForm form, 269 HttpServletRequest request, 270 HttpServletResponse response, 271 String parameter) 272 throws Exception { 273 274 String keyName = request.getParameter(parameter); 277 if (keyName == null || keyName.length() == 0) { 278 return null; 279 } 280 281 String methodName = getLookupMapName(request, keyName, mapping); 282 283 return methodName; 284 } 285 286 287 } 288 | Popular Tags |