Skip to content

Package: ContainerProvider

ContainerProvider

nameinstructionbranchcomplexitylinemethod
ContainerProvider()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getWebSocketContainer()
M: 31 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2018, 2020 Oracle and/or its affiliates and others.
3: * All rights reserved.
4: *
5: * This program and the accompanying materials are made available under the
6: * terms of the Eclipse Public License v. 2.0, which is available at
7: * http://www.eclipse.org/legal/epl-2.0.
8: *
9: * This Source Code may also be made available under the following Secondary
10: * Licenses when the conditions for such availability set forth in the
11: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
12: * version 2 with the GNU Classpath Exception, which is available at
13: * https://www.gnu.org/software/classpath/license.html.
14: *
15: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16: */
17:
18: package jakarta.websocket;
19:
20: import java.util.Iterator;
21: import java.util.ServiceConfigurationError;
22: import java.util.ServiceLoader;
23:
24: /**
25: * Provider class that allows the developer to get a reference to the implementation of the WebSocketContainer. The
26: * provider class uses the
27: * <a href="http://docs.oracle.com/javase/7/docs/api/java/util/ServiceLoader.html">ServiceLoader</a> to load an
28: * implementation of ContainerProvider. Specifically, the fully qualified class name of the container implementation of
29: * ContainerProvider must be listed in the "META-INF/services/jakarta.websocket.ContainerProvider" file in the
30: * implementation JAR file.
31: *
32: * @author dannycoward
33: */
34: public abstract class ContainerProvider {
35:
36: /**
37: * Obtain a new instance of a WebSocketContainer. The method looks for the ContainerProvider implementation class in
38: * the order listed in the "META-INF/services/jakarta.websocket.ContainerProvider" file, returning the first
39: * WebSocketContainer implementation from the ContainerProvider implementation that is not {@code null}.
40: *
41: * @return an implementation provided instance of type WebSocketContainer
42: *
43: * @throws ServiceConfigurationError If there is a problem loading one of the discovered ContainerProvider
44: * implementations. A ServiceConfigurationError is viewed as a serious problem so the exception is allowed
45: * to propagate rather than swallowing the exception and attempting to load the next provider (if any).
46: */
47: public static WebSocketContainer getWebSocketContainer() {
48: Iterator<ContainerProvider> providers = ServiceLoader.load(ContainerProvider.class).iterator();
49:• if (providers.hasNext()) {
50: do {
51: ContainerProvider impl = providers.next();
52: WebSocketContainer wsc = impl.getContainer();
53:• if (wsc != null) {
54: return wsc;
55: }
56:• } while (providers.hasNext());
57: throw new RuntimeException("Could not find an implementation class with a non-null WebSocketContainer.");
58: } else {
59: throw new RuntimeException("Could not find an implementation class.");
60: }
61: }
62:
63: /**
64: * Create a new instance of the the WebSocket container implementation.
65: *
66: * @return the new instance
67: */
68: protected abstract WebSocketContainer getContainer();
69: }