You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2014/08/15 09:51:24 UTC

[14/17] git commit: WICKET-5619 ConcurrentModificationException may occur when calling EventBus.post()

WICKET-5619 ConcurrentModificationException may occur when calling EventBus.post()

Use Iterables#filter() as suggested at com.google.common.collect.Collections2#filter


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/78fc5b53
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/78fc5b53
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/78fc5b53

Branch: refs/heads/wicket-6.x
Commit: 78fc5b53eb09a76a2e7f307f2ce5fbbd2c8f6c0a
Parents: 66bff0b
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Thu Aug 14 13:28:44 2014 +0200
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Fri Aug 15 09:48:17 2014 +0200

----------------------------------------------------------------------
 .../atmosphere/AtmosphereRequestHandler.java    |  9 ++++----
 .../wicket/atmosphere/AtmosphereWebRequest.java |  8 +++----
 .../org/apache/wicket/atmosphere/EventBus.java  | 22 ++++++++++++--------
 3 files changed, 22 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/78fc5b53/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereRequestHandler.java
----------------------------------------------------------------------
diff --git a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereRequestHandler.java b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereRequestHandler.java
index 8824468..3ba143d 100644
--- a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereRequestHandler.java
+++ b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereRequestHandler.java
@@ -16,7 +16,7 @@
  */
 package org.apache.wicket.atmosphere;
 
-import java.util.Collection;
+import java.util.Iterator;
 
 import org.apache.wicket.Component;
 import org.apache.wicket.Page;
@@ -44,7 +44,7 @@ public class AtmosphereRequestHandler implements IRequestHandler
 
 	private final AtmosphereEvent event;
 
-	private final Collection<EventSubscription> subscriptions;
+	private final Iterator<EventSubscription> subscriptions;
 
 	private final EventSubscriptionInvoker eventSubscriptionInvoker;
 
@@ -58,7 +58,7 @@ public class AtmosphereRequestHandler implements IRequestHandler
 	 * @param event
 	 * @param eventSubscriptionInvoker
 	 */
-	public AtmosphereRequestHandler(PageKey pageKey, Collection<EventSubscription> subscriptions,
+	public AtmosphereRequestHandler(PageKey pageKey, Iterator<EventSubscription> subscriptions,
 		AtmosphereEvent event, EventSubscriptionInvoker eventSubscriptionInvoker)
 	{
 		this.pageKey = pageKey;
@@ -78,8 +78,9 @@ public class AtmosphereRequestHandler implements IRequestHandler
 
 	private void executeHandlers(AjaxRequestTarget target, Page page)
 	{
-		for (EventSubscription curSubscription : subscriptions)
+		while (subscriptions.hasNext())
 		{
+			EventSubscription curSubscription = subscriptions.next();
 			if (curSubscription.getContextAwareFilter().apply(event))
 			{
 				String componentPath = curSubscription.getComponentPath();

http://git-wip-us.apache.org/repos/asf/wicket/blob/78fc5b53/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereWebRequest.java
----------------------------------------------------------------------
diff --git a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereWebRequest.java b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereWebRequest.java
index eb8f5c1..b373983 100644
--- a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereWebRequest.java
+++ b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereWebRequest.java
@@ -17,7 +17,7 @@
 package org.apache.wicket.atmosphere;
 
 import java.nio.charset.Charset;
-import java.util.Collection;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 
@@ -47,12 +47,12 @@ class AtmosphereWebRequest extends ServletWebRequest
 
 	private final PageKey pageKey;
 
-	private final Collection<EventSubscription> subscriptions;
+	private final Iterator<EventSubscription> subscriptions;
 
 	private final AtmosphereEvent event;
 
 	AtmosphereWebRequest(ServletWebRequest wrappedRequest, PageKey pageKey,
-		Collection<EventSubscription> subscriptions, AtmosphereEvent event)
+		Iterator<EventSubscription> subscriptions, AtmosphereEvent event)
 	{
 		super(wrappedRequest.getContainerRequest(), wrappedRequest.getFilterPrefix());
 		this.wrappedRequest = wrappedRequest;
@@ -66,7 +66,7 @@ class AtmosphereWebRequest extends ServletWebRequest
 		return pageKey;
 	}
 
-	public Collection<EventSubscription> getSubscriptions()
+	public Iterator<EventSubscription> getSubscriptions()
 	{
 		return subscriptions;
 	}

http://git-wip-us.apache.org/repos/asf/wicket/blob/78fc5b53/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/EventBus.java
----------------------------------------------------------------------
diff --git a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/EventBus.java b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/EventBus.java
index e60ca01..6b56d56 100644
--- a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/EventBus.java
+++ b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/EventBus.java
@@ -17,7 +17,7 @@
 package org.apache.wicket.atmosphere;
 
 import java.util.Collection;
-import java.util.Collections;
+import java.util.ConcurrentModificationException;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -48,9 +48,9 @@ import org.atmosphere.cpr.BroadcasterFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.Collections2;
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Multimap;
 
@@ -234,7 +234,7 @@ public class EventBus implements UnboundListener
 		if (log.isDebugEnabled())
 		{
 			log.debug("registered page {} for session {}", pageKey.getPageId(),
-				pageKey.getSessionId());
+					pageKey.getSessionId());
 		}
 	}
 
@@ -395,21 +395,25 @@ public class EventBus implements UnboundListener
 		ThreadContext.detach();
 		ThreadContext.setApplication(application);
 		PageKey key;
-		Collection<EventSubscription> subscriptionsForPage;
+		Iterable<EventSubscription> subscriptionsForPage;
 		synchronized (this)
 		{
 			key = trackedPages.get(resource.uuid());
-			subscriptionsForPage = Collections2.filter(
-				Collections.unmodifiableCollection(subscriptions.get(key)), new EventFilter(event));
+			Collection<EventSubscription> eventSubscriptions = subscriptions.get(key);
+			subscriptionsForPage = Iterables.filter(eventSubscriptions, new EventFilter(event));
 		}
 		if (key == null)
 			getBroadcaster().removeAtmosphereResource(resource);
-		else if (!subscriptionsForPage.isEmpty())
-			post(resource, key, subscriptionsForPage, event);
+		else
+		{
+			Iterator<EventSubscription> iterator = subscriptionsForPage.iterator();
+			if (iterator.hasNext())
+				post(resource, key, iterator, event);
+		}
 	}
 
 	private void post(AtmosphereResource resource, PageKey pageKey,
-		Collection<EventSubscription> subscriptionsForPage, AtmosphereEvent event)
+		Iterator<EventSubscription> subscriptionsForPage, AtmosphereEvent event)
 	{
 		String filterPath = WebApplication.get()
 			.getWicketFilter()