craigmcc    2003/01/06 22:14:59

  Modified:    src/share/org/apache/struts/action
                        ActionResources.properties
               web/example/WEB-INF struts-config.xml
  Added:       src/share/org/apache/struts/plugins
                        ModuleConfigVerifier.java
  Log:
  The beginnings of a standard PlugIn that attempts to verify the configuration
  information encapsulated in the ModuleConfig for the current module.  It logs
  any error reports it finds via ServletContext.log(), and optionally (but
  does by default) throws an initialization exception if any error was actually
  found.
  
  The standard struts-example webapp has been modified to register this PlugIn.
  
  Known Limitation:  Checking things here will not catch some kinds of errors
  like defining two <forward> elements with the same "name" value.  We'll need
  some additional help (perhaps in the addFoo() methods of ModuleConfig) to
  catch that kind of thing.
  
  This PlugIn is at least partially in response to Bugzilla #14749, which
  reports something that really is pilot error (incorrect value in the
  configuration file), but is something that is hard for a newbie to track
  down.  We need to reduce the number of errors that are hard to trace.
  
  Revision  Changes    Path
  1.11      +12 -0     
jakarta-struts/src/share/org/apache/struts/action/ActionResources.properties
  
  Index: ActionResources.properties
  ===================================================================
  RCS file: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/action/ActionResources.properties,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ActionResources.properties        22 Jun 2002 20:39:48 -0000      1.10
  +++ ActionResources.properties        7 Jan 2003 06:14:59 -0000       1.11
  @@ -1,11 +1,14 @@
   actionCreate=No action instance for path {0} could be created
   applicationLoading=Loading application resources from resource {0}
   applicationResources=Cannot load application resources bundle {0}
  +configCompleted=Verification of ModuleConfig has been completed
  +configFatal=Fatal module configuration error, see previous messages
   configInit=Initializing configuration from resource path {0}
   configIO=Input/output error reading configuration from resource path {0}
   configMapping=Configuring mapping for path {0}
   configMissing=Missing configuration resource for path {0}
   configParse=Parsing error processing resource path {0}
  +configVerifying=Verifying ModuleConfig for this application module
   dataSource.destroy=Finalizing application data source {0}
   dataSource.init=Initializing application data source {0}
   destroyDataSource=Exception destroying application data source {0}
  @@ -22,3 +25,12 @@
   requestDispatcher=Cannot get request dispatcher for path {0}
   sessionCreate=No user session could be created
   unhandledException=Unhandled Exception thrown: {0}
  +verifyActionMappingClass.invalid=Invalid ActionMapping class {0} specified
  +verifyActionMappingClass.missing=No ActionMapping implementation class specified
  +verifyForwardConfig.invalid=Invalid path {0} for ForwardConfig {1}
  +verifyForwardConfig.missing=Missing path for ForwardConfig {0}
  +verifyMessageResourcesConfigs.invalid=Invalid factory class {0} for 
MessageResourcesConfig
  +verifyMessageResourcesConfigs.key=No key attribute for MessageResourcesConfig
  +verifyMessageResourcesConfigs.missing=No factory attribute for 
MessageResourcesConfig
  +verifyPlugInConfigs.invalid=Invalid className {0} for PlugInConfig
  +verifyPlugInConfigs.missing=No className attribute for PlugInConfig
  
  
  
  1.1                  
jakarta-struts/src/share/org/apache/struts/plugins/ModuleConfigVerifier.java
  
  Index: ModuleConfigVerifier.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/plugins/ModuleConfigVerifier.java,v
 1.1 2003/01/07 06:14:59 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2003/01/07 06:14:59 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Struts", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.struts.plugins;
  
  
  import javax.servlet.ServletException;
  import org.apache.struts.action.ActionServlet;
  import org.apache.struts.action.PlugIn;
  import org.apache.struts.config.ForwardConfig;
  import org.apache.struts.config.MessageResourcesConfig;
  import org.apache.struts.config.ModuleConfig;
  import org.apache.struts.config.PlugInConfig;
  import org.apache.struts.util.RequestUtils;
  
  
  /**
   * <p>Convenient implementation of {@link PlugIn} that performs as many
   * verification tests on the information stored in the {@link ModuleConfig}
   * for this application module as is practical.  Based on the setting of the
   * <code>fatal</code> property (which defaults to <code>true</code>), the
   * detection of any such errors will cause a <code>ServletException</code>
   * to be thrown from the <code>init()</code> method, which will ultimately
   * cause the initialization of your Struts controller servlet to fail.</p>
   *
   * <p>Under all circumstances, errors that are detected will be logged via
   * calls to <code>ServletContext.log()</code>.</p>
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2003/01/07 06:14:59 $
   * @since Struts 1.1-b4
   */
  
  public class ModuleConfigVerifier implements PlugIn {
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * <p>The {@link ModuleConfig} instance for our application module.</p>
       */
      protected ModuleConfig config = null;
  
  
      /**
       * <p>The {@link ActionServlet} instance we are associated with.</p>
       */
      protected ActionServlet servlet = null;
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * <p>Should the existence of configuration errors be fatal.</p>
       */
      private boolean fatal = true;
  
  
      /**
       * <p>Return the "configuation errors are fatal" flag.</p>
       */
      public boolean isFatal() {
  
          return (this.fatal);
  
      }
  
  
      /**
       * <p>Set the "configuration errors are fatal" flag.</p>
       *
       * @param fatal The new flag value
       */
      public void setFatal(boolean fatal) {
  
          this.fatal = fatal;
  
      }
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * <p>Receive notification that our owning module is being
       * shut down.</p>
       */
      public void destroy() {
  
          ; // No action required
  
      }
  
  
      /**
       * <p>Receive notification that the specified module is being
       * started up.</p>
       *
       * @param servlet ActionServlet that is managing all the
       *  modules in this web application
       * @param config ModuleConfig for the module with which
       *  this plug-in is associated
       *
       * @exception ServletException if this <code>PlugIn</code> cannot
       *  be successfully initialized
       */
      public void init(ActionServlet servlet, ModuleConfig config)
          throws ServletException {
  
          this.servlet = servlet;
          this.config = config;
          boolean ok = true;
          log(servlet.getInternal().getMessage("configVerifying"));
  
          // Perform detailed validations of each portion of ModuleConfig
          /*
          if (!verifyActionConfigs()) {
              ok = false;
          }
          */
          if (!verifyActionMappingClass()) {
              ok = false;
          }
          /*
          if (!verifyControllerConfig()) {
              ok = false;
          }
          if (!verifyDataSourceConfigs()) {
              ok = false;
          }
          if (!verifyExceptionConfigs()) {
              ok = false;
          }
          if (!verifyFormBeanConfigs()) {
              ok = false;
          }
          */
          if (!verifyForwardConfigs()) {
              ok = false;
          }
          if (!verifyMessageResourcesConfigs()) {
              ok = false;
          }
          if (!verifyPlugInConfigs()) {
              ok = false;
          }
  
          // Throw an exception on a fatal error
          log(servlet.getInternal().getMessage("configCompleted"));
          if (!ok && isFatal()) {
              throw new ServletException
                  (servlet.getInternal().getMessage("configFatal"));
          }
  
  
      }
  
  
  
      // ------------------------------------------------------ Protected Methods
  
  
      /**
       * <p>Log the specified message to our servlet context log, after a
       * header including the module prefix.</p>
       *
       * @param message The message to be logged
       */
      protected void log(String message) {
  
          String output = "[" + config.getPrefix() + "]: " + message;
          servlet.log(output);
  
      }
  
  
      /**
       * <p>Return <code>true</code> if information returned by
       * <code>config.getActionMappingClass()</code> is all valid;
       * otherwise, log error messages and return <code>false</code>.</p>
       */
      protected boolean verifyActionMappingClass() {
  
          String amcName = config.getActionMappingClass();
          if (amcName == null) {
              log(servlet.getInternal().getMessage
                  ("verifyActionMappingClass.missing"));
              return (false);
          }
          try {
              Class amcClass = RequestUtils.applicationClass(amcName);
          } catch (ClassNotFoundException e) {
              log(servlet.getInternal().getMessage
                  ("verifyActionMappingClass.invalid", amcName));
              return (false);
          }
          return (true);
  
      }
  
  
      /**
       * <p>Return <code>true</code> if information returned by
       * <code>config.findForwardConfigs() is all valid;
       * otherwise, log error messages and return <code>false</code>.</p>
       */
      protected boolean verifyForwardConfigs() {
  
          boolean ok = true;
          ForwardConfig fcs[] = config.findForwardConfigs();
          for (int i = 0; i < fcs.length; i++) {
              String path = fcs[i].getPath();
              if (path == null) {
                  log(servlet.getInternal().getMessage
                      ("verifyForwardConfigs.missing",
                       fcs[i].getName()));
                  ok = false;
              } else if (!path.startsWith("/")) {
                  log(servlet.getInternal().getMessage
                      ("verifyForwardConfigs.invalid", path,
                       fcs[i].getName()));
              }
          }
          return (ok);
  
      }
  
  
      /**
       * <p>Return <code>true</code> if information returned by
       * <code>config.findMessageResourcesConfigs() is all valid;
       * otherwise, log error messages and return <code>false</code>.</p>
       */
      protected boolean verifyMessageResourcesConfigs() {
  
          boolean ok = true;
          MessageResourcesConfig mrcs[] = config.findMessageResourcesConfigs();
          for (int i = 0; i < mrcs.length; i++) {
              String factory = mrcs[i].getFactory();
              if (factory == null) {
                  log(servlet.getInternal().getMessage
                      ("verifyMessageResourcesConfigs.missing"));
                  ok = false;
              } else {
                  try {
                      Class clazz = RequestUtils.applicationClass(factory);
                  } catch (ClassNotFoundException e) {
                      log(servlet.getInternal().getMessage
                          ("verifyMessageResourcesConfigs.invalid",
                           factory));
                      ok = false;
                  }
              }
              String key = mrcs[i].getKey();
              if (key == null) {
                  log(servlet.getInternal().getMessage
                      ("verifyMessageResourcesConfigs.key"));
              }
          }
          return (ok);
  
      }
  
  
      /**
       * <p>Return <code>true</code> if information returned by
       * <code>config.findPluginConfigs() is all valid;
       * otherwise, log error messages and return <code>false</code>.</p>
       */
      protected boolean verifyPlugInConfigs() {
  
          boolean ok = true;
          PlugInConfig pics[] = config.findPlugInConfigs();
          for (int i = 0; i < pics.length; i++) {
              String className = pics[i].getClassName();
              if (className == null) {
                  log(servlet.getInternal().getMessage
                      ("verifyPlugInConfigs.missing"));
                  ok = false;
              } else {
                  try {
                      Class clazz = RequestUtils.applicationClass(className);
                  } catch (ClassNotFoundException e) {
                      log(servlet.getInternal().getMessage
                          ("verifyPlugInConfigs.invalid", className));
                      ok = false;
                  }
              }
          }
          return (ok);
  
      }
  
  
  }
  
  
  
  1.28      +2 -0      jakarta-struts/web/example/WEB-INF/struts-config.xml
  
  Index: struts-config.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/web/example/WEB-INF/struts-config.xml,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- struts-config.xml 4 Nov 2002 06:27:59 -0000       1.27
  +++ struts-config.xml 7 Jan 2003 06:14:59 -0000       1.28
  @@ -169,6 +169,8 @@
   
     <!-- ========== Plug Ins Configuration ================================== -->
   
  +  <plug-in className="org.apache.struts.plugins.ModuleConfigVerifier"/>
  +
     <plug-in className="org.apache.struts.webapp.example.memory.MemoryDatabasePlugIn">
       <set-property property="pathname" value="/WEB-INF/database.xml"/>
     </plug-in>
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to