You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2020/01/28 20:34:05 UTC

[jspwiki] 04/32: fix: add synchronization to progressing tasks on progress manager

This is an automated email from the ASF dual-hosted git repository.

juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git

commit 8a7d63b26d4085913ffc76d73d30662b0710e9d2
Author: juanpablo <ju...@apache.org>
AuthorDate: Sat Jan 18 15:07:58 2020 +0100

    fix: add synchronization to progressing tasks on progress manager
---
 .../apache/wiki/ui/progress/ProgressManager.java   | 122 ++++++++++-----------
 1 file changed, 55 insertions(+), 67 deletions(-)

diff --git a/jspwiki-main/src/main/java/org/apache/wiki/ui/progress/ProgressManager.java b/jspwiki-main/src/main/java/org/apache/wiki/ui/progress/ProgressManager.java
index 7e04394..8552a3f 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/ui/progress/ProgressManager.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/ui/progress/ProgressManager.java
@@ -18,51 +18,46 @@
  */
 package org.apache.wiki.ui.progress;
 
+import org.apache.log4j.Logger;
+import org.apache.wiki.ajax.WikiAjaxDispatcherServlet;
+import org.apache.wiki.ajax.WikiAjaxServlet;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
 
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.apache.log4j.Logger;
-import org.apache.wiki.ajax.WikiAjaxDispatcherServlet;
-import org.apache.wiki.ajax.WikiAjaxServlet;
 
 /**
- *  Manages progressing items.  In general this class is used whenever JSPWiki
- *  is doing something which may require a long time.  In addition, this manager
- *  provides a JSON interface for finding remotely what the progress is.  The
- *  JSON object name is JSON_PROGRESSTRACKER = "{@value #JSON_PROGRESSTRACKER}".
+ *  Manages progressing items.  In general this class is used whenever JSPWiki is doing something which may require a long time.
+ *  In addition, this manager provides a JSON interface for finding remotely what the progress is.  The JSON object name is
+ *  JSON_PROGRESSTRACKER = "{@value #JSON_PROGRESSTRACKER}".
  *
  *  @since  2.6
  */
-// FIXME: Needs synchronization, I think
-public class ProgressManager
-{
-    private Map<String,ProgressItem> m_progressingTasks = new HashMap<String,ProgressItem>();
+public class ProgressManager {
 
-    /**
-     *  The name of the progress tracker JSON object.  The current value is "{@value}",
-     */
+    private Map< String,ProgressItem > m_progressingTasks = new ConcurrentHashMap<>();
+
+    /** The name of the progress tracker JSON object.  The current value is "{@value}", */
     public static final String JSON_PROGRESSTRACKER = "progressTracker";
 
-    private static Logger log = Logger.getLogger( ProgressManager.class );
+    private static final Logger log = Logger.getLogger( ProgressManager.class );
 
     /**
      *  Creates a new ProgressManager.
      */
-    public ProgressManager()
-    {
-    	//TODO: Replace with custom annotations. See JSPWIKI-566
+    public ProgressManager() {
+    	// TODO: Replace with custom annotations. See JSPWIKI-566
         WikiAjaxDispatcherServlet.registerServlet( JSON_PROGRESSTRACKER, new JSONTracker() );
     }
 
     /**
      *  You can use this to get an unique process identifier.
+     *
      *  @return A new random value
      */
     public String getNewProgressIdentifier()
@@ -71,31 +66,29 @@ public class ProgressManager
     }
 
     /**
-     *  Call this method to get your ProgressItem into the ProgressManager queue.
-     *  The ProgressItem will be moved to state STARTED.
+     *  Call this method to get your ProgressItem into the ProgressManager queue. The ProgressItem will be moved to state STARTED.
      *
      *  @param pi ProgressItem to start
      *  @param id The progress identifier
      */
-    public void startProgress( ProgressItem pi, String id )
-    {
-        log.debug("Adding "+id+" to progress queue");
+    public void startProgress( final ProgressItem pi, final String id ) {
+        log.debug( "Adding " + id + " to progress queue" );
         m_progressingTasks.put( id, pi );
         pi.setState( ProgressItem.STARTED );
     }
 
     /**
-     *  Call this method to remove your ProgressItem from the queue (after which
-     *  getProgress() will no longer find it.  The ProgressItem will be moved to state
-     *  STOPPED.
+     *  Call this method to remove your ProgressItem from the queue (after which getProgress() will no longer find it.
+     *  The ProgressItem will be moved to state STOPPED.
      *
      *  @param id The progress identifier
      */
-    public void stopProgress( String id )
-    {
-        log.debug("Removed "+id+" from progress queue");
-        ProgressItem pi = m_progressingTasks.remove( id );
-        if( pi != null ) pi.setState( ProgressItem.STOPPED );
+    public void stopProgress( final String id ) {
+        log.debug( "Removed " + id + " from progress queue" );
+        final ProgressItem pi = m_progressingTasks.remove( id );
+        if( pi != null ) {
+            pi.setState( ProgressItem.STOPPED );
+        }
     }
 
     /**
@@ -105,33 +98,27 @@ public class ProgressManager
      *  @return a value between 0 to 100 indicating the progress
      *  @throws IllegalArgumentException If no such progress item exists.
      */
-    public int getProgress( String id )
-        throws IllegalArgumentException
-    {
-        ProgressItem pi = m_progressingTasks.get( id );
-
-        if( pi != null )
-        {
+    public int getProgress( final String id ) throws IllegalArgumentException {
+        final ProgressItem pi = m_progressingTasks.get( id );
+        if( pi != null ) {
             return pi.getProgress();
         }
 
-        throw new IllegalArgumentException("No such id was found");
+        throw new IllegalArgumentException( "No such id was found" );
     }
 
     /**
-     *  Provides access to a progress indicator, assuming you know the ID.
-     *  Progress of zero (0) means that the progress has just started, and a progress of
-     *  100 means that it is complete.
+     *  Provides access to a progress indicator, assuming you know the ID. Progress of zero (0) means that the progress has just started,
+     *  and a progress of 100 means that it is complete.
      */
-    public class JSONTracker implements WikiAjaxServlet
-    {
+    public class JSONTracker implements WikiAjaxServlet {
         /**
          *  Returns upload progress in percents so far.
-         *  @param progressId The string representation of the progress ID that you want to know the
-         *                    progress of.
+         *
+         *  @param progressId The string representation of the progress ID that you want to know the progress of.
          *  @return a value between 0 to 100 indicating the progress
          */
-        public int getProgress( String progressId )
+        public int getProgress( final String progressId )
         {
             return ProgressManager.this.getProgress( progressId );
         }
@@ -140,26 +127,27 @@ public class ProgressManager
         	return JSON_PROGRESSTRACKER;
         }
         
-        public void service(HttpServletRequest req, HttpServletResponse resp, String actionName, List<String> params)
-        		throws ServletException, IOException 
-        {
-        	log.debug("ProgressManager.doGet() START");
-        	if (params.size()<1) {
+        public void service( final HttpServletRequest req,
+                             final HttpServletResponse resp,
+                             final String actionName,
+                             final List< String > params ) throws IOException {
+        	log.debug( "ProgressManager.doGet() START" );
+        	if( params.size() < 1 ) {
         		return;
         	}
-        	String progressId = params.get(0);
-        	log.debug("progressId="+progressId);
+        	final String progressId = params.get(0);
+        	log.debug( "progressId=" + progressId );
         	String progressString = "";
         	try {
-        		int progress = getProgress(progressId);
-        		progressString = Integer.toString(progress);
-        	} catch (IllegalArgumentException e) {
-        		// ignore
-        		log.debug("progressId "+progressId+" is no longer valid");
+        		progressString = Integer.toString( getProgress( progressId ) );
+        	} catch( final IllegalArgumentException e ) { // ignore
+        		log.debug( "progressId " + progressId + " is no longer valid" );
         	}
-        	log.debug("progressString="+progressString);
-        	resp.getWriter().write(progressString);
-        	log.debug("ProgressManager.doGet() DONE");
+        	log.debug( "progressString=" + progressString );
+        	resp.getWriter().write( progressString );
+        	log.debug( "ProgressManager.doGet() DONE" );
         }
+
     }
+
 }