You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pluto-scm@portals.apache.org by ms...@apache.org on 2016/04/08 15:40:34 UTC

[22/34] portals-pluto git commit: fixed bugs in async portlet

fixed bugs in async portlet


Project: http://git-wip-us.apache.org/repos/asf/portals-pluto/repo
Commit: http://git-wip-us.apache.org/repos/asf/portals-pluto/commit/75119d2e
Tree: http://git-wip-us.apache.org/repos/asf/portals-pluto/tree/75119d2e
Diff: http://git-wip-us.apache.org/repos/asf/portals-pluto/diff/75119d2e

Branch: refs/heads/V3Prototype
Commit: 75119d2ed7d4645bebf22183435517a22bf155e6
Parents: c4a9c53
Author: Scott Nicklous <ms...@apache.org>
Authored: Thu Mar 31 21:18:59 2016 +0200
Committer: Scott Nicklous <ms...@apache.org>
Committed: Thu Mar 31 21:18:59 2016 +0200

----------------------------------------------------------------------
 .../apache/portals/samples/AsyncDialogBean.java | 77 +++++++++++---------
 .../portals/samples/AsyncPortletResource.java   | 47 ++++--------
 2 files changed, 59 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/75119d2e/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncDialogBean.java
----------------------------------------------------------------------
diff --git a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncDialogBean.java b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncDialogBean.java
index a74af06..3e1f003 100644
--- a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncDialogBean.java
+++ b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncDialogBean.java
@@ -35,28 +35,30 @@ import javax.portlet.annotations.RenderStateScoped;
  * Render state scoped bean. The bean is stored as a render parameter, so it
  * needs to be portlet serializable.
  */
-@RenderStateScoped @Named("adb")
+@RenderStateScoped
+@Named("adb")
 public class AsyncDialogBean implements PortletSerializable {
    private static final Logger LOGGER = Logger.getLogger(AsyncDialogBean.class.getName());
-   
-   public enum OutputType {TEXT, INC, FWD, DISPATCH, AUTO}
-   
-   public static final String PARAM_MSG = "msg";
-   public static final String PARAM_DELAY = "delay";
-   public static final String PARAM_REPS = "reps";
-   public static final String PARAM_AUTO = "auto";
-   public static final String PARAM_TYPE = "type";
+
+   public enum OutputType {
+      TEXT, INC, FWD, DISPATCH, AUTO
+   }
+
+   public static final String PARAM_MSG      = "msg";
+   public static final String PARAM_DELAY    = "delay";
+   public static final String PARAM_REPS     = "reps";
+   public static final String PARAM_AUTO     = "auto";
+   public static final String PARAM_TYPE     = "type";
    public static final String PARAM_TYPE_TXT = OutputType.TEXT.toString();
    public static final String PARAM_TYPE_INC = OutputType.INC.toString();
    public static final String PARAM_TYPE_FWD = OutputType.FWD.toString();
    public static final String PARAM_TYPE_DIS = OutputType.DISPATCH.toString();
 
-   
-   private int delay;
-   private int reps;
-   private OutputType type;
-   private String msg;
-   private boolean autoDispatch;
+   private int                delay;
+   private int                reps;
+   private OutputType         type;
+   private String             msg;
+   private boolean            autoDispatch;
 
    /**
     * This method is called by the portlet container to initialize the bean at
@@ -86,12 +88,11 @@ public class AsyncDialogBean implements PortletSerializable {
     */
    @Override
    public String[] serialize() {
-      String[] state = {""+delay, ""+reps, type.toString(), msg, ""+autoDispatch};
+      String[] state = { "" + delay, "" + reps, type.toString(), msg, "" + autoDispatch };
       LOGGER.fine("serialized: " + Arrays.asList(state).toString());
       return state;
    }
 
-
    /**
     * @return the delay
     */
@@ -100,7 +101,8 @@ public class AsyncDialogBean implements PortletSerializable {
    }
 
    /**
-    * @param delay the delay to set
+    * @param delay
+    *           the delay to set
     */
    public void setDelay(int delay) {
       this.delay = delay;
@@ -114,7 +116,8 @@ public class AsyncDialogBean implements PortletSerializable {
    }
 
    /**
-    * @param reps the reps to set
+    * @param reps
+    *           the reps to set
     */
    public void setReps(int reps) {
       this.reps = reps;
@@ -128,7 +131,8 @@ public class AsyncDialogBean implements PortletSerializable {
    }
 
    /**
-    * @param type the type to set
+    * @param type
+    *           the type to set
     */
    public void setType(OutputType type) {
       this.type = type;
@@ -142,7 +146,8 @@ public class AsyncDialogBean implements PortletSerializable {
    }
 
    /**
-    * @param msg the msg to set
+    * @param msg
+    *           the msg to set
     */
    public void setMsg(String msg) {
       this.msg = msg;
@@ -156,7 +161,8 @@ public class AsyncDialogBean implements PortletSerializable {
    }
 
    /**
-    * @param autoDispatch the autoDispatch to set
+    * @param autoDispatch
+    *           the autoDispatch to set
     */
    public void setAutoDispatch(boolean autoDispatch) {
       this.autoDispatch = autoDispatch;
@@ -167,8 +173,7 @@ public class AsyncDialogBean implements PortletSerializable {
     * 
     * @return the action form as string
     */
-   @RenderMethod(portletNames = "AsyncPortlet", ordinal = 100, 
-         include = "/WEB-INF/jsp/asyncDialog.jsp")
+   @RenderMethod(portletNames = "AsyncPortlet", ordinal = 100, include = "/WEB-INF/jsp/asyncDialog.jsp")
    public void getDialog() {
    }
 
@@ -177,36 +182,40 @@ public class AsyncDialogBean implements PortletSerializable {
     */
    @ActionMethod(portletName = "AsyncPortlet")
    public void handleDialog(ActionRequest req, ActionResponse resp) throws PortletException, IOException {
-      
+
       msg = null;
-      
+
       String strReps = req.getActionParameters().getValue(PARAM_REPS);
       if (strReps != null) {
          try {
             reps = Integer.parseInt(strReps);
-            if (reps <= 0 || reps > 8) throw new Exception("broken");
+            if (reps <= 0 || reps > 8)
+               throw new Exception("broken");
          } catch (Exception e) {
             msg = "try again. bad repetitions.";
          }
       }
-      
+
       String strDelay = req.getActionParameters().getValue(PARAM_DELAY);
       if (strDelay != null) {
          try {
             delay = Integer.parseInt(strDelay);
-            if (delay < 0) throw new Exception("broken");
+            if (delay < 0)
+               throw new Exception("broken");
          } catch (Exception e) {
             msg = "try again. bad delay.";
          }
       }
-      
+
       String strType = req.getActionParameters().getValue(PARAM_TYPE);
       if (strType != null) {
          try {
             type = OutputType.valueOf(strType);
-            if (type == OutputType.FWD && reps > 1) {
-               msg = "Repetitions cannot be > 1 for forwards.";
-               reps = 1;
+            if (reps > 1) {
+               if ((type == OutputType.FWD) || (type == OutputType.DISPATCH)) {
+                  msg = "Repetitions cannot be > 1 for forwards or dispatches.";
+                  reps = 1;
+               }
             }
          } catch (Exception e) {
             msg = "try again. bad type: " + strType;
@@ -224,7 +233,7 @@ public class AsyncDialogBean implements PortletSerializable {
          }
       }
 
-      String[] state = {""+delay, ""+reps, type.toString(), msg, ""+autoDispatch};
+      String[] state = { "" + delay, "" + reps, type.toString(), msg, "" + autoDispatch };
       LOGGER.fine("Resulting params: " + Arrays.asList(state).toString());
    }
 

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/75119d2e/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletResource.java
----------------------------------------------------------------------
diff --git a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletResource.java b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletResource.java
index 72b681b..0868f58 100644
--- a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletResource.java
+++ b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletResource.java
@@ -95,7 +95,7 @@ public class AsyncPortletResource {
                StringBuilder txt = new StringBuilder(128);
                txt.append("<h5>Thread producing text output for portlet: " + portletName + "</h5>");
                txt.append("<p>dispatcher type: ").append(hreq.getDispatcherType().toString());
-               txt.append("</p>");
+               txt.append("</p><hr>");
                hresp.getWriter().write(txt.toString());
                ctx.complete();
                break;
@@ -154,6 +154,11 @@ public class AsyncPortletResource {
          reps = adb.getReps();
       }
       
+      boolean done = (reps <= 0) || (adb.getDelay() <= 0);
+
+      reps--;
+      req.setAttribute(ATTRIB_REPS, reps);
+      
       StringBuilder txt = new StringBuilder(128);
       txt.append("Resource method.");
       txt.append(" delay: ").append(adb.getDelay());
@@ -164,35 +169,14 @@ public class AsyncPortletResource {
       txt.append(", auto-dispatch: ").append(auto);
       LOGGER.fine(txt.toString());
       
-      AsyncContext ctx = null;
-
-      if (reps > 0) {
-         reps--;
-         req.setAttribute(ATTRIB_REPS, reps);
-         
-         ctx = req.startAsync();
-         ctx.setTimeout(4000);
-      }
+      AsyncContext ctx = req.startAsync();
+      ctx.setTimeout(4000);
 
       if (auto || (adb.getDelay() <= 0)) {
          
          // produce output if dispatched from work thread or if there is no delay requested
          
          PortletRequestDispatcher rd;
-         
-         if (ctx == null) {
-            // last iteration of auto-dispatch
-            LOGGER.fine("Retrieving async context for the last run.");
-            try {
-               ctx = req.getAsyncContext();
-            } catch (Exception e) {
-               txt.setLength(0);
-               txt.append("Could not get AsyncContext. Exception: ").append(e.toString());
-               LOGGER.warning(txt.toString());
-               resp.getWriter().write(txt.toString());
-               return;
-            }
-         }
 
          HttpServletRequest hreq = (HttpServletRequest) req.getAttribute("javax.portlet.debug.ServletRequest");
          trace(hreq, "Resource method: ");
@@ -207,7 +191,8 @@ public class AsyncPortletResource {
          switch (adb.getType()) {
          case DISPATCH:
             LOGGER.fine("Dispatching to JSP.");
-            ctx.dispatch(JSP);
+            req.setAttribute(ATTRIB_TITLE, "Resource Method dispatching to JSP");
+            ctx.dispatch(hreq.getServletContext(), JSP);
             break;
          case FWD:
             LOGGER.fine("Doing request dispatcher forward to JSP.");
@@ -215,7 +200,7 @@ public class AsyncPortletResource {
             rd = req.getPortletContext().getRequestDispatcher(JSP);
             rd.forward(req, resp);
             resp.flushBuffer();
-            if (!auto) {
+            if (done) {
                ctx.complete();
             }
             break;
@@ -225,26 +210,26 @@ public class AsyncPortletResource {
             rd = req.getPortletContext().getRequestDispatcher(JSP);
             rd.include(req, resp);
             resp.flushBuffer();
-            if (!auto) {
+            if (done) {
                ctx.complete();
             }
             break;
          default:
             LOGGER.fine("Producing text output.");
             txt.setLength(0);
-            txt.append("<h5>Async portlet resource method producing text output for portlet: " + portletName + "</h5>");
+            txt.append("<h5>Resource method producing text output for portlet: " + portletName + "</h5>");
             txt.append("<p>dispatcher type: ").append(req.getDispatcherType().toString());
-            txt.append("</p>");
+            txt.append("</p><hr>");
             resp.getWriter().write(txt.toString());
             resp.flushBuffer();
-            if (!auto) {
+            if (done) {
                ctx.complete();
             }
             break;
          }
       }
       
-      if (adb.getDelay() > 0 && req.isAsyncStarted()) {
+      if (!done) {
          
          // now start the executor thread