NETTY-19 Investigate if there's any copyright issue related with MINA

* Replaced NamePreservingRunnable with a new ThreadRenamingRunnable
This commit is contained in:
Trustin Lee 2008-08-19 10:06:07 +00:00
parent 094a4aaa8d
commit 4d17646743
6 changed files with 42 additions and 40 deletions

View File

@ -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();

View File

@ -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() + ')'));

View File

@ -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();

View File

@ -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() + " => " +

View File

@ -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() +

View File

@ -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);
}
}
}
}