Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoakim Erdfelt2015-03-10 17:27:07 +0000
committerJoakim Erdfelt2015-03-12 17:09:52 +0000
commitdc81cda742b354138fd3f78c815aa9d81ae2e078 (patch)
tree87495872a728cfe11340aedce04b7ea1d05d70e4 /jetty-cdi
parent31ee46d4825bec41ab0bd7aff2a458936b517321 (diff)
downloadorg.eclipse.jetty.project-dc81cda742b354138fd3f78c815aa9d81ae2e078.tar.gz
org.eclipse.jetty.project-dc81cda742b354138fd3f78c815aa9d81ae2e078.tar.xz
org.eclipse.jetty.project-dc81cda742b354138fd3f78c815aa9d81ae2e078.zip
Using Jetty component listeners for CDI
Diffstat (limited to 'jetty-cdi')
-rw-r--r--jetty-cdi/cdi-servlet/src/main/java/org/eclipse/jetty/cdi/servlet/JettyWeldInitializer.java2
-rw-r--r--jetty-cdi/cdi-websocket/src/main/java/org/eclipse/jetty/cdi/websocket/WebSocketCdiInitializer.java74
-rw-r--r--jetty-cdi/cdi-websocket/src/main/java/org/eclipse/jetty/cdi/websocket/WebSocketCdiListener.java138
-rw-r--r--jetty-cdi/cdi-websocket/src/main/java/org/eclipse/jetty/cdi/websocket/WebSocketScopeContext.java4
-rw-r--r--jetty-cdi/cdi-websocket/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer1
-rw-r--r--jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/basicapp/BasicAppTest.java1
-rw-r--r--jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/CdiAppTest.java32
-rw-r--r--jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/DataMaker.java7
-rw-r--r--jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/EchoSocket.java57
-rw-r--r--jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/InfoSocket.java6
10 files changed, 315 insertions, 7 deletions
diff --git a/jetty-cdi/cdi-servlet/src/main/java/org/eclipse/jetty/cdi/servlet/JettyWeldInitializer.java b/jetty-cdi/cdi-servlet/src/main/java/org/eclipse/jetty/cdi/servlet/JettyWeldInitializer.java
index 08694d2310..ac243cc414 100644
--- a/jetty-cdi/cdi-servlet/src/main/java/org/eclipse/jetty/cdi/servlet/JettyWeldInitializer.java
+++ b/jetty-cdi/cdi-servlet/src/main/java/org/eclipse/jetty/cdi/servlet/JettyWeldInitializer.java
@@ -57,8 +57,6 @@ public class JettyWeldInitializer
public static void initContext(ContextHandler handler) throws NamingException
{
- // TODO: handler.addLifeCycleListener(new WebSocketServerLifecycleListener(handler));
-
// Add context specific weld container reference.
// See https://issues.jboss.org/browse/WELD-1710
// and https://github.com/weld/core/blob/2.2.5.Final/environments/servlet/core/src/main/java/org/jboss/weld/environment/servlet/WeldServletLifecycle.java#L244-L253
diff --git a/jetty-cdi/cdi-websocket/src/main/java/org/eclipse/jetty/cdi/websocket/WebSocketCdiInitializer.java b/jetty-cdi/cdi-websocket/src/main/java/org/eclipse/jetty/cdi/websocket/WebSocketCdiInitializer.java
new file mode 100644
index 0000000000..e0a5bb742b
--- /dev/null
+++ b/jetty-cdi/cdi-websocket/src/main/java/org/eclipse/jetty/cdi/websocket/WebSocketCdiInitializer.java
@@ -0,0 +1,74 @@
+//
+// ========================================================================
+// 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.cdi.websocket;
+
+import java.util.Set;
+
+import javax.servlet.ServletContainerInitializer;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+
+import org.eclipse.jetty.server.handler.ContextHandler;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.util.component.ContainerLifeCycle;
+import org.eclipse.jetty.websocket.common.util.ThreadClassLoaderScope;
+
+public class WebSocketCdiInitializer implements ServletContainerInitializer
+{
+ public static void configureContext(ServletContextHandler context) throws ServletException
+ {
+ try (ThreadClassLoaderScope scope = new ThreadClassLoaderScope(context.getClassLoader()))
+ {
+ addListeners(context);
+ }
+ }
+
+ @Override
+ public void onStartup(Set<Class<?>> c, ServletContext context) throws ServletException
+ {
+ ContextHandler handler = ContextHandler.getContextHandler(context);
+
+ if (handler == null)
+ {
+ throw new ServletException("Not running on Jetty, WebSocket+CDI support unavailable");
+ }
+
+ if (!(handler instanceof ServletContextHandler))
+ {
+ throw new ServletException("Not running in Jetty ServletContextHandler, WebSocket+CDI support unavailable");
+ }
+
+ ServletContextHandler jettyContext = (ServletContextHandler)handler;
+ try (ThreadClassLoaderScope scope = new ThreadClassLoaderScope(context.getClassLoader()))
+ {
+ addListeners(jettyContext);
+ }
+ }
+
+ private static void addListeners(ContainerLifeCycle container)
+ {
+ WebSocketCdiListener listener = new WebSocketCdiListener(container);
+
+ if (listener != null)
+ {
+ container.addLifeCycleListener(listener);
+ container.addEventListener(listener);
+ }
+ }
+}
diff --git a/jetty-cdi/cdi-websocket/src/main/java/org/eclipse/jetty/cdi/websocket/WebSocketCdiListener.java b/jetty-cdi/cdi-websocket/src/main/java/org/eclipse/jetty/cdi/websocket/WebSocketCdiListener.java
new file mode 100644
index 0000000000..bee9002d38
--- /dev/null
+++ b/jetty-cdi/cdi-websocket/src/main/java/org/eclipse/jetty/cdi/websocket/WebSocketCdiListener.java
@@ -0,0 +1,138 @@
+package org.eclipse.jetty.cdi.websocket;
+
+import java.util.Set;
+
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.CDI;
+
+import org.eclipse.jetty.cdi.core.AnyLiteral;
+import org.eclipse.jetty.cdi.core.ScopedInstance;
+import org.eclipse.jetty.util.component.Container;
+import org.eclipse.jetty.util.component.ContainerLifeCycle;
+import org.eclipse.jetty.util.component.LifeCycle;
+import org.eclipse.jetty.util.log.Log;
+import org.eclipse.jetty.util.log.Logger;
+import org.eclipse.jetty.websocket.api.Session;
+import org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope;
+import org.eclipse.jetty.websocket.common.scopes.WebSocketSessionScope;
+
+public class WebSocketCdiListener implements LifeCycle.Listener, Container.InheritedListener
+{
+ private static final Logger LOG = Log.getLogger(WebSocketCdiListener.class);
+ private final ContainerLifeCycle container;
+
+ public WebSocketCdiListener(ContainerLifeCycle container)
+ {
+ this.container = container;
+ }
+
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ public static <T> ScopedInstance<T> newInstance(Class<T> clazz)
+ {
+ BeanManager bm = CDI.current().getBeanManager();
+
+ ScopedInstance sbean = new ScopedInstance();
+ Set<Bean<?>> beans = bm.getBeans(clazz,AnyLiteral.INSTANCE);
+ if (beans.size() > 0)
+ {
+ sbean.bean = beans.iterator().next();
+ sbean.creationalContext = bm.createCreationalContext(sbean.bean);
+ sbean.instance = bm.getReference(sbean.bean,clazz,sbean.creationalContext);
+ return sbean;
+ }
+ else
+ {
+ throw new RuntimeException(String.format("Can't find class %s",clazz));
+ }
+ }
+
+ private ScopedInstance<WebSocketScopeContext> wsScope;
+
+ private synchronized ScopedInstance<WebSocketScopeContext> getWebSocketScope()
+ {
+ if (wsScope == null)
+ {
+ wsScope = newInstance(WebSocketScopeContext.class);
+ }
+ return wsScope;
+ }
+
+ @Override
+ public void lifeCycleStarting(LifeCycle event)
+ {
+ if (event instanceof WebSocketContainerScope)
+ {
+ if (LOG.isDebugEnabled())
+ {
+ LOG.debug("starting websocket container [{}]", event);
+ }
+ getWebSocketScope().instance.begin();
+ }
+ else if (event instanceof WebSocketSessionScope)
+ {
+ if (LOG.isDebugEnabled())
+ {
+ LOG.debug("starting websocket session [{}]", event);
+ }
+ getWebSocketScope().instance.setSession((Session)event);
+ }
+ }
+
+ @Override
+ public void lifeCycleStarted(LifeCycle event)
+ {
+ }
+
+ @Override
+ public void lifeCycleFailure(LifeCycle event, Throwable cause)
+ {
+ }
+
+ @Override
+ public void lifeCycleStopping(LifeCycle event)
+ {
+ }
+
+ @Override
+ public void lifeCycleStopped(LifeCycle event)
+ {
+ if (event instanceof WebSocketContainerScope)
+ {
+ if (LOG.isDebugEnabled())
+ {
+ LOG.debug("stopped websocket container [{}]", event);
+ }
+ getWebSocketScope().instance.end();
+ }
+ else if (event == container)
+ {
+ if (LOG.isDebugEnabled())
+ {
+ LOG.debug("stopped parent container [{}]", event);
+ }
+ if (wsScope != null)
+ {
+ wsScope.destroy();
+ }
+ }
+ }
+
+ @Override
+ public void beanAdded(Container parent, Object child)
+ {
+ if (child instanceof LifeCycle)
+ {
+ ((LifeCycle)child).addLifeCycleListener(this);
+ }
+ }
+
+ @Override
+ public void beanRemoved(Container parent, Object child)
+ {
+ if (child instanceof LifeCycle)
+ {
+ ((LifeCycle)child).removeLifeCycleListener(this);
+ }
+ }
+}
diff --git a/jetty-cdi/cdi-websocket/src/main/java/org/eclipse/jetty/cdi/websocket/WebSocketScopeContext.java b/jetty-cdi/cdi-websocket/src/main/java/org/eclipse/jetty/cdi/websocket/WebSocketScopeContext.java
index 561e6fb043..111a513a2d 100644
--- a/jetty-cdi/cdi-websocket/src/main/java/org/eclipse/jetty/cdi/websocket/WebSocketScopeContext.java
+++ b/jetty-cdi/cdi-websocket/src/main/java/org/eclipse/jetty/cdi/websocket/WebSocketScopeContext.java
@@ -52,7 +52,7 @@ public class WebSocketScopeContext
public void begin()
{
- LOG.debug("begin()");
+ LOG.debug("begin()", new Throwable("Trace"));
if (state.get() != null)
{
throw new IllegalAccessError("Already in WebSocketScope");
@@ -102,7 +102,7 @@ public class WebSocketScopeContext
public void setSession(Session sess)
{
- LOG.debug("setSession()");
+ LOG.debug("setSession({})",sess);
jettySessionProducer.setSession(sess);
if(sess instanceof javax.websocket.Session)
{
diff --git a/jetty-cdi/cdi-websocket/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer b/jetty-cdi/cdi-websocket/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer
new file mode 100644
index 0000000000..f527270e52
--- /dev/null
+++ b/jetty-cdi/cdi-websocket/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer
@@ -0,0 +1 @@
+org.eclipse.jetty.cdi.websocket.WebSocketCdiInitializer \ No newline at end of file
diff --git a/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/basicapp/BasicAppTest.java b/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/basicapp/BasicAppTest.java
index e531f2aefa..cd9b2354d4 100644
--- a/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/basicapp/BasicAppTest.java
+++ b/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/basicapp/BasicAppTest.java
@@ -49,6 +49,7 @@ public class BasicAppTest
private static final Logger LOG = Log.getLogger(BasicAppTest.class);
private static Server server;
+ @SuppressWarnings("unused")
private static URI serverHttpURI;
private static URI serverWebsocketURI;
diff --git a/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/CdiAppTest.java b/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/CdiAppTest.java
index 26c89d1140..f6d984973e 100644
--- a/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/CdiAppTest.java
+++ b/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/CdiAppTest.java
@@ -29,6 +29,7 @@ import javax.websocket.server.ServerContainer;
import org.eclipse.jetty.cdi.servlet.EmbeddedCdiHandler;
import org.eclipse.jetty.cdi.websocket.CheckSocket;
+import org.eclipse.jetty.cdi.websocket.WebSocketCdiInitializer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
@@ -60,6 +61,7 @@ public class CdiAppTest
server.addConnector(connector);
EmbeddedCdiHandler context = new EmbeddedCdiHandler();
+ WebSocketCdiInitializer.configureContext(context);
File baseDir = MavenTestingUtils.getTestResourcesDir();
@@ -69,6 +71,7 @@ public class CdiAppTest
// Add some websockets
ServerContainer container = WebSocketServerContainerInitializer.configureContext(context);
+ container.addEndpoint(EchoSocket.class);
container.addEndpoint(InfoSocket.class);
server.start();
@@ -120,7 +123,7 @@ public class CdiAppTest
}
@Test
- public void testWebSocketInfo() throws Exception
+ public void testWebSocket_Info_FieldPresence() throws Exception
{
WebSocketClient client = new WebSocketClient();
try
@@ -150,4 +153,31 @@ public class CdiAppTest
client.stop();
}
}
+
+ @Test
+ public void testWebSocket_Info_DataFromCdi() throws Exception
+ {
+ WebSocketClient client = new WebSocketClient();
+ try
+ {
+ client.start();
+ CheckSocket socket = new CheckSocket();
+ client.connect(socket,serverWebsocketURI.resolve("/cdi-info"));
+
+ socket.awaitOpen(2,TimeUnit.SECONDS);
+ socket.sendText("data|stuff");
+ socket.close(StatusCode.NORMAL,"Test complete");
+ socket.awaitClose(2,TimeUnit.SECONDS);
+
+ assertThat("Messages received",socket.getTextMessages().size(),is(1));
+ String response = socket.getTextMessages().poll();
+ System.err.println(response);
+
+ assertThat("Message[0]",response,containsString("Hello there data"));
+ }
+ finally
+ {
+ client.stop();
+ }
+ }
}
diff --git a/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/DataMaker.java b/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/DataMaker.java
index 0d6cbcc8de..cd78073329 100644
--- a/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/DataMaker.java
+++ b/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/DataMaker.java
@@ -20,15 +20,22 @@ package org.eclipse.jetty.cdi.websocket.cdiapp;
import javax.inject.Inject;
+import org.eclipse.jetty.util.log.Log;
+import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.Session;
public class DataMaker
{
+ private static final Logger LOG = Log.getLogger(DataMaker.class);
+
@Inject
private Session session;
public void processMessage(String msg)
{
+ LOG.debug(".processMessage({})",msg);
+ LOG.debug("session = {}",session);
+
session.getRemote().sendStringByFuture("Hello there " + msg);
}
}
diff --git a/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/EchoSocket.java b/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/EchoSocket.java
new file mode 100644
index 0000000000..264b265291
--- /dev/null
+++ b/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/EchoSocket.java
@@ -0,0 +1,57 @@
+//
+// ========================================================================
+// 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.cdi.websocket.cdiapp;
+
+import javax.websocket.CloseReason;
+import javax.websocket.OnClose;
+import javax.websocket.OnMessage;
+import javax.websocket.OnOpen;
+import javax.websocket.Session;
+import javax.websocket.server.ServerEndpoint;
+
+import org.eclipse.jetty.util.log.Log;
+import org.eclipse.jetty.util.log.Logger;
+
+@ServerEndpoint("/echo")
+public class EchoSocket
+{
+ private static final Logger LOG = Log.getLogger(EchoSocket.class);
+ @SuppressWarnings("unused")
+ private Session session;
+
+ @OnOpen
+ public void onOpen(Session session)
+ {
+ LOG.debug("onOpen(): {}",session);
+ this.session = session;
+ }
+
+ @OnClose
+ public void onClose(CloseReason close)
+ {
+ LOG.debug("onClose(): {}",close);
+ this.session = null;
+ }
+
+ @OnMessage
+ public String onMessage(String msg)
+ {
+ return msg;
+ }
+}
diff --git a/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/InfoSocket.java b/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/InfoSocket.java
index 31cf6cb44c..e724edd8f7 100644
--- a/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/InfoSocket.java
+++ b/jetty-cdi/cdi-websocket/src/test/java/org/eclipse/jetty/cdi/websocket/cdiapp/InfoSocket.java
@@ -69,8 +69,10 @@ public class InfoSocket
{
StringWriter str = new StringWriter();
PrintWriter out = new PrintWriter(str);
+
+ String args[] = msg.split("\\|");
- switch (msg)
+ switch (args[0])
{
case "info":
out.printf("websocketSession is %s%n",asPresent(session));
@@ -78,7 +80,7 @@ public class InfoSocket
out.printf("servletContext is %s%n",asPresent(servletContext));
break;
case "data":
- dataMaker.processMessage(msg);
+ dataMaker.processMessage(args[1]);
break;
}

Back to the top