From 3d74148652a617e2e3b1d9150da6b39c2e82a910 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Thu, 12 Mar 2020 12:34:03 +1100 Subject: [PATCH] Issue #4650 - do not use ServiceLoader every time a WSSession is started Signed-off-by: Lachlan Roberts --- .../websocket/common/WebSocketSession.java | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java index 660fe71d3df..6d923db9224 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java @@ -64,6 +64,7 @@ import org.eclipse.jetty.websocket.common.scopes.WebSocketSessionScope; public class WebSocketSession extends ContainerLifeCycle implements Session, RemoteEndpointFactory, WebSocketSessionScope, IncomingFrames, OutgoingFrames, Connection.Listener { private static final Logger LOG = Log.getLogger(WebSocketSession.class); + private static final RemoteEndpointFactory defaultRemoteEndpointFactory; private final WebSocketContainerScope containerScope; private final URI requestURI; private final LogicalConnection connection; @@ -71,9 +72,9 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Rem private final Executor executor; private final WebSocketPolicy policy; private final AtomicBoolean onCloseCalled = new AtomicBoolean(false); + private final RemoteEndpointFactory remoteEndpointFactory; private ClassLoader classLoader; private ExtensionFactory extensionFactory; - private RemoteEndpointFactory remoteEndpointFactory; private String protocolVersion; private Map parameterMap = new HashMap<>(); private RemoteEndpoint remote; @@ -83,6 +84,15 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Rem private UpgradeResponse upgradeResponse; private CompletableFuture openFuture; + static + { + // Attempt to discover a RemoteEndpointFactory with the SerivceLoader. + Iterator iter = ServiceLoader.load(RemoteEndpointFactory.class).iterator(); + defaultRemoteEndpointFactory = iter.hasNext() ? iter.next() : null; + if (LOG.isDebugEnabled()) + LOG.debug("Discovered default RemoteEndpointFactory: {}", defaultRemoteEndpointFactory); + } + public WebSocketSession(WebSocketContainerScope containerScope, URI requestURI, EventDriver websocket, LogicalConnection connection) { Objects.requireNonNull(containerScope, "Container Scope cannot be null"); @@ -98,6 +108,10 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Rem this.incomingHandler = websocket; this.policy = websocket.getPolicy(); + remoteEndpointFactory = (defaultRemoteEndpointFactory == null) ? this : defaultRemoteEndpointFactory; + if (LOG.isDebugEnabled()) + LOG.debug("Using RemoteEndpointFactory: {}", remoteEndpointFactory); + this.connection.setSession(this); addBean(this.connection); @@ -168,16 +182,6 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Rem if (LOG.isDebugEnabled()) LOG.debug("starting - {}", this); - Iterator iter = ServiceLoader.load(RemoteEndpointFactory.class).iterator(); - if (iter.hasNext()) - remoteEndpointFactory = iter.next(); - - if (remoteEndpointFactory == null) - remoteEndpointFactory = this; - - if (LOG.isDebugEnabled()) - LOG.debug("Using RemoteEndpointFactory: {}", remoteEndpointFactory); - super.doStart(); }