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.channel.MessageEvent;
import org.jboss.netty.logging.InternalLogger; import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory; import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.util.NamePreservingRunnable; import org.jboss.netty.util.ThreadRenamingRunnable;
class NioClientSocketPipelineSink extends AbstractChannelSink { class NioClientSocketPipelineSink extends AbstractChannelSink {
@ -193,9 +193,8 @@ class NioClientSocketPipelineSink extends AbstractChannelSink {
throw new ChannelException( throw new ChannelException(
"Failed to register a socket to the selector.", e); "Failed to register a socket to the selector.", e);
} }
bossExecutor.execute(new NamePreservingRunnable( bossExecutor.execute(new ThreadRenamingRunnable(
this, this, "New I/O client boss #" + id));
"New I/O client boss #" + id));
} else { } else {
synchronized (selectorGuard) { synchronized (selectorGuard) {
selector.wakeup(); selector.wakeup();

View File

@ -42,7 +42,7 @@ import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.logging.InternalLogger; import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory; import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.util.NamePreservingRunnable; import org.jboss.netty.util.ThreadRenamingRunnable;
class NioServerSocketPipelineSink extends AbstractChannelSink { class NioServerSocketPipelineSink extends AbstractChannelSink {
@ -146,7 +146,7 @@ class NioServerSocketPipelineSink extends AbstractChannelSink {
Executor bossExecutor = Executor bossExecutor =
((NioServerSocketChannelFactory) channel.getFactory()).bossExecutor; ((NioServerSocketChannelFactory) channel.getFactory()).bossExecutor;
bossExecutor.execute(new NamePreservingRunnable( bossExecutor.execute(new ThreadRenamingRunnable(
new Boss(channel), new Boss(channel),
"New I/O server boss #" + id +" (channelId: " + channel.getId() + "New I/O server boss #" + id +" (channelId: " + channel.getId() +
", " + channel.getLocalAddress() + ')')); ", " + channel.getLocalAddress() + ')'));

View File

@ -42,7 +42,7 @@ import org.jboss.netty.channel.ChannelException;
import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.logging.InternalLogger; import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory; import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.util.NamePreservingRunnable; import org.jboss.netty.util.ThreadRenamingRunnable;
class NioWorker implements Runnable { class NioWorker implements Runnable {
@ -106,7 +106,7 @@ class NioWorker implements Runnable {
(server ? "New I/O server worker #" (server ? "New I/O server worker #"
: "New I/O client worker #") + bossId + '-' + id; : "New I/O client worker #") + bossId + '-' + id;
executor.execute(new NamePreservingRunnable(this, threadName)); executor.execute(new ThreadRenamingRunnable(this, threadName));
} else { } else {
synchronized (selectorGuard) { synchronized (selectorGuard) {
selector.wakeup(); 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.ChannelState;
import org.jboss.netty.channel.ChannelStateEvent; import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.util.NamePreservingRunnable; import org.jboss.netty.util.ThreadRenamingRunnable;
class OioClientSocketPipelineSink extends AbstractChannelSink { class OioClientSocketPipelineSink extends AbstractChannelSink {
@ -131,7 +131,7 @@ class OioClientSocketPipelineSink extends AbstractChannelSink {
fireChannelConnected(channel, channel.getRemoteAddress()); fireChannelConnected(channel, channel.getRemoteAddress());
// Start the business. // Start the business.
workerExecutor.execute(new NamePreservingRunnable( workerExecutor.execute(new ThreadRenamingRunnable(
new OioWorker(channel), new OioWorker(channel),
"Old I/O client worker (channelId: " + channel.getId() + ", " + "Old I/O client worker (channelId: " + channel.getId() + ", " +
channel.getLocalAddress() + " => " + channel.getLocalAddress() + " => " +

View File

@ -40,7 +40,7 @@ import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.logging.InternalLogger; import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory; import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.util.NamePreservingRunnable; import org.jboss.netty.util.ThreadRenamingRunnable;
class OioServerSocketPipelineSink extends AbstractChannelSink { class OioServerSocketPipelineSink extends AbstractChannelSink {
@ -141,7 +141,7 @@ class OioServerSocketPipelineSink extends AbstractChannelSink {
Executor bossExecutor = Executor bossExecutor =
((OioServerSocketChannelFactory) channel.getFactory()).bossExecutor; ((OioServerSocketChannelFactory) channel.getFactory()).bossExecutor;
bossExecutor.execute(new NamePreservingRunnable( bossExecutor.execute(new ThreadRenamingRunnable(
new Boss(channel), new Boss(channel),
"Old I/O server boss (channelId: " + channel.getId() + "Old I/O server boss (channelId: " + channel.getId() +
", " + localAddress + ')')); ", " + localAddress + ')'));
@ -195,7 +195,7 @@ class OioServerSocketPipelineSink extends AbstractChannelSink {
OioServerSocketPipelineSink.this, OioServerSocketPipelineSink.this,
acceptedSocket); acceptedSocket);
workerExecutor.execute( workerExecutor.execute(
new NamePreservingRunnable( new ThreadRenamingRunnable(
new OioWorker(acceptedChannel), new OioWorker(acceptedChannel),
"Old I/O server worker (parentId: " + "Old I/O server worker (parentId: " +
channel.getId() + channel.getId() +

View File

@ -26,8 +26,8 @@ import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory; import org.jboss.netty.logging.InternalLoggerFactory;
/** /**
* Meta {@link Runnable} that changes the current thread name and reverts it back * Meta {@link Runnable} that changes the current thread name and reverts it
* when its execution ends. * back when its execution ends.
* *
* @author The Netty Project (netty-dev@lists.jboss.org) * @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com) * @author Trustin Lee (tlee@redhat.com)
@ -35,11 +35,11 @@ import org.jboss.netty.logging.InternalLoggerFactory;
* @version $Rev$, $Date$ * @version $Rev$, $Date$
* *
*/ */
public class NamePreservingRunnable implements Runnable { public class ThreadRenamingRunnable implements Runnable {
private static final InternalLogger logger = 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; 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 * and changes the thread name to the specified thread name when the
* specified {@code runnable} is running. * 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.runnable = runnable;
this.newName = newName;
} }
public void run() { public void run() {
Thread currentThread = Thread.currentThread(); final Thread currentThread = Thread.currentThread();
String oldName = currentThread.getName(); final String oldThreadName = currentThread.getName();
if (newName != null) {
setName(currentThread, newName);
}
// Change the thread name before starting the actual runnable.
boolean renamed = false;
try { try {
runnable.run(); currentThread.setName(threadName);
} finally { renamed = true;
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);
} catch (Exception e) { } catch (Exception e) {
// Probably SecurityException. // Probably SecurityException.
logger.warn( logger.warn(
"Failed to set the current thread name.", e); "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);
}
}
} }
} }