/
JettyWebSocketServletContainerInitializer.java
109 lines (97 loc) · 4.38 KB
/
JettyWebSocketServletContainerInitializer.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// ========================================================================
// Copyright (c) 1995-2020 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.server.config;
import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.websocket.core.WebSocketComponents;
import org.eclipse.jetty.websocket.core.server.WebSocketMappings;
import org.eclipse.jetty.websocket.core.server.WebSocketServerComponents;
import org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* ServletContext configuration for Jetty Native WebSockets API.
*/
public class JettyWebSocketServletContainerInitializer implements ServletContainerInitializer
{
private static final Logger LOG = LoggerFactory.getLogger(JettyWebSocketServletContainerInitializer.class);
private final Configurator configurator;
public JettyWebSocketServletContainerInitializer()
{
this(null);
}
public JettyWebSocketServletContainerInitializer(Configurator configurator)
{
this.configurator = configurator;
}
public interface Configurator
{
void accept(ServletContext servletContext, JettyWebSocketServerContainer container);
}
/**
* Configure the {@link ServletContextHandler} to call the {@link JettyWebSocketServletContainerInitializer}
* during the {@link ServletContext} initialization phase.
*
* @param context the context to add listener to.
* @param configurator a lambda that is called to allow the {@link WebSocketMappings} to
* be configured during {@link ServletContext} initialization phase
*/
public static void configure(ServletContextHandler context, Configurator configurator)
{
if (!context.isStopped())
throw new IllegalStateException("configure should be called before starting");
context.addServletContainerInitializer(new JettyWebSocketServletContainerInitializer(configurator));
}
/**
* Immediately initialize the {@link ServletContextHandler} with the default {@link JettyWebSocketServerContainer}.
*
* <p>
* This method is typically called from {@link #onStartup(Set, ServletContext)} itself or from
* another dependent {@link ServletContainerInitializer} that requires minimal setup to
* be performed.
* </p>
* <p>
* This method SHOULD NOT BE CALLED by users of Jetty.
* Use the {@link #configure(ServletContextHandler, Configurator)} method instead.
* </p>
* <p>
* This will return the default {@link JettyWebSocketServerContainer} if already initialized,
* and not create a new {@link JettyWebSocketServerContainer} each time it is called.
* </p>
*
* @param context the context to work with
* @return the default {@link JettyWebSocketServerContainer}
*/
private static JettyWebSocketServerContainer initialize(ServletContextHandler context)
{
WebSocketComponents components = WebSocketServerComponents.ensureWebSocketComponents(context.getServer(), context.getServletContext());
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.ensureContainer(context.getServletContext());
if (LOG.isDebugEnabled())
LOG.debug("initialize {} {}", container, components);
return container;
}
@Override
public void onStartup(Set<Class<?>> c, ServletContext context)
{
ServletContextHandler contextHandler = ServletContextHandler.getServletContextHandler(context, "Jetty WebSocket SCI");
JettyWebSocketServerContainer container = JettyWebSocketServletContainerInitializer.initialize(contextHandler);
if (LOG.isDebugEnabled())
LOG.debug("onStartup {}", container);
if (configurator != null)
{
configurator.accept(context, container);
}
}
}