1 64 65 package com.jcorporate.expresso.core.controller; 66 67 import com.jcorporate.expresso.core.misc.ConfigManager; 68 import com.jcorporate.expresso.core.misc.ConfigurationException; 69 import com.jcorporate.expresso.core.misc.SystemMacros; 70 import org.apache.commons.digester.Digester; 71 import org.apache.struts.Globals; 72 import org.apache.struts.action.Action; 73 import org.apache.struts.action.ActionFormBean; 74 import org.apache.struts.action.ActionFormBeans; 75 import org.apache.struts.action.ActionForward; 76 import org.apache.struts.action.ActionForwards; 77 import org.apache.struts.action.ActionMapping; 78 import org.apache.struts.action.ActionMappings; 79 import org.apache.struts.action.ActionServlet; 80 import org.apache.struts.action.DynaActionFormClass; 81 import org.apache.struts.config.ActionConfig; 82 import org.apache.struts.config.ControllerConfig; 83 import org.apache.struts.config.FormBeanConfig; 84 import org.apache.struts.config.ForwardConfig; 85 import org.apache.struts.config.MessageResourcesConfig; 86 import org.apache.struts.config.ModuleConfig; 87 import org.apache.struts.config.ModuleConfigFactory; 88 import org.xml.sax.InputSource ; 89 90 import javax.servlet.ServletException ; 91 import javax.servlet.UnavailableException ; 92 import java.io.IOException ; 93 import java.io.InputStream ; 94 import java.net.URL ; 95 import java.util.Enumeration ; 96 import java.util.HashMap ; 97 import java.util.Iterator ; 98 import java.util.Map ; 99 100 101 114 public class ExpressoActionServlet extends ActionServlet 115 implements ControllerFactory { 116 117 protected Map mapModuleConfig = new HashMap (); 118 119 122 public ExpressoActionServlet() { 123 } 124 125 147 protected ModuleConfig initModuleConfig(String prefix, String paths) 148 throws ServletException { 149 ModuleConfig config = initExpressoFromConfigDirectory(prefix, paths); 154 if (config != null) { 155 synchronized (mapModuleConfig) { 158 mapModuleConfig.put(prefix, config); 159 } 160 return config; 161 } 162 163 168 config = super.initModuleConfig(prefix, paths); 169 170 synchronized (mapModuleConfig) { 173 mapModuleConfig.put(prefix, config); 174 } 175 176 if (prefix.length() < 1) { 177 178 Enumeration names = getServletConfig().getInitParameterNames(); 182 while (names.hasMoreElements()) { 183 String name = (String ) names.nextElement(); 184 if (log.isDebugEnabled()) { 185 log.debug("init parameter name=`" + name + "'"); 186 } 187 if (name.startsWith("expresso/")) { 188 String subapp = name.substring(8); 190 String configPath = getServletConfig().getInitParameter(name); 191 initExpressoSubapplication(subapp, config, configPath); 192 } 193 } 194 195 } 196 197 198 return (config); 201 } 202 203 204 215 protected ModuleConfig initExpressoFromConfigDirectory(String prefix, String paths) 216 throws ServletException { 217 218 ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory(); 221 ModuleConfig config = factoryObject.createModuleConfig(prefix); 222 config.setPrefix(""); 223 224 String mapping = getServletConfig().getInitParameter("mapping"); 226 if (mapping != null) { 227 config.setActionMappingClass(mapping); 228 } 229 230 Digester digester = initConfigDigester(); 232 233 234 if (log.isDebugEnabled()) { 235 log.debug("initExpressoFromConfigDirectory()"); 236 } 237 String expressoConfigDir = this.getServletContext().getInitParameter("configDir"); 242 if (expressoConfigDir != null && 243 expressoConfigDir.length() > 0 && prefix.length() == 0) { 244 expressoConfigDir = SystemMacros.getInstance().expandValue(expressoConfigDir); 245 if (expressoConfigDir.startsWith("WEB-INF")) { 246 expressoConfigDir = SystemMacros.getInstance().getWebAppDir() + "/" + expressoConfigDir; 247 } 248 249 java.io.File f = new java.io.File (expressoConfigDir + "/struts-config.xml"); 250 if (f != null && f.exists()) { 251 paths = expressoConfigDir + "/struts-config.xml"; 252 } else { 253 return null; 258 } 259 } else { 260 return null; 265 } 266 267 HashMap inputMap = null; 271 272 try { 273 inputMap = ConfigManager.getConfigInputSources("struts-config"); 274 } catch (ConfigurationException ce) { 275 log.info("Unable to load struts-config doctypes. Relying on web.xml parameters", ce); 276 return null; 277 } 278 279 if (inputMap == null) { 280 log.info("Unable to load struts-config doctypes. Relying on web.xml parameters"); 281 return null; 282 } 283 284 if (!inputMap.containsKey("struts-config.xml")) { 285 log.info("Unable to load struts-config.xml. Relying on web.xml parameters"); 286 return null; 287 } 288 289 InputSource is = (InputSource ) inputMap.get("struts-config.xml"); 293 digester.push(config); 295 loadOneConfigFile(digester, is); 296 297 298 for (Iterator i = inputMap.keySet().iterator(); i.hasNext();) { 302 String key = (String ) i.next(); 303 if (log.isInfoEnabled()) { 304 log.info("Loading struts configuration file: " + key); 305 } 306 if (!("struts-config.xml".equals(key))) { 307 digester = null; 308 digester = initConfigDigester(); 309 digester.push(config); 310 is = (InputSource ) inputMap.get(key); 311 loadOneConfigFile(digester, is); 312 } 313 } 314 315 316 FormBeanConfig fbs[] = config.findFormBeanConfigs(); 319 for (int i = 0; i < fbs.length; i++) { 320 if (fbs[i].getDynamic()) { 321 DynaActionFormClass.createDynaActionFormClass(fbs[i]); 322 } 323 } 324 325 getServletContext().setAttribute(Globals.MODULE_KEY + prefix, config); 326 327 if (prefix.length() < 1) { 330 defaultControllerConfig(config); 331 defaultMessageResourcesConfig(config); 332 defaultFormBeansConfig(config); 333 defaultForwardsConfig(config); 334 defaultMappingsConfig(config); 335 } 336 337 338 return config; 340 341 } 342 343 350 protected void loadOneConfigFile(Digester digester, InputSource is) 351 throws ServletException { 352 try { 353 digester.parse(is); 354 } catch (Exception ex) { 355 log.error(internal.getMessage("configParse", ex)); 356 throw new UnavailableException 357 (internal.getMessage("configParse", ex)); 358 } 359 360 } 361 362 363 372 protected void initExpressoSubapplication(String prefix, ModuleConfig config, String configPath) 373 throws ServletException { 374 if (log.isDebugEnabled()) { 375 log.debug("initExpressoSubapplication()"); 376 } 377 378 InputStream input = null; 379 try { 380 Digester digester = initConfigDigester(); 382 383 if (log.isDebugEnabled()) { 384 log.debug("prefix=`" + prefix + "'"); 385 log.debug("config=`" + config + "'"); 386 log.debug("configPath=`" + configPath + "'"); 387 } 388 389 digester.push(config); 391 URL url = getServletContext().getResource(configPath); 392 if (log.isDebugEnabled()) { 393 log.debug("url=`" + url + "'"); 394 } 395 InputSource is = null; 396 if (url == null) { 397 } else { 399 is = new InputSource (url.toExternalForm()); 400 input = getServletContext().getResourceAsStream(configPath); 401 is.setByteStream(input); 402 } 403 404 digester.parse(is); 405 } catch (Throwable t) { 406 log.error(internal.getMessage("configParse", configPath, t)); 407 throw new UnavailableException 408 (internal.getMessage("configParse", configPath)); 409 } finally { 410 if (input != null) { 411 try { 412 input.close(); 413 } catch (IOException e) { 414 ; 415 } 416 } 417 } 418 } 419 420 428 public synchronized Controller getController(String className) 429 throws ControllerException { 430 return getController("", className); 431 } 432 433 434 443 public synchronized Controller getController(String moduleName, String className) 444 throws ControllerException { 445 ModuleConfig config = null; 448 449 synchronized (mapModuleConfig) { 450 config = (ModuleConfig) mapModuleConfig.get(moduleName); 451 } 452 if (config == null) { 453 throw new ControllerException("unable to find specified Struts module 1.1 prefix:`" + moduleName + "'"); 454 } 455 456 ExpressoRequestProcessor processor = null; 457 try { 458 processor = (ExpressoRequestProcessor) getRequestProcessor(config); 459 } 460 catch (ServletException se) { 461 throw new ControllerException( "Unable to retrieve request processor from module config.", se ); 462 } catch (ClassCastException ex) { 463 throw new ControllerException("You need to set the request processor in your configuration for module: " 464 + moduleName + " to point to a derivative of " + ExpressoRequestProcessor.class.getName() 465 + " instead we got: " + ex.getMessage()); 466 } 467 468 Action action = processor.createAction(className); 469 if (!(action instanceof Controller)) { 470 throw new ControllerException( 471 "className:`" + className + "' is not a subclass of `" 472 + Controller.class.getName() + "'"); 473 } 474 Controller controller = (Controller) action; 475 return controller; 476 } 477 478 488 public Controller getController(ControllerRequest request) 489 throws ControllerException { 490 493 496 497 String className = request.getParameter(Controller.CONTROLLER_PARAM_KEY); 498 499 if (className == null || className.length() == 0) { 500 throw new IllegalArgumentException ("You must define your controller " + 501 "parameter in your controller request"); 502 } 503 if (request instanceof ServletControllerRequest) { 504 ServletControllerRequest scr = (ServletControllerRequest) request; 505 javax.servlet.Servlet as = scr.getCallingServlet(); 506 507 if (as instanceof ControllerFactory) { 508 if (as != this) { 509 return ((ControllerFactory) as).getController(className); 510 } else { 511 return this.getController(className); 512 } 513 } else { 514 515 return this.getController(className); 517 } 518 } 519 520 return this.getController(className); 521 } 522 523 528 public void init() 529 throws javax.servlet.ServletException { 530 ConfigManager.setControllerFactory(this); 531 super.init(); 532 } 533 534 537 public void destroy() { 538 ConfigManager.setControllerFactory(null); 539 super.destroy(); 540 } 541 542 557 575 576 585 private void defaultControllerConfig(ModuleConfig config) { 586 587 String value = null; 588 ControllerConfig cc = config.getControllerConfig(); 589 590 value = getServletConfig().getInitParameter("bufferSize"); 591 if (value != null) { 592 cc.setBufferSize(Integer.parseInt(value)); 593 } 594 595 value = getServletConfig().getInitParameter("content"); 596 if (value != null) { 597 cc.setContentType(value); 598 } 599 600 value = getServletConfig().getInitParameter("locale"); 601 if (value != null) { 603 if ("true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value)) { 604 cc.setLocale(true); 605 } else { 606 cc.setLocale(false); 607 } 608 } 609 610 value = getServletConfig().getInitParameter("maxFileSize"); 611 if (value != null) { 612 cc.setMaxFileSize(value); 613 } 614 615 value = getServletConfig().getInitParameter("nocache"); 616 if (value != null) { 617 if ("true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value)) { 618 cc.setNocache(true); 619 } else { 620 cc.setNocache(false); 621 } 622 } 623 624 value = getServletConfig().getInitParameter("multipartClass"); 625 if (value != null) { 626 cc.setMultipartClass(value); 627 } 628 629 value = getServletConfig().getInitParameter("tempDir"); 630 if (value != null) { 631 cc.setTempDir(value); 632 } 633 634 } 635 636 637 647 private void defaultFormBeansConfig(ModuleConfig config) { 648 649 FormBeanConfig fbcs[] = config.findFormBeanConfigs(); 650 ActionFormBeans afb = new ActionFormBeans(); 651 afb.setFast(false); 652 for (int i = 0; i < fbcs.length; i++) { 653 afb.addFormBean((ActionFormBean) fbcs[i]); 654 } 655 afb.setFast(true); 656 getServletContext().setAttribute(Globals.FORM_BEANS_KEY, afb); 657 658 } 659 660 661 671 private void defaultForwardsConfig(ModuleConfig config) { 672 673 ForwardConfig fcs[] = config.findForwardConfigs(); 674 ActionForwards af = new ActionForwards(); 675 af.setFast(false); 676 for (int i = 0; i < fcs.length; i++) { 677 af.addForward((ActionForward) fcs[i]); 678 } 679 af.setFast(true); 680 getServletContext().setAttribute(Globals.FORWARDS_KEY, af); 681 682 } 683 684 685 695 private void defaultMappingsConfig(ModuleConfig config) { 696 697 ActionConfig acs[] = config.findActionConfigs(); 698 ActionMappings am = new ActionMappings(); 699 am.setServlet(this); 700 am.setFast(false); 701 for (int i = 0; i < acs.length; i++) { 702 am.addMapping((ActionMapping) acs[i]); 703 } 704 am.setFast(true); 705 getServletContext().setAttribute(Globals.MAPPINGS_KEY, am); 706 707 } 708 709 710 719 private void defaultMessageResourcesConfig(ModuleConfig config) { 720 721 String value = null; 722 723 MessageResourcesConfig mrc = 724 config.findMessageResourcesConfig(Globals.MESSAGES_KEY); 725 if (mrc == null) { 726 mrc = new MessageResourcesConfig(); 727 mrc.setKey(Globals.MESSAGES_KEY); 728 config.addMessageResourcesConfig(mrc); 729 } 730 value = getServletConfig().getInitParameter("application"); 731 if (value != null) { 732 mrc.setParameter(value); 733 } 734 value = getServletConfig().getInitParameter("factory"); 735 if (value != null) { 736 mrc.setFactory(value); 737 } 738 value = getServletConfig().getInitParameter("null"); 739 if (value != null) { 740 if (value.equalsIgnoreCase("true") || 741 value.equalsIgnoreCase("yes")) { 742 mrc.setNull(true); 743 } else { 744 mrc.setNull(false); 745 } 746 } 747 748 } 749 750 } 751 | Popular Tags |