Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoakim Erdfelt2015-02-18 18:07:21 +0000
committerJoakim Erdfelt2015-03-12 17:09:52 +0000
commit31ee46d4825bec41ab0bd7aff2a458936b517321 (patch)
tree3f8847fb55aa149b848687a24bcb5834f71aea7a /jetty-websocket
parent3008ab85e543b46bff16f7193d19413f8a000b4b (diff)
downloadorg.eclipse.jetty.project-31ee46d4825bec41ab0bd7aff2a458936b517321.tar.gz
org.eclipse.jetty.project-31ee46d4825bec41ab0bd7aff2a458936b517321.tar.xz
org.eclipse.jetty.project-31ee46d4825bec41ab0bd7aff2a458936b517321.zip
453834 - CDI Support for WebSocket
+ Breaking down jetty-cdi into 3 modules * cdi-core * cdi-servlet * cdi-websocket + Creating WebSocketScope for cdi-websocket + Creating @Produces for jetty websocket api session and javax.websocket.Session + Unit tests for new functionality
Diffstat (limited to 'jetty-websocket')
-rw-r--r--jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java26
-rw-r--r--jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JsrBasicRemote.java1
-rw-r--r--jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java31
-rw-r--r--jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/SimpleContainerScope.java42
-rw-r--r--jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/WebSocketContainerScope.java27
-rw-r--r--jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/WebSocketScopeEvents.java99
-rw-r--r--jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/WebSocketScopeListener.java42
-rw-r--r--jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java32
8 files changed, 298 insertions, 2 deletions
diff --git a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java
index 84e668ace2..0cefc85d53 100644
--- a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java
+++ b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java
@@ -22,6 +22,7 @@ import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.HashSet;
+import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
@@ -58,6 +59,8 @@ import org.eclipse.jetty.websocket.common.SessionListener;
import org.eclipse.jetty.websocket.common.WebSocketSession;
import org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope;
import org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope;
+import org.eclipse.jetty.websocket.common.scopes.WebSocketScopeEvents;
+import org.eclipse.jetty.websocket.common.scopes.WebSocketScopeListener;
import org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner;
import org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata;
import org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig;
@@ -115,6 +118,12 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
ShutdownThread.register(this);
}
+
+ @Override
+ public void addScopeListener(WebSocketScopeListener listener)
+ {
+ this.scopeDelegate.addScopeListener(listener);
+ }
private Session connect(EndpointInstance instance, URI path) throws IOException
{
@@ -341,6 +350,17 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
{
return scopeDelegate.getPolicy();
}
+
+ public WebSocketScopeEvents getScopeEvents()
+ {
+ return scopeDelegate.getScopeEvents();
+ }
+
+ @Override
+ public List<WebSocketScopeListener> getScopeListeners()
+ {
+ return scopeDelegate.getScopeListeners();
+ }
@Override
public SslContextFactory getSslContextFactory()
@@ -405,6 +425,12 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
Session.class.getName());
}
}
+
+ @Override
+ public void removeScopeListener(WebSocketScopeListener listener)
+ {
+ scopeDelegate.removeScopeListener(listener);
+ }
@Override
public void setAsyncSendTimeout(long ms)
diff --git a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JsrBasicRemote.java b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JsrBasicRemote.java
index 9548ee593e..4232e600b6 100644
--- a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JsrBasicRemote.java
+++ b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JsrBasicRemote.java
@@ -100,6 +100,7 @@ public class JsrBasicRemote extends AbstractJsrRemote implements RemoteEndpoint.
@Override
public void sendText(String text) throws IOException
{
+ LOG.info("sendText({})", TextUtil.hint(text));
assertMessageNotNull(text);
if (LOG.isDebugEnabled())
{
diff --git a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java
index e9f192689f..7200921da5 100644
--- a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java
+++ b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java
@@ -24,6 +24,7 @@ import java.net.SocketAddress;
import java.net.URI;
import java.util.Collections;
import java.util.HashSet;
+import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.Executor;
@@ -60,6 +61,8 @@ import org.eclipse.jetty.websocket.common.events.EventDriver;
import org.eclipse.jetty.websocket.common.events.EventDriverFactory;
import org.eclipse.jetty.websocket.common.extensions.WebSocketExtensionFactory;
import org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope;
+import org.eclipse.jetty.websocket.common.scopes.WebSocketScopeEvents;
+import org.eclipse.jetty.websocket.common.scopes.WebSocketScopeListener;
/**
* WebSocketClient provides a means of establishing connections to remote websocket endpoints.
@@ -71,6 +74,7 @@ public class WebSocketClient extends ContainerLifeCycle implements SessionListen
private final WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
private final SslContextFactory sslContextFactory;
private final WebSocketExtensionFactory extensionRegistry;
+ private WebSocketScopeEvents scopeEvents = new WebSocketScopeEvents();
private boolean daemon = false;
private EventDriverFactory eventDriverFactory;
private SessionFactory sessionFactory;
@@ -145,6 +149,12 @@ public class WebSocketClient extends ContainerLifeCycle implements SessionListen
addBean(this.sslContextFactory);
addBean(this.bufferPool);
}
+
+ @Override
+ public void addScopeListener(WebSocketScopeListener listener)
+ {
+ this.scopeEvents.addScopeListener(listener);
+ }
public Future<Session> connect(Object websocket, URI toUri) throws IOException
{
@@ -280,6 +290,8 @@ public class WebSocketClient extends ContainerLifeCycle implements SessionListen
}
super.doStart();
+
+ scopeEvents.fireContainerActivated(this);
if (LOG.isDebugEnabled())
LOG.debug("Started {}",this);
@@ -303,6 +315,8 @@ public class WebSocketClient extends ContainerLifeCycle implements SessionListen
}
super.doStop();
+
+ scopeEvents.fireContainerDeactivated(this);
if (LOG.isDebugEnabled())
LOG.debug("Stopped {}",this);
@@ -438,6 +452,17 @@ public class WebSocketClient extends ContainerLifeCycle implements SessionListen
{
return scheduler;
}
+
+ public WebSocketScopeEvents getScopeEvents()
+ {
+ return scopeEvents;
+ }
+
+ @Override
+ public List<WebSocketScopeListener> getScopeListeners()
+ {
+ return this.scopeEvents.getScopeListeners();
+ }
public SessionFactory getSessionFactory()
{
@@ -505,6 +530,12 @@ public class WebSocketClient extends ContainerLifeCycle implements SessionListen
if (LOG.isDebugEnabled())
LOG.debug("Session Opened: {}",session);
}
+
+ @Override
+ public void removeScopeListener(WebSocketScopeListener listener)
+ {
+ this.scopeEvents.removeScopeListener(listener);
+ }
public void setAsyncWriteTimeout(long ms)
{
diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/SimpleContainerScope.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/SimpleContainerScope.java
index 132925bc98..935583e5f6 100644
--- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/SimpleContainerScope.java
+++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/SimpleContainerScope.java
@@ -18,22 +18,25 @@
package org.eclipse.jetty.websocket.common.scopes;
+import java.util.List;
import java.util.concurrent.Executor;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.MappedByteBufferPool;
import org.eclipse.jetty.util.DecoratedObjectFactory;
+import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
-public class SimpleContainerScope implements WebSocketContainerScope
+public class SimpleContainerScope extends ContainerLifeCycle implements WebSocketContainerScope
{
private final ByteBufferPool bufferPool;
private final DecoratedObjectFactory objectFactory;
private final WebSocketPolicy policy;
private Executor executor;
private SslContextFactory sslContextFactory;
+ private WebSocketScopeEvents scopeEvents = new WebSocketScopeEvents();
public SimpleContainerScope(WebSocketPolicy policy)
{
@@ -59,6 +62,20 @@ public class SimpleContainerScope implements WebSocketContainerScope
}
@Override
+ protected void doStart() throws Exception
+ {
+ super.doStart();
+ scopeEvents.fireContainerActivated(this);
+ }
+
+ @Override
+ protected void doStop() throws Exception
+ {
+ super.doStop();
+ scopeEvents.fireContainerDeactivated(this);
+ }
+
+ @Override
public ByteBufferPool getBufferPool()
{
return this.bufferPool;
@@ -92,4 +109,27 @@ public class SimpleContainerScope implements WebSocketContainerScope
{
this.sslContextFactory = sslContextFactory;
}
+
+ public WebSocketScopeEvents getScopeEvents()
+ {
+ return scopeEvents;
+ }
+
+ @Override
+ public void addScopeListener(WebSocketScopeListener listener)
+ {
+ this.scopeEvents.addScopeListener(listener);
+ }
+
+ @Override
+ public void removeScopeListener(WebSocketScopeListener listener)
+ {
+ this.scopeEvents.removeScopeListener(listener);
+ }
+
+ @Override
+ public List<WebSocketScopeListener> getScopeListeners()
+ {
+ return this.scopeEvents.getScopeListeners();
+ }
}
diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/WebSocketContainerScope.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/WebSocketContainerScope.java
index acc611f778..1f65f1bc26 100644
--- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/WebSocketContainerScope.java
+++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/WebSocketContainerScope.java
@@ -18,6 +18,7 @@
package org.eclipse.jetty.websocket.common.scopes;
+import java.util.List;
import java.util.concurrent.Executor;
import org.eclipse.jetty.io.ByteBufferPool;
@@ -31,6 +32,32 @@ import org.eclipse.jetty.websocket.api.WebSocketPolicy;
public interface WebSocketContainerScope
{
/**
+ * Add a {@link WebSocketScopeListener} for this container.
+ *
+ * @param listener
+ * the listener to receive container scope events
+ */
+ public void addScopeListener(WebSocketScopeListener listener);
+
+ /**
+ * Remove a {@link WebSocketScopeListener} from this container
+ *
+ * @param listener
+ * the listener to receive websocket scope events
+ */
+ public void removeScopeListener(WebSocketScopeListener listener);
+
+ /**
+ * Get the current list of {@link WebSocketScopeListener}s for this container
+ */
+ public List<WebSocketScopeListener> getScopeListeners();
+
+ /**
+ * Get the WebSocketScopeEvents handler, for firing {@link WebSocketScopeListener} events.
+ */
+ public WebSocketScopeEvents getScopeEvents();
+
+ /**
* The configured Container Buffer Pool.
*
* @return the buffer pool (never null)
diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/WebSocketScopeEvents.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/WebSocketScopeEvents.java
new file mode 100644
index 0000000000..6294aa3bc2
--- /dev/null
+++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/WebSocketScopeEvents.java
@@ -0,0 +1,99 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+package org.eclipse.jetty.websocket.common.scopes;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class WebSocketScopeEvents
+{
+ private List<WebSocketScopeListener> scopeListeners;
+
+ public void addScopeListener(WebSocketScopeListener listener)
+ {
+ if (scopeListeners == null)
+ {
+ scopeListeners = new ArrayList<>();
+ }
+ scopeListeners.add(listener);
+ }
+
+ public void fireContainerActivated(WebSocketContainerScope container)
+ {
+ if (scopeListeners != null)
+ {
+ for (WebSocketScopeListener listener : scopeListeners)
+ {
+ listener.onWebSocketContainerActivated(container);
+ }
+ }
+ }
+
+ public void fireContainerDeactivated(WebSocketContainerScope container)
+ {
+ if (scopeListeners != null)
+ {
+ for (WebSocketScopeListener listener : scopeListeners)
+ {
+ listener.onWebSocketContainerDeactivated(container);
+ }
+ }
+ }
+
+ public void fireSessionActivated(WebSocketSessionScope session)
+ {
+ if (scopeListeners != null)
+ {
+ for (WebSocketScopeListener listener : scopeListeners)
+ {
+ listener.onWebSocketSessionActivated(session);
+ }
+ }
+ }
+
+ public void fireSessionDeactivated(WebSocketSessionScope session)
+ {
+ if (scopeListeners != null)
+ {
+ for (WebSocketScopeListener listener : scopeListeners)
+ {
+ listener.onWebSocketSessionDeactivated(session);
+ }
+ }
+ }
+
+ public List<WebSocketScopeListener> getScopeListeners()
+ {
+ if (scopeListeners == null)
+ {
+ return Collections.emptyList();
+ }
+ return Collections.unmodifiableList(scopeListeners);
+ }
+
+ public void removeScopeListener(WebSocketScopeListener listener)
+ {
+ if (scopeListeners == null)
+ {
+ return;
+ }
+ scopeListeners.remove(listener);
+ }
+}
diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/WebSocketScopeListener.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/WebSocketScopeListener.java
new file mode 100644
index 0000000000..5aca114e9b
--- /dev/null
+++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/scopes/WebSocketScopeListener.java
@@ -0,0 +1,42 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+package org.eclipse.jetty.websocket.common.scopes;
+
+public interface WebSocketScopeListener
+{
+ /**
+ * A WebSocket Container scope was created / activated
+ */
+ void onWebSocketContainerActivated(WebSocketContainerScope scope);
+
+ /**
+ * A WebSocket Container scope was stopped / deactivated
+ */
+ void onWebSocketContainerDeactivated(WebSocketContainerScope scope);
+
+ /**
+ * A WebSocket Session scope was created / activated
+ */
+ void onWebSocketSessionActivated(WebSocketSessionScope scope);
+
+ /**
+ * A WebSocket Session scope was stopped / deactivated
+ */
+ void onWebSocketSessionDeactivated(WebSocketSessionScope scope);
+}
diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java
index 1ca3805746..62d5738164 100644
--- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java
+++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java
@@ -66,6 +66,8 @@ import org.eclipse.jetty.websocket.common.events.EventDriverFactory;
import org.eclipse.jetty.websocket.common.extensions.ExtensionStack;
import org.eclipse.jetty.websocket.common.extensions.WebSocketExtensionFactory;
import org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope;
+import org.eclipse.jetty.websocket.common.scopes.WebSocketScopeEvents;
+import org.eclipse.jetty.websocket.common.scopes.WebSocketScopeListener;
import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
import org.eclipse.jetty.websocket.servlet.WebSocketCreator;
@@ -95,6 +97,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
private WebSocketCreator creator;
private List<Class<?>> registeredSocketClasses;
private DecoratedObjectFactory objectFactory;
+ private WebSocketScopeEvents scopeEvents = new WebSocketScopeEvents();
public WebSocketServerFactory()
{
@@ -205,6 +208,12 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
Thread.currentThread().setContextClassLoader(old);
}
}
+
+ @Override
+ public void addScopeListener(WebSocketScopeListener listener)
+ {
+ this.scopeEvents.addScopeListener(listener);
+ }
public void addSessionFactory(SessionFactory sessionFactory)
{
@@ -316,6 +325,8 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
}
super.doStart();
+
+ scopeEvents.fireContainerActivated(this);
}
@Override
@@ -323,6 +334,8 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
{
shutdownAllConnections();
super.doStop();
+
+ scopeEvents.fireContainerDeactivated(this);
}
@Override
@@ -369,6 +382,17 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
{
return defaultPolicy;
}
+
+ public WebSocketScopeEvents getScopeEvents()
+ {
+ return scopeEvents;
+ }
+
+ @Override
+ public List<WebSocketScopeListener> getScopeListeners()
+ {
+ return this.scopeEvents.getScopeListeners();
+ }
@Override
public SslContextFactory getSslContextFactory()
@@ -515,7 +539,13 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
{
registeredSocketClasses.add(websocketPojo);
}
-
+
+ @Override
+ public void removeScopeListener(WebSocketScopeListener listener)
+ {
+ this.scopeEvents.removeScopeListener(listener);
+ }
+
@Override
public void setCreator(WebSocketCreator creator)
{

Back to the top