You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by ek...@apache.org on 2006/03/02 20:32:36 UTC

svn commit: r382479 - /beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/ProcessPopulate.java

Author: ekoneil
Date: Thu Mar  2 11:32:34 2006
New Revision: 382479

URL: http://svn.apache.org/viewcvs?rev=382479&view=rev
Log:
Some cleanup in ProcessPopulate.  Make a package protected method private and avoid declaring two method locals that didn't need their own definitions.

BB: self
Test: NetUI BVT pass


Modified:
    beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/ProcessPopulate.java

Modified: beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/ProcessPopulate.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/ProcessPopulate.java?rev=382479&r1=382478&r2=382479&view=diff
==============================================================================
--- beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/ProcessPopulate.java (original)
+++ beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/ProcessPopulate.java Thu Mar  2 11:32:34 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004 The Apache Software Foundation.
+ * Copyright 2006 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -51,24 +51,22 @@
  * the updates are delegated to the Struts processPopulate infrastructure.
  *
  */
-public class ProcessPopulate
-{
+public class ProcessPopulate {
     /**
      * This defines the name of the parameter that will contain the NetUI ID map..
      */
     public static final String IDMAP_PARAMETER_NAME = "netuiIdMap";
 
-    private static final Logger _logger = Logger.getInstance(ProcessPopulate.class);
+    private static final Logger LOG = Logger.getInstance(ProcessPopulate.class);
+
+    private static final String TAG_HANDLER_PREFIX = "wlw-";
+    private static final String TAG_HANDLER_SUFFIX = ":";
+    private static final Map HANDLER_MAP = new HashMap();
 
     // these must be kept in sync with the context names specified in the scripting languages
     private static final String PAGE_FLOW_CONTEXT = "pageFlow";
     private static final String GLOBAL_APP_CONTEXT = "globalApp";
 
-    private static final String WLW_TAG_HANDLER_PREFIX = "wlw-";
-    private static final String WLW_TAG_HANDLER_SUFFIX = ":";
-
-    private static final Map handlerMap = new HashMap();
-
     /**
      * An inner class that represnts the data that will be used to
      * perform an update.  If a key has a prefix handler, this
@@ -76,21 +74,20 @@
      * so that the prefix handler can change the expression or
      * values that will be used to execute the expression update.
      */
-    public final static class ExpressionUpdateNode
-    {
+    public final static class ExpressionUpdateNode {
+
         public String expression = null;
         public String[] values = null;
 
-        // can't be constructed outside of this class
+        /* Prevent construction outside of this class */
         private ExpressionUpdateNode() {}
 
-        public String toString()
-        {
+        public String toString() {
             InternalStringBuilder buf = new InternalStringBuilder();
-            buf.append("expression: " + expression + "\n");
+            buf.append("expression: ").append(expression).append("\n");
             if(values != null)
                 for(int i = 0; i < values.length; i++)
-                    buf.append("value[" + i + "]: " + values[i]);
+                    buf.append("value[").append(i).append("]: ").append(values[i]);
             else buf.append("values are null");
 
             return buf.toString();
@@ -112,15 +109,17 @@
      */
     public static void registerPrefixHandler(String prefix, RequestParameterHandler handler)
     {
-        // should happen very infrequently
-        synchronized(handlerMap)
-        {
+        /*
+        This synchronization point should happen very infrequently as this only happens when a prefix handler
+        is added to the set.
+        */
+        synchronized(HANDLER_MAP) {
             String msg = "Register RequestParameterHandler with\n\tprefix: " + prefix + "\n\thandler: " + (handler != null ? handler.getClass().getName(): null);
 
-            if(_logger.isInfoEnabled()) _logger.info(msg);
+            LOG.info(msg);
 
-            if (handlerMap.get(prefix) == null)
-                handlerMap.put(prefix, handler);
+            if (HANDLER_MAP.get(prefix) == null)
+                HANDLER_MAP.put(prefix, handler);
         }
     }
 
@@ -132,13 +131,13 @@
         if(!ExpressionEvaluatorFactory.getInstance().isExpression(expression))
             throw new IllegalArgumentException(Bundle.getErrorString("ProcessPopulate_handler_nonAtomicExpression", new Object[] {expression}));
 
-        if(!handlerMap.containsKey(handler))
+        if(!HANDLER_MAP.containsKey(handler))
             throw new IllegalStateException(Bundle.getErrorString("ProcessPopulate_handler_notRegistered", new Object[] {handler}));
 
         InternalStringBuilder buf = new InternalStringBuilder();
-        buf.append(WLW_TAG_HANDLER_PREFIX);
+        buf.append(TAG_HANDLER_PREFIX);
         buf.append(handler);
-        buf.append(WLW_TAG_HANDLER_SUFFIX);
+        buf.append(TAG_HANDLER_SUFFIX);
         buf.append(expression);
 
         return buf.toString();
@@ -158,14 +157,16 @@
      *         the request; failure here can be caused by failures in creating
      *         or executing update expressions.
      */
-    public static void populate(HttpServletRequest request, HttpServletResponse response, ActionForm form, boolean requestHasPopulated)
-        throws ServletException
-    {
+    public static void populate(HttpServletRequest request,
+                                HttpServletResponse response,
+                                ActionForm form,
+                                boolean requestHasPopulated)
+        throws ServletException {
         String key = null;
         Map strutsProperties = null;
         ExpressionEvaluator ee = ExpressionEvaluatorFactory.getInstance();
 
-        // a boolean so that we can avoid an instanceof below...
+        /* Boolean used to avoid instanceof check below */
         boolean isMultipart = false;
 
         // if this returns null, it's not a mulitpart request
@@ -176,41 +177,33 @@
             isMultipart = true;
         else params = request.getParameterMap();
 
-        if(params == null)
-        {
-            if(_logger.isWarnEnabled()) _logger.warn("An error occurred checking a request for multipart status.  No model values were updated.");
+        if(params == null) {
+            LOG.warn("An error occurred checking a request for multipart status.  No model values were updated.");
             return;
         }
 
         /* explicitly build a variable resolver that is used to provide objects that may be updated to the expression engine */
         VariableResolver variableResolver = ImplicitObjectUtil.getUpdateVariableResolver(form, request, response, true);
 
-        /* todo: are there any ordering issues with using an Iterator vs. an Enumeration here? */
         Iterator iterator = params.keySet().iterator();
-        while (iterator.hasNext())
-        {
-            key = (String) iterator.next();
+        while (iterator.hasNext()) {
+            key = (String)iterator.next();
             String expr = null;
 
             // if there is an expression map, lookup the real expression from the name
             expr = key;
-            if (_logger.isDebugEnabled())
-                _logger.debug("key: " + key + " value type: " + params.get(key).getClass().getName() + " value: " + params.get(key));
+            LOG.debug("key: " + key + " value type: " + params.get(key).getClass().getName() + " value: " + params.get(key));
 
-            try
-            {
+            try {
                 Object paramsValue = params.get(key);
-                if (ee.containsExpression(expr))
-                {
+                if (ee.containsExpression(expr)) {
                     Object updateValue = null;
-                    if (!isMultipart || paramsValue instanceof String[])
-                    {
+                    if (!isMultipart || paramsValue instanceof String[]) {
                         String[] values = (String[]) paramsValue;
 
                         // the only "contains" case that is accepted
-                        if (expr.startsWith(WLW_TAG_HANDLER_PREFIX))
-                        {
-                            if (_logger.isDebugEnabled()) _logger.debug("Found an expression requiring a TAG HANDLER");
+                        if (expr.startsWith(TAG_HANDLER_PREFIX)) {
+                            LOG.debug("Found an expression requiring a TAG HANDLER");
 
                             ExpressionUpdateNode node = doTagHandler(key, expr, values, request);
 
@@ -224,22 +217,18 @@
                             updateValue = values;
                     }
                     // handle funky types that Struts returns for a file upload request handler
-                    else
-                    {
+                    else {
                         updateValue = params.get(key);
                     }
 
-                    try
-                    {
+                    try {
                         // trap any bad expressions here
-                        if (ee.isExpression(expr))
-                        {
+                        if (ee.isExpression(expr)) {
                             // common case, make this fast
                             if (!requestHasPopulated)
                                 ee.update(expr, updateValue, variableResolver, true);
                             // must check the expression to make sure pageFlow. and globalApp. don't get executed more than once
-                            else
-                            {
+                            else {
                                 Expression pe = ee.parseExpression(expr);
                                 String contextName = pe.getContext();
                                 if (!contextName.equals(PAGE_FLOW_CONTEXT) && !contextName.equals(GLOBAL_APP_CONTEXT))
@@ -247,21 +236,17 @@
                             }
                         }
                     }
-                            // catch any errors, particularly expression parse failures
-                    catch (ExpressionUpdateException e)
-                    {
+                    // catch any errors, particularly expression parse failures
+                    catch (ExpressionUpdateException e) {
                         String s = Bundle.getString("ExprUpdateError", new Object[]{expr, e});
-
-                        // this is the hairy NetUI Warning that gets printed to the console
-                        if (_logger.isErrorEnabled()) _logger.error(s);
+                        LOG.error(s);
 
                         // add binding errors via PageFlowUtils
                         InternalUtils.addBindingUpdateError(request, expr, s, e);
                     }
                 }
-                else
-                {
-                    if (_logger.isDebugEnabled()) _logger.debug("HTTP request parameter key \"" + key + "\" is not an expression, handle with Struts");
+                else {
+                    LOG.debug("HTTP request parameter key \"" + key + "\" is not an expression, handle with Struts");
 
                     if (strutsProperties == null)
                         strutsProperties = new HashMap();
@@ -269,13 +254,11 @@
                     strutsProperties.put(key, paramsValue);
                 }
             }
-                    // catch any unexpected exception
-            catch (Exception e)
-            {
+            // catch any unexpected exception
+            catch (Exception e) {
                 String s = Bundle.getString("ProcessPopulate_exprUpdateError", new Object[]{expr, e});
-                //e.printStackTrace();
 
-                if (_logger.isWarnEnabled()) _logger.warn(s, e);
+                LOG.warn(s, e);
 
                 // add binding errors via PageFlowUtils
                 InternalUtils.addBindingUpdateError(request, expr, s, e);
@@ -286,63 +269,65 @@
     }
 
     /**
-     * Process a single key.
+     * Execute a NetUI prefix handler.  This method is called when a prefix is encountered in the request that
+     * has a "handler" prefix key.  This allows some handler to modify the request's name / value pairs
+     * before they are passed to the expression language.
      *
      * @param key the request key that is being processed
-     * @param request the ServletRequest object representing this request
+     * @param request this request's {@link javax.servlet.ServletRequest}
      */
-    static final ExpressionUpdateNode doTagHandler(String key, String expression, String[] values, HttpServletRequest request)
-    {
-        // not sure if this array will be mutable.  don't want to find out at this point.
-        String[] _values = values;
+    private static ExpressionUpdateNode doTagHandler(String key, String expression, String[] values, HttpServletRequest request) {
 
-        // key might be mangled here; make a copy
-        String expr = expression;
+        LOG.debug("Found prefixed tag; handlerName: " + key.substring(TAG_HANDLER_PREFIX.length(), key.indexOf(TAG_HANDLER_SUFFIX)));
 
-        if(_logger.isDebugEnabled()) _logger.debug("Found prefixed tag; handlerName: " + key.substring(WLW_TAG_HANDLER_PREFIX.length(), key.indexOf(WLW_TAG_HANDLER_SUFFIX)));
+        String handlerName = expression.substring(TAG_HANDLER_PREFIX.length(), expression.indexOf(TAG_HANDLER_SUFFIX));
 
-        String handlerName = expression.substring(WLW_TAG_HANDLER_PREFIX.length(), expression.indexOf(WLW_TAG_HANDLER_SUFFIX));
+        // Execute callback to a parameter handler.
+        RequestParameterHandler handler = (RequestParameterHandler)HANDLER_MAP.get(handlerName);
 
-        // execute callback to parameter handler.  Generally, these are tags.
-        RequestParameterHandler handler = (RequestParameterHandler)handlerMap.get(handlerName);
+        if(handler != null) {
+            expression = expression.substring(expression.indexOf(TAG_HANDLER_SUFFIX)+1);
 
-        if(handler != null)
-        {
-            expr = expression.substring(expression.indexOf(WLW_TAG_HANDLER_SUFFIX)+1);
-
-            if(_logger.isDebugEnabled()) _logger.debug("found handler for prefix \"" + handlerName + "\" type: " +
-                                   (handler != null ? handler.getClass().getName() : null) + "\n\t" + 
-                                   "key: \"" + key + "\" expr: \"" + expr + "\"");
+            LOG.debug("found handler for prefix \"" + handlerName + "\" type: " +
+                (handler != null ? handler.getClass().getName() : null)
+                + "\n\t" + "key: \"" + key + "\" expr: \"" + expression + "\"");
             
             ExpressionUpdateNode node = new ExpressionUpdateNode();
-            node.expression = expr;
-            node.values = _values;
+            node.expression = expression;
+            node.values = values;
             
-            // request, request key, the standalone expression (may have other stuff bracketing the expression
+            /*
+             Call the haneler to process the request's parameter
+             */
             handler.process(request, key, expression, node);
             
             return node;
         }
         else throw new IllegalStateException("Request parameter references a tag handler prefix \"" + 
-                                             handlerName + "\" that is not registered for expression \"" + key + "\"");
+            handlerName + "\" that is not registered for expression \"" + key + "\"");
     }
 
-    // @struts: org.apache.struts.util.RequestUtils.populate
-    private static final void handleStrutsProperties(Map strutsProperties, ActionForm form)
-    {
-        if(strutsProperties != null)
-        {
-            if(_logger.isDebugEnabled()) _logger.debug("Handle Struts request parameters.");
-
-            // default to Struts for non-expression keys
-            try
-            {
+    /**
+     * This code originated from the Struts class
+     * {@link org.apache.struts.util.RequestUtils#populate(Object, javax.servlet.http.HttpServletRequest)}
+     * and just defers to Struts for Struts's usual request parameter population.
+     *
+     * @param strutsProperties a Map of properties that should be applied to the form using Strus's algorithm
+     * for doing so
+     * @param form the form to which properties should be applied
+     */
+    private static void handleStrutsProperties(Map strutsProperties, ActionForm form) {
+        if(strutsProperties != null) {
+
+            LOG.debug("Handle Struts request parameters.");
+
+            /* defer as Struts does to BeanUtils for non-NetUI expression keys */
+            try {
                 BeanUtils.populate(form, strutsProperties);
             }
-            catch(Exception e)
-            {
+            catch(Exception e) {
                 throw new RuntimeException("Exception processing bean and request parameters: ", e);
             }
         }
     }
-}
+}
\ No newline at end of file