husted      2002/11/23 11:09:19

  Modified:    contrib/scaffold/src/java/org/apache/struts/scaffold
                        BaseForm.java BaseAction.java
  Log:
  + BizFormImpl: Initial version, reusing code from ProcessAction. Interface and 
BizAction to follow.
  + BaseForm: Add createHelper method (from BaseAction)
  + ProcessDispatchAction: Add support for an optional parameter.
  + ProcessAction: Pass up remoteNode.
  
  Revision  Changes    Path
  1.11      +206 -140  
jakarta-struts/contrib/scaffold/src/java/org/apache/struts/scaffold/BaseForm.java
  
  Index: BaseForm.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-struts/contrib/scaffold/src/java/org/apache/struts/scaffold/BaseForm.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- BaseForm.java     13 Nov 2002 19:16:41 -0000      1.10
  +++ BaseForm.java     23 Nov 2002 19:09:19 -0000      1.11
  @@ -20,49 +20,65 @@
   import com.wintecinc.struts.action.ValidatorForm; // Struts 1.0.x
   
   import org.apache.commons.scaffold.lang.ChainedException;
  -import org.apache.commons.scaffold.text.ConvertUtils;
  +import org.apache.commons.scaffold.lang.Log;
   import org.apache.commons.scaffold.lang.Tokens;
  +import org.apache.commons.scaffold.text.ConvertUtils;
   
   
   /**
    * Enhanced base ActionForm.
  + * :TODO: Extend from DynaValidatorForm in 1.1 version.
    * @author Ted Husted
    * @version $Revision$ $Date$
  - * @todo Change from BeanUtil.populate to copyProperties
  - * in 1.1 version.
    */
   public class BaseForm extends ValidatorForm {
   
   
  -// ----------------------------------------------------------- Properties
  -
  +// ---------------------------------------------------------- Remote Host
  + 
       /**
        * The network address making this request.
        * <p>
  -     * This is the value returned by reqest.getRemoteAddr.
  +     * This is the value returned by reqest.getremoteHost.
        * It is provided so that this can be logged by components on
        * the business tier if needed.
        * This property is maintained automatically through the
        * <code>reset</code> method.
        */
  -    private String remoteAddr = null;
  +    private String remoteHost = null;
   
       /**
  -     * Return our remoteAddr property.
  +     * Return our remoteHost property.
        */
  -    public String getRemoteAddr() {
  -        return (this.remoteAddr);
  +    public String getRemoteHost() {
  +        return (this.remoteHost);
       }
   
  -
       /**
  -     * Set our remoteAddr property.
  +     * Set our remoteHost property.
        */
  -    public void setRemoteAddr(String remoteAddr) {
  -        this.remoteAddr = remoteAddr;
  +    public void setRemoteHost(String remoteHost) {
  +        this.remoteHost = remoteHost;
       }
   
   
  +     /**
  +      * Sets RemoteHost attribute to request.getRemoteHost().
  +      */
  +     protected void resetRemoteHost(HttpServletRequest request) {
  +         setRemoteHost(request.getRemoteHost());
  +     }
  + 
  + 
  +// ------------------------------------------------------- Session Locale
  +
  +    /**
  +     * The session attribute key for our session locale [Action.LOCALE_KEY].
  +     * (Suggestion only, may be overridden by presentation framework
  +     */
  +    public static String STRUTS_LOCALE_KEY = Action.LOCALE_KEY;
  +
  +
       /**
        * Our locale property.
        * <p>
  @@ -95,6 +111,74 @@
   
   
       /**
  +     * Return the session key attribute for our locale object.
  +     */
  +    public String getSessionLocaleName() {
  +        return STRUTS_LOCALE_KEY;
  +    }
  +
  +
  +    /**
  +     * Reset our locale property to the locale object found in
  +     * the session associated with this request.
  +     */
  +    protected void resetSessionLocale(HttpServletRequest request) {
  +
  +        HttpSession session = request.getSession();
  +        if (session!=null) {
  +
  +            setSessionLocale((Locale)
  +                session.getAttribute(getSessionLocaleName()));
  +
  +        }
  +        else {
  +
  +            setSessionLocale(Locale.getDefault());
  +        }
  +
  +    } // end resetSessionLocale
  +
  +
  +    /**
  +     * Change the locale property in the session to our locale object,
  +     * or the default Locale if ours is null.
  +     */
  +    protected void putSessionLocale(HttpServletRequest request) {
  +
  +        Locale locale = getSessionLocale();
  +        if (null==locale) locale = Locale.getDefault();
  +
  +        request.getSession(true).setAttribute(Action.LOCALE_KEY,locale);
  +
  +    } // end putSessionLocale
  +
  +
  +    /**
  +     * Display the user's locale setting or the default locale.
  +     */
  +     public String getLocaleDisplay() {
  +
  +         Locale locale = getSessionLocale();
  +         if (null==locale) locale = Locale.getDefault();
  +         return locale.getDisplayName();
  +
  +     } // end getLocaleDisplay
  +
  +
  +    /**
  +     * Set our locale to given ISO Language Code.
  +     * An empty String is used for the country.
  +     * <p>
  +     * Mainly provided for completeness.
  +     */
  +     public void setLocaleDisplay(String language) {
  +         setSessionLocale(new Locale(language,EMPTY));
  +     }
  +
  +
  +// -------------------------------------------------------------- Mutable
  +
  +    /**
        * The mutable state.
        * <p>
        * To avoid autopopulation when forwarding beans between actions,
  @@ -123,6 +207,8 @@
       }
   
   
  +// ------------------------------------------------------------- Dispatch
  +
       /**
        * The dispatch property.
        * <p>
  @@ -152,6 +238,40 @@
   // --------------------------------------------------------- Public Methods
   
       /**
  +     * A static, empty String used by isBlank.
  +     */
  +     private static String EMPTY = "";
  +
  +
  +    /**
  +     * Convenience method to check for a null or empty String.
  +     *
  +     * @param s The sting to check
  +     */
  +    protected boolean blank(String s) {
  +        return ConvertUtils.blank(s);
  +    }
  +
  +
  +    /**
  +     * Convenience method to check for a null, empty, or "0" String.
  +     *
  +     * @param s The sting to check
  +     */
  +    protected boolean blankValue(String s) {
  +        return ConvertUtils.blankValue(s);
  +    }
  +
  +
  +    /**
  +     * @deprecated Use blank instead.
  +     */
  +    protected boolean isBlank(String s) {
  +        return blank(s);
  +    }
  +
  +
  +    /**
        * Convenience method to test for a required field
        * and setup the error message.
        */
  @@ -184,131 +304,38 @@
   
   
       /**
  -     * If bean is set to mutable, calls <code>resetSessionLocale</code>
  -     * and <code>setRemoteAddr</code>.
  +     * Create an object of the specified class,
  +     * throwing a runtime exception if any error occurs.
  +     * If an exception is not thrown, then helper is guaranteed to exist.
        *
  -     * Subclasses resetting their own fields should observe the mutable
  -     * state (<code>if (isMutable()) ...</code>).
  -     *
  -     * @param mapping The mapping used to select this instance
  -     * @param request The servlet request we are processing
  -     */
  -    public void reset(
  -            ActionMapping mapping,
  -            HttpServletRequest request) {
  -
  -        if (isMutable()) {
  -
  -            resetSessionLocale(request);
  -            setRemoteAddr(request.getRemoteAddr());
  -        }
  -
  -    } // end reset
  -
  -
  -    /**
  -     * Return an empty ActionErrors or the result of calling
  -     * the superclass validate. Will not return null.
  +     * @param objectClass The name of the class
  +     * @throws IllegalArgumentException if object cannot be
  +     * instantiated
        */
  -    public ActionErrors validate(ActionMapping mapping,
  -        HttpServletRequest request) {
  -
  -        ActionErrors errors = super.validate(mapping,request);
  -        if (null==errors) errors = new ActionErrors();
  -        return errors;
  -    }
  -
  -
  -    /**
  -     * Reset our locale property to the locale object found in
  -     * the session associated with this request.
  -     */
  -    protected void resetSessionLocale(HttpServletRequest request) {
  -
  -        HttpSession session = request.getSession();
  -        if (session!=null) {
  -
  -            setSessionLocale((Locale)
  -                session.getAttribute(Action.LOCALE_KEY));
  +    public Object createObject(
  +            String objectClass) {
   
  +            // Try the create
  +        Object object = null;
  +        try {
  +            object = Class.forName(objectClass).newInstance();
           }
  -        else {
  -
  -            setSessionLocale(Locale.getDefault());
  +        catch (Throwable t) {
  +           object = null;
  +               // assemble message: {class}: {exception}
  +           StringBuffer sb = new StringBuffer();
  +           sb.append(Log.CREATE_OBJECT_ERROR);
  +           sb.append(Log.CLASS);
  +           sb.append(objectClass);
  +           sb.append(Log.SPACE);
  +           sb.append(Log.ACTION_EXCEPTION);
  +           sb.append(t.toString());
  +                // throw runtime exception
  +           throw new IllegalArgumentException(sb.toString());
           }
  +        return object;
   
  -    } // end resetSessionLocale
  -
  -
  -    /**
  -     * Change the locale property in the session to our locale object,
  -     * or the default Locale if ours is null.
  -     */
  -    protected void putSessionLocale(HttpServletRequest request) {
  -
  -        Locale locale = getSessionLocale();
  -        if (null==locale) locale = Locale.getDefault();
  -
  -        request.getSession(true).setAttribute(Action.LOCALE_KEY,locale);
  -
  -    } // end putSessionLocale
  -
  -
  -    /**
  -     * Display the user's locale setting or the default locale.
  -     */
  -     public String getLocaleDisplay() {
  -
  -         Locale locale = getSessionLocale();
  -         if (null==locale) locale = Locale.getDefault();
  -         return locale.getDisplayName();
  -
  -     } // end getLocaleDisplay
  -
  -
  -    /**
  -     * Set our locale to given ISO Language Code.
  -     * An empty String is used for the country.
  -     * <p>
  -     * Mainly provided for completeness.
  -     */
  -     public void setLocaleDisplay(String language) {
  -         setSessionLocale(new Locale(language,EMPTY));
  -     }
  -
  -
  -    /**
  -     * A static, empty String used by isBlank.
  -     */
  -     private static String EMPTY = "";
  -
  -
  -    /**
  -     * Convenience method to check for a null or empty String.
  -     *
  -     * @param s The sting to check
  -     */
  -    protected boolean blank(String s) {
  -        return ConvertUtils.blank(s);
  -    }
  -
  -
  -    /**
  -     * Convenience method to check for a null, empty, or "0" String.
  -     *
  -     * @param s The sting to check
  -     */
  -    protected boolean blankValue(String s) {
  -        return ConvertUtils.blankValue(s);
  -    }
  -
  -
  -    /**
  -     * @deprecated Use blank instead.
  -     */
  -    protected boolean isBlank(String s) {
  -        return blank(s);
  -    }
  +     } // createHelperObject()
   
   
       /**
  @@ -318,7 +345,7 @@
        * this way, or a property name should be altered.
        * This will return the actual public properties.
        *
  -     * @exception Throws Exception on any error.
  +     * @exception Exception on any error.
        */
       public Map describe() throws Exception {
   
  @@ -337,7 +364,7 @@
        * <code>PropertyUtils.describe</code>.
        *
        * @param o The object to use to populate this object.
  -     * @exception Throws Exception on any error.
  +     * @exception Exception on any error.
        */
       public void set(Object o) throws Exception {
   
  @@ -356,7 +383,7 @@
        * <code>describe()</code>.
        *
        * @param o The object to populate from this object.
  -     * @exception Throws Exception on any error.
  +     * @exception Exception on any error.
        */
       public void populate(Object o) throws Exception {
   
  @@ -389,7 +416,7 @@
        * For an instance of BaseMapForm, getMap() is used; otherwise
        * describe() or PropertyUtils.describe() is used.
        *
  -     * @fixme Needs testing. Works OK without a profile bean =:o)
  +     * :FIXME: Needs testing. Works OK without a profile bean =:o)
        * @param profile The profile bean, if any
        * @throws Exception if error transfering data to map
        */
  @@ -434,9 +461,48 @@
   
       } // end merge
   
  -// end BaseForm
   
  -}
  + 
  +    /**
  +     * If bean is set to mutable, calls <code>resetSessionLocale</code>
  +     * and <code>resetRemoteHost</code>.
  +     *
  +     * Subclasses resetting their own fields should observe the mutable
  +     * state (<code>if (isMutable()) ...</code>).
  +     *
  +     * @param mapping The mapping used to select this instance
  +     * @param request The servlet request we are processing
  +     */
  +    public void reset(
  +            ActionMapping mapping,
  +            HttpServletRequest request) {
  +
  +        if (isMutable()) {
  +                     
  +                     super.reset(mapping,request);
  +
  +                // :TODO: Might be useful to have a collection of reset listeners 
  +            resetRemoteHost(request);
  +            resetSessionLocale(request);
  +        }
  +
  +    } // end reset
  +
  +
  +    /**
  +     * Return an empty ActionErrors or the result of calling
  +     * the superclass validate. Will not return null.
  +     */
  +    public ActionErrors validate(ActionMapping mapping,
  +        HttpServletRequest request) {
  +
  +        ActionErrors errors = super.validate(mapping,request);
  +        if (null==errors) errors = new ActionErrors();
  +        return errors;
  +    }
  +    
  +
  +} // end BaseForm
   
   
   /*
  
  
  
  1.7       +39 -39    
jakarta-struts/contrib/scaffold/src/java/org/apache/struts/scaffold/BaseAction.java
  
  Index: BaseAction.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-struts/contrib/scaffold/src/java/org/apache/struts/scaffold/BaseAction.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- BaseAction.java   31 Oct 2002 14:32:08 -0000      1.6
  +++ BaseAction.java   23 Nov 2002 19:09:19 -0000      1.7
  @@ -6,25 +6,13 @@
   import java.util.Locale;
   import java.util.Map;
   import java.util.Properties;
  -import java.util.StringTokenizer;
   
   import javax.servlet.ServletException;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
   import javax.servlet.http.HttpSession;
   
  -import org.apache.commons.beanutils.BeanUtils;
  -
  -import org.apache.struts.action.Action;
  -import org.apache.struts.action.ActionError;
  -import org.apache.struts.action.ActionErrors;
  -import org.apache.struts.action.ActionForm;
  -import org.apache.struts.action.ActionFormBean;
  -import org.apache.struts.action.ActionForward;
  -import org.apache.struts.action.ActionMapping;
  -import org.apache.struts.action.ActionServlet;
  -
  -import org.apache.struts.taglib.html.Constants;
  +import org.apache.struts.action.*;
   
   import org.apache.struts.util.MessageResources;
   
  @@ -40,6 +28,7 @@
    * operational details.
    * A <code>perform</code> method is also provided for backwards
    * compatibility with 1_0.
  + * :TODO: Remove deprecations after formal 1.0 Scaffod release.
    *
    * @author Ted Husted
    * @version $Revision$ $Date$
  @@ -124,8 +113,8 @@
        * set, the default locale is returned.
        *
        * @param request The HTTP request we are processing
  -     * @author  François Rey (FREY - [EMAIL PROTECTED])
  -     * @author  Eric Bariaux (EBRX - [EMAIL PROTECTED])
  +     * author  François Rey (FREY - [EMAIL PROTECTED])
  +     * author  Eric Bariaux (EBRX - [EMAIL PROTECTED])
        */
       protected Locale getLocale(HttpServletRequest request) {
   
  @@ -148,7 +137,7 @@
        * If a session context does not exist, one is created.
        *
        * @param request The HTTP request we are processing
  -     * @local locale The locale to use for this session
  +     * @param locale The locale to use for this session
        */
       protected void setLocale(
               HttpServletRequest request,
  @@ -161,6 +150,17 @@
   
   
   
  +// ------------------------------------------------------ Remote Node
  +
  +
  +    /**
  +     * Returns the RemoteHost IP as an Integer.
  +     */
  +    protected Integer getRemoteNode(HttpServletRequest request) {
  +        return new Integer(0); // :FIXME: Non functional
  +    }
  +
  +
   // ------------------------------------------------------ Remote Server
   
       /**
  @@ -187,7 +187,6 @@
   return servlet.getServletContext().getAttribute(getRemoteServerName());
       }
   
  -
   // ----------------------------------------------------------- Messages
   
       /**
  @@ -224,8 +223,9 @@
        * // :FIXME: In 1.1 this should be updated to use the
        * new ActionMessages superclass.
        *
  -     * @param errors Our ActionErrors collection
  -     * @param message our list of replaceable parameters
  +     * @param request The request we are servicing
  +     * @param alerts Our ActionErrors collection
  +     * @param list our list of replaceable parameters
        */
       protected void mergeAlerts(
               HttpServletRequest request,
  @@ -280,11 +280,11 @@
        * If it doesn't exist, and create is true, a new collection is
        * returned.
        *
  -     * @fixme In 1.1 this should be the ActionMessage superclass
  +     * :FIXME: In 1.1 this should be the ActionMessage superclass
        * @param request The HTTP request we are processing
        * @param create Whether to create a new collection if one does
        * not exist
  -     * @returns The pending ActionError queue
  +     * @return The pending ActionError queue
        */
       protected ActionErrors getMessages(
               HttpServletRequest request,
  @@ -337,7 +337,7 @@
        * Return whether there is an informational alert collection pending.
        *
        * @param request The HTTP request we are processing
  -     * @returns True if an informational alert collection exists
  +     * @return True if an informational alert collection exists
        */
       protected boolean isMessages(HttpServletRequest request) {
           return (null!=getMessages(request,false));
  @@ -369,7 +369,7 @@
        * Return whether there is an errors alert collection pending.
        *
        * @param request The HTTP request we are processing
  -     * @returns True if an errors alert collection exists
  +     * @return True if an errors alert collection exists
        */
       protected ActionErrors getErrors(
               HttpServletRequest request,
  @@ -396,7 +396,7 @@
        * Return whether there is an errors alert collection pending.
        *
        * @param request The HTTP request we are processing
  -     * @returns True if an errors alert collection exists
  +     * @return True if an errors alert collection exists
        */
       protected boolean isErrors(HttpServletRequest request) {
           return (null!=getErrors(request,false));
  @@ -425,10 +425,13 @@
        * separator.
        * Blanks are trimmed from tokens.
        *
  -     * @parameter parameter The string to tokenize into an array
  +     * @param parameter The string to tokenize into an array
        */
       public String[] tokenize(String parameter) {
  -
  +        
  +        return ConvertUtils.tokensToArray(parameter,getTokenSep());
  +             
  +/*        
           StringTokenizer tokenizer =
               new StringTokenizer(parameter,getTokenSep());
           int i = 0;
  @@ -439,7 +442,7 @@
               tokens[i++] = token;
           }
           return tokens;
  -
  +*/
       } // end tokenize()
   
   
  @@ -593,7 +596,7 @@
        * default behaviour will branch to findFailure().
        *
        * @param mapping The ActionMapping used to select this instance
  -     * @param actionForm The optional ActionForm bean for this request
  +     * @param form The optional ActionForm bean for this request
        * @param request The HTTP request we are processing
        * @param response The resonse we are creating
        */
  @@ -619,7 +622,7 @@
         * One possible error may be whether the form is null.
         *
         * @param mapping The ActionMapping used to select this instance
  -      * @param actionForm The optional ActionForm bean for this request
  +      * @param form The optional ActionForm bean for this request
         * @param request The HTTP request we are processing
         * @param response The resonse we are creating
         * @return The ActionForward representing FAILURE
  @@ -689,14 +692,12 @@
        * If overridden, if an alert is logged to the errors
        * queue (getErrors()), then default behaviour will branch
        * to findFailure().
  +     * :TODO: Use a StringBufferOUTPUTStream to capture trace for error queue
        *
        * @param mapping The ActionMapping used to select this instance
  -     * @param actionForm The optional ActionForm bean for this request
  +     * @param form The optional ActionForm bean for this request
        * @param request The HTTP request we are processing
        * @param response The response we are creating
  -     * @param errors Our ActionErrors collection
  -     * @param exception The exception we are catching
  -     * @todo Use a StringBufferOUTPUTStream to capture trace for error queue
        */
       protected void catchException(
                   ActionMapping mapping,
  @@ -752,7 +753,7 @@
        * Use getException() to check if error occured.
        *
        * @param mapping The ActionMapping used to select this instance
  -     * @param actionForm The optional ActionForm bean for this request
  +     * @param form The optional ActionForm bean for this request
        * @param request The HTTP request we are processing
        * @param response The resonse we are creating
        */
  @@ -773,10 +774,9 @@
        * The default returns mapping.findForward("continue");
        *
        * @param mapping The ActionMapping used to select this instance
  -     * @param actionForm The optional ActionForm bean for this request
  +     * @param form The optional ActionForm bean for this request
        * @param request The HTTP request we are processing
        * @param response The response we are creating
  -     * @param errors Our ActionErrors collection
        * @return The ActionForward representing SUCCESS
        * or null if a SUCCESS forward has not been specified.
        */
  @@ -810,7 +810,7 @@
        * <code>ServetExceptions</code> and rethrown.
        *
        * @param mapping The ActionMapping used to select this instance
  -     * @param actionForm The optional ActionForm bean for this request
  +     * @param form The optional ActionForm bean for this request
        * @param request The HTTP request we are processing
        * @param response The HTTP response we are creating
        * @exception IOException if an input/output error occurs
  @@ -854,7 +854,7 @@
        * as needed, and leave this one as is.
        *
        * @param mapping The ActionMapping used to select this instance
  -     * @param actionForm The optional ActionForm bean for this request
  +     * @param form The optional ActionForm bean for this request
        * @param request The HTTP request we are processing
        * @param response The HTTP response we are creating
        * @exception IOException if an input/output error occurs
  
  
  

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

Reply via email to