Author: remy.maucherat(a)jboss.com
Date: 2013-12-19 08:09:03 -0500 (Thu, 19 Dec 2013)
New Revision: 2331
Modified:
branches/7.4.x/src/main/java/org/apache/tomcat/websocket/server/UpgradeUtil.java
branches/7.4.x/src/main/java/org/apache/tomcat/websocket/server/WsFilter.java
branches/7.4.x/src/main/java/org/apache/tomcat/websocket/server/WsServerContainer.java
Log:
Drop the broken lazy init in favor of a simple flag to indicate if websockets is used.
Modified:
branches/7.4.x/src/main/java/org/apache/tomcat/websocket/server/UpgradeUtil.java
===================================================================
---
branches/7.4.x/src/main/java/org/apache/tomcat/websocket/server/UpgradeUtil.java 2013-12-17
14:08:02 UTC (rev 2330)
+++
branches/7.4.x/src/main/java/org/apache/tomcat/websocket/server/UpgradeUtil.java 2013-12-19
13:09:03 UTC (rev 2331)
@@ -69,7 +69,7 @@
* WebSocket spec 1.0, section 8.2 implies such a limitation and RFC
* 6455 section 4.1 requires that a WebSocket Upgrade uses GET.
*/
- public static boolean isWebSocketUpgrageRequest(ServletRequest request,
+ public static boolean isWebSocketUpgradeRequest(ServletRequest request,
ServletResponse response) {
return ((request instanceof HttpServletRequest) &&
Modified: branches/7.4.x/src/main/java/org/apache/tomcat/websocket/server/WsFilter.java
===================================================================
---
branches/7.4.x/src/main/java/org/apache/tomcat/websocket/server/WsFilter.java 2013-12-17
14:08:02 UTC (rev 2330)
+++
branches/7.4.x/src/main/java/org/apache/tomcat/websocket/server/WsFilter.java 2013-12-19
13:09:03 UTC (rev 2331)
@@ -47,7 +47,8 @@
FilterChain chain) throws IOException, ServletException {
// This filter only needs to handle WebSocket upgrade requests
- if (!UpgradeUtil.isWebSocketUpgrageRequest(request, response)) {
+ if (!sc.areEndpointsRegistered() ||
+ !UpgradeUtil.isWebSocketUpgradeRequest(request, response)) {
chain.doFilter(request, response);
return;
}
Modified:
branches/7.4.x/src/main/java/org/apache/tomcat/websocket/server/WsServerContainer.java
===================================================================
---
branches/7.4.x/src/main/java/org/apache/tomcat/websocket/server/WsServerContainer.java 2013-12-17
14:08:02 UTC (rev 2330)
+++
branches/7.4.x/src/main/java/org/apache/tomcat/websocket/server/WsServerContainer.java 2013-12-19
13:09:03 UTC (rev 2331)
@@ -84,89 +84,74 @@
private volatile boolean addAllowed = true;
private final ConcurrentHashMap<String,Set<WsSession>>
authenticatedSessions =
new ConcurrentHashMap<String, Set<WsSession>>();
- private ExecutorService executorService;
- private volatile boolean initialized = false;
+ private final ExecutorService executorService;
+ private volatile boolean endpointsRegistered = false;
WsServerContainer(ServletContext servletContext) {
this.servletContext = servletContext;
- }
- private void init() {
+ // Configure servlet context wide defaults
+ String value = servletContext.getInitParameter(
+ Constants.BINARY_BUFFER_SIZE_SERVLET_CONTEXT_INIT_PARAM);
+ if (value != null) {
+ setDefaultMaxBinaryMessageBufferSize(Integer.parseInt(value));
+ }
- // Double checked locking. This is safe since Java > 1.5 and initialized
- // is volatile
- if (initialized) {
- return;
+ value = servletContext.getInitParameter(
+ Constants.TEXT_BUFFER_SIZE_SERVLET_CONTEXT_INIT_PARAM);
+ if (value != null) {
+ setDefaultMaxTextMessageBufferSize(Integer.parseInt(value));
}
- synchronized (this) {
- if (initialized) {
- return;
- }
- initialized = true;
- // Configure servlet context wide defaults
- String value = servletContext.getInitParameter(
- Constants.BINARY_BUFFER_SIZE_SERVLET_CONTEXT_INIT_PARAM);
- if (value != null) {
- setDefaultMaxBinaryMessageBufferSize(Integer.parseInt(value));
- }
+ value = servletContext.getInitParameter(
+ Constants.ENFORCE_NO_ADD_AFTER_HANDSHAKE_CONTEXT_INIT_PARAM);
+ if (value != null) {
+ setEnforceNoAddAfterHandshake(Boolean.parseBoolean(value));
+ }
+ // Executor config
+ int executorCoreSize = 0;
+ int executorMaxSize = 10;
+ long executorKeepAliveTimeSeconds = 60;
+ value = servletContext.getInitParameter(
+ Constants.EXECUTOR_CORE_SIZE_INIT_PARAM);
+ if (value != null) {
+ executorCoreSize = Integer.parseInt(value);
+ }
+ value = servletContext.getInitParameter(
+ Constants.EXECUTOR_MAX_SIZE_INIT_PARAM);
+ if (value != null) {
+ executorMaxSize = Integer.parseInt(value);
+ }
+ value = servletContext.getInitParameter(
+ Constants.EXECUTOR_KEEPALIVETIME_SECONDS_INIT_PARAM);
+ if (value != null) {
+ executorKeepAliveTimeSeconds = Long.parseLong(value);
+ }
- value = servletContext.getInitParameter(
- Constants.TEXT_BUFFER_SIZE_SERVLET_CONTEXT_INIT_PARAM);
- if (value != null) {
- setDefaultMaxTextMessageBufferSize(Integer.parseInt(value));
- }
+ FilterRegistration.Dynamic fr = servletContext.addFilter(
+ "Tomcat WebSocket (JSR356) Filter", new WsFilter());
+ fr.setAsyncSupported(true);
- value = servletContext.getInitParameter(
- Constants.ENFORCE_NO_ADD_AFTER_HANDSHAKE_CONTEXT_INIT_PARAM);
- if (value != null) {
- setEnforceNoAddAfterHandshake(Boolean.parseBoolean(value));
- }
- // Executor config
- int executorCoreSize = 0;
- int executorMaxSize = 10;
- long executorKeepAliveTimeSeconds = 60;
- value = servletContext.getInitParameter(
- Constants.EXECUTOR_CORE_SIZE_INIT_PARAM);
- if (value != null) {
- executorCoreSize = Integer.parseInt(value);
- }
- value = servletContext.getInitParameter(
- Constants.EXECUTOR_MAX_SIZE_INIT_PARAM);
- if (value != null) {
- executorMaxSize = Integer.parseInt(value);
- }
- value = servletContext.getInitParameter(
- Constants.EXECUTOR_KEEPALIVETIME_SECONDS_INIT_PARAM);
- if (value != null) {
- executorKeepAliveTimeSeconds = Long.parseLong(value);
- }
+ EnumSet<DispatcherType> types = EnumSet.of(DispatcherType.REQUEST,
+ DispatcherType.FORWARD);
- FilterRegistration.Dynamic fr = servletContext.addFilter(
- "Tomcat WebSocket (JSR356) Filter", new WsFilter());
- fr.setAsyncSupported(true);
+ fr.addMappingForUrlPatterns(types, true, "/*");
- EnumSet<DispatcherType> types = EnumSet.of(DispatcherType.REQUEST,
- DispatcherType.FORWARD);
-
- fr.addMappingForUrlPatterns(types, true, "/*");
-
- // Use a per web application executor for any threads the the WebSocket
- // server code needs to create. Group all of the threads under a single
- // ThreadGroup.
- StringBuffer threadGroupName = new
StringBuffer("WebSocketServer-");
- if ("".equals(servletContext.getContextPath())) {
- threadGroupName.append("ROOT");
- } else {
- threadGroupName.append(servletContext.getContextPath());
- }
- ThreadGroup threadGroup = new ThreadGroup(threadGroupName.toString());
- WsThreadFactory wsThreadFactory = new WsThreadFactory(threadGroup);
-
- executorService = new ThreadPoolExecutor(executorCoreSize,
- executorMaxSize, executorKeepAliveTimeSeconds, TimeUnit.SECONDS,
- new LinkedBlockingQueue<Runnable>(), wsThreadFactory);
+ // Use a per web application executor for any threads the the WebSocket
+ // server code needs to create. Group all of the threads under a single
+ // ThreadGroup.
+ StringBuffer threadGroupName = new StringBuffer("WebSocketServer-");
+ if ("".equals(servletContext.getContextPath())) {
+ threadGroupName.append("ROOT");
+ } else {
+ threadGroupName.append(servletContext.getContextPath());
}
+ ThreadGroup threadGroup = new ThreadGroup(threadGroupName.toString());
+ WsThreadFactory wsThreadFactory = new WsThreadFactory(threadGroup);
+
+ executorService = new ThreadPoolExecutor(executorCoreSize,
+ executorMaxSize, executorKeepAliveTimeSeconds, TimeUnit.SECONDS,
+ new LinkedBlockingQueue<Runnable>(), wsThreadFactory);
}
@@ -182,8 +167,6 @@
public void addEndpoint(ServerEndpointConfig sec)
throws DeploymentException {
- init();
-
if (enforceNoAddAfterHandshake && !addAllowed) {
throw new DeploymentException(MESSAGES.addNotAllowed());
}
@@ -218,6 +201,8 @@
throw new DeploymentException(MESSAGES.duplicatePaths(path));
}
}
+
+ endpointsRegistered = true;
}
@@ -231,8 +216,6 @@
@Override
public void addEndpoint(Class<?> pojo) throws DeploymentException {
- init();
-
ServerEndpoint annotation = pojo.getAnnotation(ServerEndpoint.class);
if (annotation == null) {
throw new DeploymentException(MESSAGES.cannotDeployPojo(pojo.getName()));
@@ -276,6 +259,11 @@
}
+ boolean areEndpointsRegistered() {
+ return endpointsRegistered;
+ }
+
+
public void doUpgrade(HttpServletRequest request,
HttpServletResponse response, ServerEndpointConfig sec,
Map<String,String> pathParams)