dgraham     2003/08/02 14:03:41

  Modified:    src/share/org/apache/struts/util RequestUtils.java
  Added:       src/share/org/apache/struts/util ModuleUtils.java
  Log:
  Moved module related methods to new ModuleUtils class.
  
  Revision  Changes    Path
  1.132     +26 -69    
jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java
  
  Index: RequestUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v
  retrieving revision 1.131
  retrieving revision 1.132
  diff -u -r1.131 -r1.132
  --- RequestUtils.java 2 Aug 2003 20:40:18 -0000       1.131
  +++ RequestUtils.java 2 Aug 2003 21:03:41 -0000       1.132
  @@ -90,7 +90,6 @@
   import org.apache.struts.action.ActionServletWrapper;
   import org.apache.struts.action.DynaActionForm;
   import org.apache.struts.action.DynaActionFormClass;
  -import org.apache.struts.action.RequestProcessor;
   import org.apache.struts.config.ActionConfig;
   import org.apache.struts.config.FormBeanConfig;
   import org.apache.struts.config.ForwardConfig;
  @@ -109,7 +108,6 @@
    * @author David Graham
    * @version $Revision$ $Date$
    */
  -
   public class RequestUtils {
   
       // ------------------------------------------------------- Static Variables
  @@ -1261,27 +1259,15 @@
        * @param request The servlet request we are processing
        * @param context The ServletContext for this web application
        * @since Struts 1.1
  +     * @deprecated Use ModuleUtils.selectModule() instead.  This will be
  +     * removed after Struts 1.2.
        */
       public static void selectModule(
           String prefix,
           HttpServletRequest request,
           ServletContext context) {
   
  -        // Expose the resources for this module
  -        ModuleConfig config = (ModuleConfig) 
context.getAttribute(Globals.MODULE_KEY + prefix);
  -        if (config != null) {
  -            request.setAttribute(Globals.MODULE_KEY, config);
  -        } else {
  -            request.removeAttribute(Globals.MODULE_KEY);
  -        }
  -        MessageResources resources =
  -            (MessageResources) context.getAttribute(Globals.MESSAGES_KEY + prefix);
  -        if (resources != null) {
  -            request.setAttribute(Globals.MESSAGES_KEY, resources);
  -        } else {
  -            request.removeAttribute(Globals.MESSAGES_KEY);
  -        }
  -
  +        ModuleUtils.getInstance().selectModule(prefix, request, context);
       }
   
       /**
  @@ -1290,13 +1276,11 @@
        *
        * @param request The servlet request we are processing
        * @param context The ServletContext for this web application
  +     * @deprecated Use ModuleUtils.selectModule() instead.  This will be
  +     * removed after Struts 1.2.
        */
       public static void selectModule(HttpServletRequest request, ServletContext 
context) {
  -        // Compute module name
  -        String prefix = getModuleName(request, context);
  -        // Expose the resources for this module
  -        selectModule(prefix, request, context);
  -
  +        ModuleUtils.getInstance().selectModule(request, context);
       }
   
       /**
  @@ -1304,16 +1288,11 @@
        * @param request The servlet request we are processing
        * @param context The ServletContext for this web application
        * @return The module prefix or ""
  +     * @deprecated Use ModuleUtils.getModuleName() instead.  This will be
  +     * removed after Struts 1.2.
        */
       public static String getModuleName(HttpServletRequest request, ServletContext 
context) {
  -
  -        // Acquire the path used to compute the module
  -        String matchPath = (String) 
request.getAttribute(RequestProcessor.INCLUDE_SERVLET_PATH);
  -
  -        if (matchPath == null) {
  -            matchPath = request.getServletPath();
  -        }
  -        return getModuleName( matchPath, context);
  +        return ModuleUtils.getInstance().getModuleName(request, context);
       }
   
       /**
  @@ -1321,34 +1300,11 @@
        * @param matchPath The uri from which we want the module name.
        * @param context The ServletContext for this web application
        * @return The module prefix or ""
  +     * @deprecated Use ModuleUtils.getModuleName() instead.  This will be
  +     * removed after Struts 1.2.
        */
       public static String getModuleName(String matchPath, ServletContext context) {
  -        if (log.isDebugEnabled()) {
  -            log.debug("Get module name for path " + matchPath);
  -        }
  -
  -        String prefix = ""; // Initialize prefix before we try lookup
  -        String prefixes[] = getModulePrefixes(context); // Get all other possible 
prefixes
  -        int lastSlash = 0; // Initialize before loop
  -
  -        while (prefix.equals("") && ((lastSlash = matchPath.lastIndexOf("/")) > 0)) 
{
  -
  -            // We may be in a non-default module.  Try to get it's prefix.
  -            matchPath = matchPath.substring(0, lastSlash);
  -
  -            // Match against the list of module prefixes
  -            for (int i = 0; i < prefixes.length; i++) {
  -                if (matchPath.equals(prefixes[i])) {
  -                    prefix = prefixes[i];
  -                    break;
  -                }
  -            }
  -        }
  -
  -        if (log.isDebugEnabled()) {
  -            log.debug("Module name found: " + (prefix.equals("") ? "default" : 
prefix));
  -        }
  -        return prefix;
  +        return ModuleUtils.getInstance().getModuleName(matchPath, context);
       }
   
       /**
  @@ -1363,9 +1319,11 @@
        * @return the ModuleConfig object from request, or null if none is set in
        * the request.
        * @since Struts 1.1
  +     * @deprecated Use ModuleUtils.getRequestModuleConfig() instead.  This will be
  +     * removed after Struts 1.2.
        */
       public static ModuleConfig getRequestModuleConfig( HttpServletRequest request) {
  -        return (ModuleConfig) request.getAttribute(Globals.MODULE_KEY);
  +        return ModuleUtils.getInstance().getRequestModuleConfig(request);
       }
   
       /**
  @@ -1374,17 +1332,14 @@
        * @param context The ServletContext for this web application
        * @return the ModuleConfig object
        * @since Struts 1.1
  +     * @deprecated Use ModuleUtils.getModuleConfig() instead.  This will be
  +     * removed after Struts 1.2.
        */
       public static ModuleConfig getModuleConfig(
           HttpServletRequest request,
           ServletContext context) {
  -            
  -        ModuleConfig moduleConfig = (ModuleConfig) 
request.getAttribute(Globals.MODULE_KEY);
  -        if (moduleConfig == null) {
  -            moduleConfig = (ModuleConfig) context.getAttribute(Globals.MODULE_KEY);
  -            request.setAttribute(Globals.MODULE_KEY, moduleConfig);
  -        }
  -        return moduleConfig;
  +        
  +        return ModuleUtils.getInstance().getModuleConfig(request, context);
       }
       
       /**
  @@ -1407,9 +1362,11 @@
        * @param context The ServletContext for this web application.
        * @return An array of module prefixes.
        * @since Struts 1.1
  +     * @deprecated Use ModuleUtils.getModulePrefixes() instead.  This will be
  +     * removed after Struts 1.2.
        */
       public static String[] getModulePrefixes(ServletContext context) {
  -        return (String[]) context.getAttribute(Globals.MODULE_PREFIXES_KEY);
  +        return ModuleUtils.getInstance().getModulePrefixes(context);
       }
   
       /**
  
  
  
  1.1                  jakarta-struts/src/share/org/apache/struts/util/ModuleUtils.java
  
  Index: ModuleUtils.java
  ===================================================================
  /*
   * $Header$
   * $Revision$
   * $Date$
   *
   * ====================================================================
   *
   * 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.util;
  
  import javax.servlet.ServletContext;
  import javax.servlet.http.HttpServletRequest;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.struts.Globals;
  import org.apache.struts.action.RequestProcessor;
  import org.apache.struts.config.ModuleConfig;
  
  /**
   * General purpose utility methods related to module processing.
   * 
   * @author Craig R. McClanahan
   * @author Ted Husted
   * @author James Turner
   * @author David Graham
   * @version $Revision$
   */
  public class ModuleUtils {
  
      /**
       * The Singleton instance.
       */
      private static final ModuleUtils instance = new ModuleUtils();
  
      /**
       * Commons logging instance.
       */
      private static final Log log = LogFactory.getLog(ModuleUtils.class);
  
      /**
       * The message resources for this package.
       */
      private static final MessageResources messages =
          MessageResources.getMessageResources("org.apache.struts.util.LocalStrings");
  
      /**
       * Returns the Singleton instance of TagUtils.
       */
      public static ModuleUtils getInstance() {
          return instance;
      }
  
      /**
       * Constructor for ModuleUtils.
       */
      protected ModuleUtils() {
          super();
      }
  
      /**
       * Return the ModuleConfig object is it exists, null otherwise.
       * @param request The servlet request we are processing
       * @param context The ServletContext for this web application
       * @return the ModuleConfig object
       */
      public ModuleConfig getModuleConfig(
          HttpServletRequest request,
          ServletContext context) {
  
          ModuleConfig moduleConfig = this.getRequestModuleConfig(request);
  
          if (moduleConfig == null) {
              moduleConfig = (ModuleConfig) context.getAttribute(Globals.MODULE_KEY);
              request.setAttribute(Globals.MODULE_KEY, moduleConfig);
          }
  
          return moduleConfig;
      }
  
      /**
       * Get the module name to which the specified request belong.
       * @param request The servlet request we are processing
       * @param context The ServletContext for this web application
       * @return The module prefix or ""
       */
      public String getModuleName(
          HttpServletRequest request,
          ServletContext context) {
  
          // Acquire the path used to compute the module
          String matchPath =
              (String) request.getAttribute(RequestProcessor.INCLUDE_SERVLET_PATH);
  
          if (matchPath == null) {
              matchPath = request.getServletPath();
          }
  
          return this.getModuleName(matchPath, context);
      }
  
      /**
       * Get the module name to which the specified uri belong.
       * @param matchPath The uri from which we want the module name.
       * @param context The ServletContext for this web application
       * @return The module prefix or ""
       */
      public String getModuleName(String matchPath, ServletContext context) {
          if (log.isDebugEnabled()) {
              log.debug("Get module name for path " + matchPath);
          }
  
          String prefix = ""; // Initialize prefix before we try lookup
          String prefixes[] = getModulePrefixes(context);
          // Get all other possible prefixes
          int lastSlash = 0; // Initialize before loop
  
          while (prefix.equals("")
              && ((lastSlash = matchPath.lastIndexOf("/")) > 0)) {
  
              // We may be in a non-default module.  Try to get it's prefix.
              matchPath = matchPath.substring(0, lastSlash);
  
              // Match against the list of module prefixes
              for (int i = 0; i < prefixes.length; i++) {
                  if (matchPath.equals(prefixes[i])) {
                      prefix = prefixes[i];
                      break;
                  }
              }
          }
  
          if (log.isDebugEnabled()) {
              log.debug(
                  "Module name found: " + (prefix.equals("") ? "default" : prefix));
          }
  
          return prefix;
      }
  
      /**
       * Return the list of module prefixes that are defined for
       * this web application.  <strong>NOTE</strong> -
       * the "" prefix for the default module is not included in this list.
       *
       * @param context The ServletContext for this web application.
       * @return An array of module prefixes.
       */
      public String[] getModulePrefixes(ServletContext context) {
          return (String[]) context.getAttribute(Globals.MODULE_PREFIXES_KEY);
      }
  
      /**
       * Return the current ModuleConfig object stored in request, if it exists,
       * null otherwise.
       * This method can be used by plugin to retrieve the current module config
       * object. If no moduleConfig is found, this means that the request haven't
       * hit the server throught the struts servlet. The appropriate module config
       * can be set and found with
       * <code>[EMAIL PROTECTED] RequestUtils#selectModule(HttpServletRequest, 
ServletContext)} </code>.
       * @param request The servlet request we are processing
       * @return the ModuleConfig object from request, or null if none is set in
       * the request.
       */
      public ModuleConfig getRequestModuleConfig(HttpServletRequest request) {
          return (ModuleConfig) request.getAttribute(Globals.MODULE_KEY);
      }
  
      /**
       * Select the module to which the specified request belongs, and
       * add corresponding request attributes to this request.
       *
       * @param request The servlet request we are processing
       * @param context The ServletContext for this web application
       */
      public void selectModule(HttpServletRequest request, ServletContext context) {
          // Compute module name
          String prefix = getModuleName(request, context);
  
          // Expose the resources for this module
          this.selectModule(prefix, request, context);
  
      }
  
      /**
       * Select the module to which the specified request belongs, and
       * add corresponding request attributes to this request.
       *
       * @param prefix The module prefix of the desired module
       * @param request The servlet request we are processing
       * @param context The ServletContext for this web application
       */
      public void selectModule(
          String prefix,
          HttpServletRequest request,
          ServletContext context) {
  
          // Expose the resources for this module
          ModuleConfig config =
              (ModuleConfig) context.getAttribute(Globals.MODULE_KEY + prefix);
  
          if (config != null) {
              request.setAttribute(Globals.MODULE_KEY, config);
          } else {
              request.removeAttribute(Globals.MODULE_KEY);
          }
  
          MessageResources resources =
              (MessageResources) context.getAttribute(Globals.MESSAGES_KEY + prefix);
  
          if (resources != null) {
              request.setAttribute(Globals.MESSAGES_KEY, resources);
          } else {
              request.removeAttribute(Globals.MESSAGES_KEY);
          }
  
      }
  
  }
  
  
  

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

Reply via email to