/
JavaxWebSocketClientContainerProvider.java
70 lines (64 loc) · 2.67 KB
/
JavaxWebSocketClientContainerProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.client;
import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.websocket.javax.client.internal.JavaxWebSocketClientContainer;
/**
* Client {@link ContainerProvider} implementation.
* <p>
* Created by a {@link java.util.ServiceLoader} call in the
* {@link javax.websocket.ContainerProvider#getWebSocketContainer()} call.
* </p>
*/
public class JavaxWebSocketClientContainerProvider extends ContainerProvider
{
public static void stop(WebSocketContainer container) throws Exception
{
if (container instanceof LifeCycle)
{
((LifeCycle)container).stop();
}
}
/**
* Used by {@link ContainerProvider#getWebSocketContainer()} to get a <b>NEW INSTANCE</b>
* of the Client {@link WebSocketContainer}.
* <p>
* <em>NOTE: A WebSocket Client Container is a heavyweight object.</em>
* It is dangerous to repeatedly request a new container, or to manage many containers.
* The existing javax.websocket API has no lifecycle for a ClientContainer, once started
* they exist for the duration of the JVM with no ability to stop them.
* See/Comment on <a href="https://github.com/javaee/websocket-spec/issues/212">javax.websocket Issue #212</a>
* if this is a big concern for you.
* </p>
*/
@Override
protected WebSocketContainer getContainer()
{
return getContainer(null);
}
/**
* Get a new instance of a client {@link WebSocketContainer} which uses a supplied {@link HttpClient}.
* @param httpClient a pre-configured {@link HttpClient} to be used by the implementation.
* @see #getContainer()
*/
public WebSocketContainer getContainer(HttpClient httpClient)
{
JavaxWebSocketClientContainer clientContainer = new JavaxWebSocketClientContainer(httpClient);
// See: https://github.com/eclipse-ee4j/websocket-api/issues/212
LifeCycle.start(clientContainer);
return clientContainer;
}
}