From 4d17646743238dc456f99a2bd697b2a4c814327b Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Tue, 19 Aug 2008 10:06:07 +0000 Subject: [PATCH] NETTY-19 Investigate if there's any copyright issue related with MINA * Replaced NamePreservingRunnable with a new ThreadRenamingRunnable --- .../nio/NioClientSocketPipelineSink.java | 7 +-- .../nio/NioServerSocketPipelineSink.java | 4 +- .../netty/channel/socket/nio/NioWorker.java | 4 +- .../oio/OioClientSocketPipelineSink.java | 4 +- .../oio/OioServerSocketPipelineSink.java | 6 +- ...nable.java => ThreadRenamingRunnable.java} | 57 ++++++++++--------- 6 files changed, 42 insertions(+), 40 deletions(-) rename src/main/java/org/jboss/netty/util/{NamePreservingRunnable.java => ThreadRenamingRunnable.java} (63%) diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioClientSocketPipelineSink.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioClientSocketPipelineSink.java index 8cbf62ca91..6c1ae957e6 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioClientSocketPipelineSink.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioClientSocketPipelineSink.java @@ -46,7 +46,7 @@ import org.jboss.netty.channel.ChannelStateEvent; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.logging.InternalLogger; import org.jboss.netty.logging.InternalLoggerFactory; -import org.jboss.netty.util.NamePreservingRunnable; +import org.jboss.netty.util.ThreadRenamingRunnable; class NioClientSocketPipelineSink extends AbstractChannelSink { @@ -193,9 +193,8 @@ class NioClientSocketPipelineSink extends AbstractChannelSink { throw new ChannelException( "Failed to register a socket to the selector.", e); } - bossExecutor.execute(new NamePreservingRunnable( - this, - "New I/O client boss #" + id)); + bossExecutor.execute(new ThreadRenamingRunnable( + this, "New I/O client boss #" + id)); } else { synchronized (selectorGuard) { selector.wakeup(); diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioServerSocketPipelineSink.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioServerSocketPipelineSink.java index 5c90168e36..191d13b445 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioServerSocketPipelineSink.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioServerSocketPipelineSink.java @@ -42,7 +42,7 @@ import org.jboss.netty.channel.ChannelStateEvent; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.logging.InternalLogger; import org.jboss.netty.logging.InternalLoggerFactory; -import org.jboss.netty.util.NamePreservingRunnable; +import org.jboss.netty.util.ThreadRenamingRunnable; class NioServerSocketPipelineSink extends AbstractChannelSink { @@ -146,7 +146,7 @@ class NioServerSocketPipelineSink extends AbstractChannelSink { Executor bossExecutor = ((NioServerSocketChannelFactory) channel.getFactory()).bossExecutor; - bossExecutor.execute(new NamePreservingRunnable( + bossExecutor.execute(new ThreadRenamingRunnable( new Boss(channel), "New I/O server boss #" + id +" (channelId: " + channel.getId() + ", " + channel.getLocalAddress() + ')')); diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java index ff42a32bdc..c8b9485e09 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java @@ -42,7 +42,7 @@ import org.jboss.netty.channel.ChannelException; import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.logging.InternalLogger; import org.jboss.netty.logging.InternalLoggerFactory; -import org.jboss.netty.util.NamePreservingRunnable; +import org.jboss.netty.util.ThreadRenamingRunnable; class NioWorker implements Runnable { @@ -106,7 +106,7 @@ class NioWorker implements Runnable { (server ? "New I/O server worker #" : "New I/O client worker #") + bossId + '-' + id; - executor.execute(new NamePreservingRunnable(this, threadName)); + executor.execute(new ThreadRenamingRunnable(this, threadName)); } else { synchronized (selectorGuard) { selector.wakeup(); diff --git a/src/main/java/org/jboss/netty/channel/socket/oio/OioClientSocketPipelineSink.java b/src/main/java/org/jboss/netty/channel/socket/oio/OioClientSocketPipelineSink.java index 43ca159b73..3ab4e478ee 100644 --- a/src/main/java/org/jboss/netty/channel/socket/oio/OioClientSocketPipelineSink.java +++ b/src/main/java/org/jboss/netty/channel/socket/oio/OioClientSocketPipelineSink.java @@ -36,7 +36,7 @@ import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.ChannelState; import org.jboss.netty.channel.ChannelStateEvent; import org.jboss.netty.channel.MessageEvent; -import org.jboss.netty.util.NamePreservingRunnable; +import org.jboss.netty.util.ThreadRenamingRunnable; class OioClientSocketPipelineSink extends AbstractChannelSink { @@ -131,7 +131,7 @@ class OioClientSocketPipelineSink extends AbstractChannelSink { fireChannelConnected(channel, channel.getRemoteAddress()); // Start the business. - workerExecutor.execute(new NamePreservingRunnable( + workerExecutor.execute(new ThreadRenamingRunnable( new OioWorker(channel), "Old I/O client worker (channelId: " + channel.getId() + ", " + channel.getLocalAddress() + " => " + diff --git a/src/main/java/org/jboss/netty/channel/socket/oio/OioServerSocketPipelineSink.java b/src/main/java/org/jboss/netty/channel/socket/oio/OioServerSocketPipelineSink.java index 1b4167a8c8..69a77edc21 100644 --- a/src/main/java/org/jboss/netty/channel/socket/oio/OioServerSocketPipelineSink.java +++ b/src/main/java/org/jboss/netty/channel/socket/oio/OioServerSocketPipelineSink.java @@ -40,7 +40,7 @@ import org.jboss.netty.channel.ChannelStateEvent; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.logging.InternalLogger; import org.jboss.netty.logging.InternalLoggerFactory; -import org.jboss.netty.util.NamePreservingRunnable; +import org.jboss.netty.util.ThreadRenamingRunnable; class OioServerSocketPipelineSink extends AbstractChannelSink { @@ -141,7 +141,7 @@ class OioServerSocketPipelineSink extends AbstractChannelSink { Executor bossExecutor = ((OioServerSocketChannelFactory) channel.getFactory()).bossExecutor; - bossExecutor.execute(new NamePreservingRunnable( + bossExecutor.execute(new ThreadRenamingRunnable( new Boss(channel), "Old I/O server boss (channelId: " + channel.getId() + ", " + localAddress + ')')); @@ -195,7 +195,7 @@ class OioServerSocketPipelineSink extends AbstractChannelSink { OioServerSocketPipelineSink.this, acceptedSocket); workerExecutor.execute( - new NamePreservingRunnable( + new ThreadRenamingRunnable( new OioWorker(acceptedChannel), "Old I/O server worker (parentId: " + channel.getId() + diff --git a/src/main/java/org/jboss/netty/util/NamePreservingRunnable.java b/src/main/java/org/jboss/netty/util/ThreadRenamingRunnable.java similarity index 63% rename from src/main/java/org/jboss/netty/util/NamePreservingRunnable.java rename to src/main/java/org/jboss/netty/util/ThreadRenamingRunnable.java index e094b1b5e4..041aa03018 100644 --- a/src/main/java/org/jboss/netty/util/NamePreservingRunnable.java +++ b/src/main/java/org/jboss/netty/util/ThreadRenamingRunnable.java @@ -26,8 +26,8 @@ import org.jboss.netty.logging.InternalLogger; import org.jboss.netty.logging.InternalLoggerFactory; /** - * Meta {@link Runnable} that changes the current thread name and reverts it back - * when its execution ends. + * Meta {@link Runnable} that changes the current thread name and reverts it + * back when its execution ends. * * @author The Netty Project (netty-dev@lists.jboss.org) * @author Trustin Lee (tlee@redhat.com) @@ -35,11 +35,11 @@ import org.jboss.netty.logging.InternalLoggerFactory; * @version $Rev$, $Date$ * */ -public class NamePreservingRunnable implements Runnable { +public class ThreadRenamingRunnable implements Runnable { private static final InternalLogger logger = - InternalLoggerFactory.getInstance(NamePreservingRunnable.class); + InternalLoggerFactory.getInstance(ThreadRenamingRunnable.class); - private final String newName; + private final String threadName; private final Runnable runnable; /** @@ -47,38 +47,41 @@ public class NamePreservingRunnable implements Runnable { * and changes the thread name to the specified thread name when the * specified {@code runnable} is running. */ - public NamePreservingRunnable(Runnable runnable, String newName) { + public ThreadRenamingRunnable(Runnable runnable, String threadName) { + if (threadName == null) { + throw new NullPointerException("threadName"); + } + if (runnable == null) { + throw new NullPointerException("runnable"); + } + this.threadName = threadName; this.runnable = runnable; - this.newName = newName; } public void run() { - Thread currentThread = Thread.currentThread(); - String oldName = currentThread.getName(); - - if (newName != null) { - setName(currentThread, newName); - } + final Thread currentThread = Thread.currentThread(); + final String oldThreadName = currentThread.getName(); + // Change the thread name before starting the actual runnable. + boolean renamed = false; try { - runnable.run(); - } finally { - setName(currentThread, oldName); - } - } - - /** - * Wraps {@link Thread#setName(String)} to catch a possible - * {@link Exception} such as a {@link SecurityException} in a sandbox - * environment, such as an applet - */ - private void setName(Thread thread, String name) { - try { - thread.setName(name); + currentThread.setName(threadName); + renamed = true; } catch (Exception e) { // Probably SecurityException. logger.warn( "Failed to set the current thread name.", e); } + + // Run the actual runnable and revert the name back when it ends. + try { + runnable.run(); + } finally { + if (renamed) { + // Revert the name back if the current thread was renamed. + // We don't check the exception here because we know it works. + currentThread.setName(oldThreadName); + } + } } }