Uploaded image for project: 'Struts 1'
  1. Struts 1
  2. STR-2904

Validator plugin should fail-fast when loaded twice

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 1.2.9
    • 1.4.0
    • Core
    • None
    • System: Windows XP (Version 2002 - Service Pack 2)
      App Server: Tomcat 5.5.9
      Struts Version: 1.2.9
    • Patch

    Description

      There is a feature in the Struts (1.x) that allows the user to define multiples struts configuration files (struts-config.xml) without use modules. To do this, the user must define a list of files separated by comma in the "config" parameter inside the web.xml file, as you can see:

      <servlet>
      <servlet-name>action</servlet-name>
      <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
      <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml,/WEB-INF/struts-config-2.xml,/WEB-INF/struts-config-3.xml,/WEB-INF/struts-config-4.xml</param-value>
      </init-param>
      ...
      </servlet>

      So here comes the problem. If the user defines for each of them a separated validation configuration file, i.e., the file /WEB-INF/struts-config.xml has:

      ...
      <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
      <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validator.xml" />
      </plug-in>
      ...

      And the file /WEB-INF/struts-config-2.xml has:

      ...
      <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
      <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validator-2.xml" />
      </plug-in>
      ...

      And so on. Then, after the bootstrap, only the last validation configuration file was loaded. So, all others files was discarted. This occurs because after each initialization of the struts config file, the ValidatorPlugin reset the resources in the servlet context. As you can see in the follow piece of code:

      ...
      this.initResources();
      servlet.getServletContext().setAttribute(VALIDATOR_KEY + config.getPrefix(), resources);
      ...

      I guess to solve this problem we first need to verify if the resources stored in the servlet context has any value. If yes, instead of reset the new value, we have to add the constants, formsets and actions (from the new resouce readed) to previous resources stored.

      Something like this:

      ...
      this.initResources();
      ValidatorResources resources = (ValidatorResources) servlet.getServletContext().getAttribute(VALIDATOR_KEY + config.getPrefix());
      if (resources != null) {
      // Copy constants from previous resources.
      for (Iterator i = this.resources.getConstants().keySet().iterator(); i.hasNext()

      { String name = (String) i.next(); resources.addConstant(name, (String) this.resources.getConstants().get(name)); }

      // Copy validator actions from previous resources.
      for (Iterator i = this.resources.getActions().values().iterator(); i.hasNext()

      { ValidatorAction action = (ValidatorAction) i.next(); resources.addValidatorAction(action); }

      // Copy form sets from previous resources.
      for (Iterator i = this.resources.getFormSets().values().iterator(); i.hasNext()

      { FormSet formSet = (FormSet) i.next(); resources.addFormSet(formSet); }

      this.resources = resources;
      }
      servlet.getServletContext().setAttribute(VALIDATOR_KEY + config.getPrefix(), this.resources);
      servlet.getServletContext().setAttribute(STOP_ON_ERROR_KEY + '.' + config.getPrefix(), (this.stopOnFirstError ? Boolean.TRUE : Boolean.FALSE));
      ...

      But here comes another problem. The methods [getFormSets(), getActions(), getConstants()] are protected. So to do this we have to:

      1) Put those methods as public. But they are in another apache project.
      2) Create a subclass of ValidatorResources exposing other three methods that are public and return each specified value. One for form set, other for validation action and other for constant.
      3) Think in another approach.

      Felipe.

      Attachments

        Issue Links

          Activity

            People

              pbenedict Paul Benedict
              desiderati Felipe Desiderati e Souza
              Votes:
              1 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: