Author: markt Date: Fri Apr 19 19:25:49 2013 New Revision: 1469997 URL: http://svn.apache.org/r1469997 Log: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=54807 Forgot to create TreeSet with correct Comparator. Add a test case that checks the context starts correctly.
Also, TemplatePathMatchComparator is thread-safe so use a single instance rather than creating multiple instances. Added: tomcat/trunk/test/org/apache/tomcat/websocket/server/TestWebSocketServerContainer.java (with props) Modified: tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServerContainer.java Modified: tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServerContainer.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServerContainer.java?rev=1469997&r1=1469996&r2=1469997&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServerContainer.java (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServerContainer.java Fri Apr 19 19:25:49 2013 @@ -138,7 +138,8 @@ public class WsServerContainer extends W SortedSet<TemplatePathMatch> templateMatches = configTemplateMatchMap.get(key); if (templateMatches == null) { - templateMatches = new TreeSet<>(); + templateMatches = new TreeSet<>( + TemplatePathMatchComparator.getInstance()); configTemplateMatchMap.put(key, templateMatches); } templateMatches.add(new TemplatePathMatch(sec, uriTemplate)); @@ -204,8 +205,8 @@ public class WsServerContainer extends W SortedSet<TemplatePathMatch> templateMatches = configTemplateMatchMap.get(key); if (templateMatches == null) { - templateMatches = - new TreeSet<>(new TemplatePathMatchComparator()); + templateMatches = new TreeSet<>( + TemplatePathMatchComparator.getInstance()); configTemplateMatchMap.put(key, templateMatches); } templateMatches.add(new TemplatePathMatch(sec, uriTemplate)); @@ -308,9 +309,24 @@ public class WsServerContainer extends W } + /** + * This Comparator implementation is thread-safe so only create a single + * instance. + */ private static class TemplatePathMatchComparator implements Comparator<TemplatePathMatch> { + private static final TemplatePathMatchComparator INSTANCE = + new TemplatePathMatchComparator(); + + public static TemplatePathMatchComparator getInstance() { + return INSTANCE; + } + + private TemplatePathMatchComparator() { + // Hide default constructor + } + @Override public int compare(TemplatePathMatch tpm1, TemplatePathMatch tpm2) { return tpm1.getUriTemplate().getNormalizedPath().compareTo( Added: tomcat/trunk/test/org/apache/tomcat/websocket/server/TestWebSocketServerContainer.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/websocket/server/TestWebSocketServerContainer.java?rev=1469997&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/tomcat/websocket/server/TestWebSocketServerContainer.java (added) +++ tomcat/trunk/test/org/apache/tomcat/websocket/server/TestWebSocketServerContainer.java Fri Apr 19 19:25:49 2013 @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tomcat.websocket.server; + +import javax.servlet.ServletContextEvent; +import javax.websocket.DeploymentException; +import javax.websocket.server.ServerEndpointConfig; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.catalina.Context; +import org.apache.catalina.LifecycleState; +import org.apache.catalina.servlets.DefaultServlet; +import org.apache.catalina.startup.Tomcat; +import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.tomcat.websocket.TesterEchoServer; + + +public class TestWebSocketServerContainer extends TomcatBaseTest { + + @Test + public void testBug54807() throws Exception { + Tomcat tomcat = getTomcatInstance(); + // Must have a real docBase - just use temp + Context ctx = + tomcat.addContext("", System.getProperty("java.io.tmpdir")); + ctx.addApplicationListener(Bug54807Config.class.getName()); + Tomcat.addServlet(ctx, "default", new DefaultServlet()); + ctx.addServletMapping("/", "default"); + + tomcat.start(); + + Assert.assertEquals(LifecycleState.STARTED, ctx.getState()); + } + + + public static class Bug54807Config extends WsListener { + + @Override + public void contextInitialized(ServletContextEvent sce) { + super.contextInitialized(sce); + + WsServerContainer sc = WsServerContainer.getServerContainer(); + + ServerEndpointConfig sec = ServerEndpointConfig.Builder.create( + TesterEchoServer.Basic.class, "/{param}").build(); + + try { + sc.addEndpoint(sec); + } catch (DeploymentException e) { + throw new RuntimeException(e); + } + } + } +} Propchange: tomcat/trunk/test/org/apache/tomcat/websocket/server/TestWebSocketServerContainer.java ------------------------------------------------------------------------------ svn:eol-style = native --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org