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/14 16:20:24 UTC

git commit: WICKET-4660 Make it possible to notify about Atmosphere internal events

Repository: wicket
Updated Branches:
  refs/heads/wicket-6.x d817855dd -> 39c2b674a


WICKET-4660 Make it possible to notify about Atmosphere internal events


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

Branch: refs/heads/wicket-6.x
Commit: 39c2b674abf79f458417450c37010a353c70917e
Parents: d817855
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Thu Aug 14 16:20:10 2014 +0200
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Thu Aug 14 16:20:10 2014 +0200

----------------------------------------------------------------------
 .../wicket/examples/atmosphere/HomePage.java    |  7 ++
 .../wicket/atmosphere/AtmosphereBehavior.java   | 24 +++++-
 .../atmosphere/AtmosphereInternalEvent.java     | 88 ++++++++++++++++++++
 .../org/apache/wicket/atmosphere/EventBus.java  | 36 ++++++++
 4 files changed, 154 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/39c2b674/wicket-examples/src/main/java/org/apache/wicket/examples/atmosphere/HomePage.java
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/atmosphere/HomePage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/atmosphere/HomePage.java
index f475d29..ccfbb81 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/atmosphere/HomePage.java
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/atmosphere/HomePage.java
@@ -21,6 +21,7 @@ import java.util.Date;
 import org.apache.wicket.Component;
 import org.apache.wicket.ajax.AjaxRequestTarget;
 import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
+import org.apache.wicket.atmosphere.AtmosphereInternalEvent;
 import org.apache.wicket.atmosphere.EventBus;
 import org.apache.wicket.atmosphere.Subscribe;
 import org.apache.wicket.examples.WicketExamplePage;
@@ -83,4 +84,10 @@ public class HomePage extends WicketExamplePage
 		messageLabel.setDefaultModelObject(message.getMessage());
 		target.add(messageLabel);
 	}
+
+	@Subscribe
+	public void internalEvent(AjaxRequestTarget target, AtmosphereInternalEvent message)
+	{
+//		System.err.println(message);
+	}
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/39c2b674/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereBehavior.java
----------------------------------------------------------------------
diff --git a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereBehavior.java b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereBehavior.java
index 1d04911..61d281d 100644
--- a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereBehavior.java
+++ b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereBehavior.java
@@ -143,6 +143,11 @@ public class AtmosphereBehavior extends Behavior
 			Meteor meteor = Meteor.lookup(event.getResource().getRequest());
 			meteor.resume();
 		}
+		EventBus eventBus = findEventBus();
+		if (eventBus.isWantAtmosphereNotifications())
+		{
+			eventBus.post(new AtmosphereInternalEvent(AtmosphereInternalEvent.Type.Broadcast, event));
+		}
 	}
 
 	@Override
@@ -167,6 +172,11 @@ public class AtmosphereBehavior extends Behavior
 			log.debug(String.format("Resuming the %s response from ip %s:%s", transport == null
 				? "websocket" : transport, atmosphereRequest.getRemoteAddr(), atmosphereRequest.getRemotePort()));
 		}
+		EventBus eventBus = findEventBus();
+		if (eventBus.isWantAtmosphereNotifications())
+		{
+			eventBus.post(new AtmosphereInternalEvent(AtmosphereInternalEvent.Type.Resume, event));
+		}
 	}
 
 	@Override
@@ -181,9 +191,15 @@ public class AtmosphereBehavior extends Behavior
 		}
 		// It is possible that the application has already been destroyed, in which case
 		// unregistration is no longer needed
+		EventBus eventBus = findEventBus();
 		if (Application.get(applicationKey) != null)
 		{
-			findEventBus().unregisterConnection(event.getResource().uuid());
+			eventBus.unregisterConnection(event.getResource().uuid());
+		}
+
+		if (eventBus.isWantAtmosphereNotifications())
+		{
+			eventBus.post(new AtmosphereInternalEvent(AtmosphereInternalEvent.Type.Disconnect, event));
 		}
 	}
 
@@ -192,6 +208,12 @@ public class AtmosphereBehavior extends Behavior
 	{
 		Throwable throwable = event.throwable();
 		log.error(throwable.getMessage(), throwable);
+
+		EventBus eventBus = findEventBus();
+		if (eventBus.isWantAtmosphereNotifications())
+		{
+			eventBus.post(new AtmosphereInternalEvent(AtmosphereInternalEvent.Type.Throwable, event));
+		}
 	}
 
 	@Override

http://git-wip-us.apache.org/repos/asf/wicket/blob/39c2b674/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereInternalEvent.java
----------------------------------------------------------------------
diff --git a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereInternalEvent.java b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereInternalEvent.java
new file mode 100644
index 0000000..46b8da6
--- /dev/null
+++ b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereInternalEvent.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.atmosphere;
+
+import org.atmosphere.cpr.AtmosphereResourceEvent;
+
+/**
+ * An event that is broadcasted by the EventBus whenever Atmosphere notifies
+ * {@linkplain org.apache.wicket.atmosphere.AtmosphereBehavior} about {@link org.atmosphere.cpr.AtmosphereResourceEventListener}
+ * events.
+ *
+ * To be notified add a method like the one below to your components:
+ * <pre><code>{@literal
+ *      @Subscribe
+ *      public void anyName(AjaxRequestTarget target, AtmosphereInternalEvent event) {
+ *          switch (event.getType()) {
+ *              case Resume: ...; break;
+ *              case Disconnect: ...; break;
+ *              ....
+ *          }
+ *      }
+ * }</code></pre>
+ *
+ * @see org.apache.wicket.atmosphere.EventBus#wantAtmosphereNotifications
+ * @see org.atmosphere.cpr.AtmosphereResourceEventListener
+ */
+public class AtmosphereInternalEvent
+{
+	/**
+	 * The types enumerating the notification methods in {@link org.atmosphere.cpr.AtmosphereResourceEventListener}
+	 *
+	 * Suspend type is not supported because it is not possible to push messages with suspended connection
+	 */
+	public static enum Type
+	{
+		PreSuspend, /*Suspend,*/ Resume, Disconnect, Broadcast, Throwable, Close
+	}
+
+	/**
+	 * The internal Atmosphere event
+	 */
+	private final AtmosphereResourceEvent event;
+
+	/**
+	 * The type of the notification
+	 */
+	private final Type type;
+
+
+	public AtmosphereInternalEvent(Type type, AtmosphereResourceEvent event)
+	{
+		this.type = type;
+		this.event = event;
+	}
+
+	public AtmosphereResourceEvent getEvent()
+	{
+		return event;
+	}
+
+	public Type getType()
+	{
+		return type;
+	}
+
+	@Override
+	public String toString()
+	{
+		return "AtmosphereInternalEvent{" +
+				"event=" + event +
+				", type=" + type +
+				'}';
+	}
+}

http://git-wip-us.apache.org/repos/asf/wicket/blob/39c2b674/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 b085027..d2b96b5 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
@@ -114,6 +114,16 @@ public class EventBus implements UnboundListener
 	private final AtmosphereParameters parameters = new AtmosphereParameters();
 
 	/**
+	 * A flag indicating whether to be notified about Atmosphere internal events
+	 *
+	 * <strong>Caution:</strong>: enabling this may cause a lot of <em>onBroadcast</em> notifications
+	 *
+	 * @see org.apache.wicket.atmosphere.AtmosphereInternalEvent
+	 * @see org.atmosphere.cpr.AtmosphereResourceEventListener
+	 */
+	private boolean wantAtmosphereNotifications = false;
+
+	/**
 	 * Creates and registers an {@code EventBus} for the given application. The first broadcaster
 	 * returned by the {@code BroadcasterFactory} is used.
 	 *
@@ -493,4 +503,30 @@ public class EventBus implements UnboundListener
 			curListener.resourceUnregistered(uuid);
 		}
 	}
+
+	/**
+	 * @see org.apache.wicket.atmosphere.AtmosphereInternalEvent
+	 * @see org.atmosphere.cpr.AtmosphereResourceEventListener
+	 * @return whether to be notified about Atmosphere internal events or not
+	 */
+	public boolean isWantAtmosphereNotifications()
+	{
+		return wantAtmosphereNotifications;
+	}
+
+	/**
+	 * A flag indicating whether to be notified about Atmosphere internal events
+	 *
+	 * <strong>Caution:</strong>: enabling this may cause a lot of <em>onBroadcast</em> notifications
+	 *
+	 * @param wantAtmosphereNotifications
+	 *          {@code true} to be notified, {@code false} - otherwise
+	 * @see org.apache.wicket.atmosphere.AtmosphereInternalEvent
+	 * @see org.atmosphere.cpr.AtmosphereResourceEventListener
+	 */
+	public EventBus setWantAtmosphereNotifications(boolean wantAtmosphereNotifications)
+	{
+		this.wantAtmosphereNotifications = wantAtmosphereNotifications;
+		return this;
+	}
 }