Author: markt Date: Mon Jan 26 11:22:36 2015 New Revision: 1654766 URL: http://svn.apache.org/r1654766 Log: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=57490 Make it possible to use Tomcat's WebSocket client within a web application when running under a SecurityManager. Based on a patch by Mikael Sterner.
Modified: tomcat/trunk/java/org/apache/tomcat/websocket/AsyncChannelGroupUtil.java Modified: tomcat/trunk/java/org/apache/tomcat/websocket/AsyncChannelGroupUtil.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/AsyncChannelGroupUtil.java?rev=1654766&r1=1654765&r2=1654766&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/AsyncChannelGroupUtil.java (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/AsyncChannelGroupUtil.java Mon Jan 26 11:22:36 2015 @@ -105,7 +105,13 @@ public class AsyncChannelGroupUtil { private static class AsyncIOThreadFactory implements ThreadFactory { - private AtomicInteger count = new AtomicInteger(0); + static { + // Load NewThreadPrivilegedAction since newThread() will not be able + // to if called from an InnocuousThread. + // See https://issues.apache.org/bugzilla/show_bug.cgi?id=57490 + NewThreadPrivilegedAction.load(); + } + @Override public Thread newThread(final Runnable r) { @@ -113,16 +119,33 @@ public class AsyncChannelGroupUtil { // the thread inherits the current ProtectionDomain which is // essential to be able to use this with a Java Applet. See // https://issues.apache.org/bugzilla/show_bug.cgi?id=57091 - return AccessController.doPrivileged(new PrivilegedAction<Thread>() { - @Override - public Thread run() { - Thread t = new Thread(r); - t.setName("WebSocketClient-AsyncIO-" + count.incrementAndGet()); - t.setContextClassLoader(this.getClass().getClassLoader()); - t.setDaemon(true); - return t; - } - }); + return AccessController.doPrivileged(new NewThreadPrivilegedAction(r)); + } + + // Non-anonymous class so that AsyncIOThreadFactory can load it + // explicitly + private static class NewThreadPrivilegedAction implements PrivilegedAction<Thread> { + + private static AtomicInteger count = new AtomicInteger(0); + + private final Runnable r; + + public NewThreadPrivilegedAction(Runnable r) { + this.r = r; + } + + @Override + public Thread run() { + Thread t = new Thread(r); + t.setName("WebSocketClient-AsyncIO-" + count.incrementAndGet()); + t.setContextClassLoader(this.getClass().getClassLoader()); + t.setDaemon(true); + return t; + } + + private static void load() { + // NO-OP. Just provides a hook to enable the class to be loaded + } } } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org