diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/component/Container.java b/jetty-util/src/main/java/org/eclipse/jetty/util/component/Container.java
index bfeb11a1f08..6149ef4797d 100644
--- a/jetty-util/src/main/java/org/eclipse/jetty/util/component/Container.java
+++ b/jetty-util/src/main/java/org/eclipse/jetty/util/component/Container.java
@@ -152,6 +152,46 @@ public interface Container
return Collections.unmodifiableList(new ArrayList<>(getBeans(EventListener.class)));
}
+ /**
+ * A utility method to add a bean to a container.
+ * @param parent the parent container.
+ * @param child the child bean.
+ * @return true if the child was added as a bean, false if parent was not instance of {@link Container} or bean was already present.
+ */
+ static boolean addBean(Object parent, Object child)
+ {
+ if (parent instanceof Container)
+ return ((Container)parent).addBean(child);
+ return false;
+ }
+
+ /**
+ * A utility method to add a bean to a container.
+ * @param parent the parent container.
+ * @param child the child bean.
+ * @param managed whether to managed the lifecycle of the bean.
+ * @return true if the child was added as a bean, false if parent was not instance of {@link Container} or bean was already present.
+ */
+ static boolean addBean(Object parent, Object child, boolean managed)
+ {
+ if (parent instanceof Container)
+ return ((Container)parent).addBean(child, managed);
+ return false;
+ }
+
+ /**
+ * A utility method to remove a bean from a container.
+ * @param parent the parent container.
+ * @param child the child bean.
+ * @return true if parent was an instance of {@link Container} and the bean was removed.
+ */
+ static boolean removeBean(Object parent, Object child)
+ {
+ if (parent instanceof Container)
+ return ((Container)parent).removeBean(child);
+ return false;
+ }
+
/**
* A listener for Container events.
* If an added bean implements this interface it will receive the events
diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/component/ContainerLifeCycle.java b/jetty-util/src/main/java/org/eclipse/jetty/util/component/ContainerLifeCycle.java
index 115dd4f2bc8..2054efaa526 100644
--- a/jetty-util/src/main/java/org/eclipse/jetty/util/component/ContainerLifeCycle.java
+++ b/jetty-util/src/main/java/org/eclipse/jetty/util/component/ContainerLifeCycle.java
@@ -474,9 +474,9 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
if (listener instanceof InheritedListener && b.isManaged() && b._bean instanceof Container)
{
if (b._bean instanceof ContainerLifeCycle)
- ((ContainerLifeCycle)b._bean).addBean(listener, false);
+ Container.addBean(b._bean, listener, false);
else
- ((Container)b._bean).addBean(listener);
+ Container.addBean(b._bean, listener);
}
}
}
@@ -499,8 +499,8 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
{
cl.beanRemoved(this, b._bean);
- if (listener instanceof InheritedListener && b.isManaged() && b._bean instanceof Container)
- ((Container)b._bean).removeBean(listener);
+ if (listener instanceof InheritedListener && b.isManaged())
+ Container.removeBean(b._bean, listener);
}
}
return true;
@@ -541,9 +541,9 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
if (l instanceof InheritedListener)
{
if (bean._bean instanceof ContainerLifeCycle)
- ((ContainerLifeCycle)bean._bean).addBean(l, false);
+ Container.addBean(bean._bean, l, false);
else
- ((Container)bean._bean).addBean(l);
+ Container.addBean(bean._bean, l);
}
}
}
@@ -579,7 +579,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
for (Container.Listener l : _listeners)
{
if (l instanceof InheritedListener)
- ((Container)bean._bean).removeBean(l);
+ Container.removeBean(bean._bean, l);
}
}
bean._managed = Managed.UNMANAGED;
diff --git a/jetty-websocket/websocket-javax-client/pom.xml b/jetty-websocket/websocket-javax-client/pom.xml
index d7380a38d3a..b85ecaf4d19 100644
--- a/jetty-websocket/websocket-javax-client/pom.xml
+++ b/jetty-websocket/websocket-javax-client/pom.xml
@@ -45,6 +45,17 @@
${project.version}
test
+
+ org.eclipse.jetty
+ jetty-jmx
+ ${project.version}
+ test
+
+
+ org.eclipse.jetty
+ jetty-slf4j-impl
+ test
+
diff --git a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java
index d5d6fb99494..781fb87554d 100644
--- a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java
+++ b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java
@@ -60,7 +60,7 @@ public class JavaxWebSocketClientContainerProvider extends ContainerProvider
* @param httpClient a pre-configured {@link HttpClient} to be used by the implementation.
* @see #getContainer()
*/
- public WebSocketContainer getContainer(HttpClient httpClient)
+ public static WebSocketContainer getContainer(HttpClient httpClient)
{
JavaxWebSocketClientContainer clientContainer = new JavaxWebSocketClientContainer(httpClient);
// See: https://github.com/eclipse-ee4j/websocket-api/issues/212
diff --git a/jetty-websocket/websocket-javax-client/src/test/java/examples/SecureClientContainerExample.java b/jetty-websocket/websocket-javax-client/src/test/java/examples/SecureClientContainerExample.java
index 7e52cf34e32..cafd4309a3e 100644
--- a/jetty-websocket/websocket-javax-client/src/test/java/examples/SecureClientContainerExample.java
+++ b/jetty-websocket/websocket-javax-client/src/test/java/examples/SecureClientContainerExample.java
@@ -13,6 +13,7 @@
package examples;
+import java.lang.management.ManagementFactory;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import javax.websocket.ClientEndpointConfig;
@@ -21,9 +22,11 @@ import javax.websocket.WebSocketContainer;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.http.HttpClientTransportOverHTTP;
import org.eclipse.jetty.io.ClientConnector;
+import org.eclipse.jetty.jmx.MBeanContainer;
+import org.eclipse.jetty.util.component.Container;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.ssl.SslContextFactory;
-import org.eclipse.jetty.websocket.javax.client.internal.JavaxWebSocketClientContainer;
+import org.eclipse.jetty.websocket.javax.client.JavaxWebSocketClientContainerProvider;
public class SecureClientContainerExample
{
@@ -76,9 +79,12 @@ public class SecureClientContainerExample
clientConnector.setSslContextFactory(ssl);
HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP(clientConnector));
- JavaxWebSocketClientContainer clientContainer = new JavaxWebSocketClientContainer(httpClient);
- clientContainer.addManaged(httpClient); // allow clientContainer to own httpClient (for start/stop lifecycle)
- clientContainer.start();
+ WebSocketContainer clientContainer = JavaxWebSocketClientContainerProvider.getContainer(httpClient);
+
+ // Components can be added as a bean to the WebSocketContainer with the Container static method.
+ MBeanContainer mbeanContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
+ Container.addBean(clientContainer, mbeanContainer);
+
return clientContainer;
}
}