mina-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r588150 - in /mina: branches/1.0/core/src/main/java/org/apache/mina/common/ branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/ branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/support/ branches/1.0/cor...
Date Thu, 25 Oct 2007 06:20:06 GMT
Author: trustin
Date: Wed Oct 24 23:20:04 2007
New Revision: 588150

URL: http://svn.apache.org/viewvc?rev=588150&view=rev
Log:
Resolved issue: DIRMINA-462 (Thread.setName causes SecurityException in sandbox contexts (such
as Applets))
* Moved all Thread.setName call to NamePerservingRunnable


Modified:
    mina/branches/1.0/core/src/main/java/org/apache/mina/common/ExecutorThreadModel.java
    mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java
    mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java
    mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketIoProcessor.java
    mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java
    mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramConnectorDelegate.java
    mina/branches/1.0/core/src/main/java/org/apache/mina/util/NamePreservingRunnable.java
    mina/branches/1.1/core/src/main/java/org/apache/mina/common/ExecutorThreadModel.java
    mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java
    mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java
    mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketIoProcessor.java
    mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java
    mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramConnectorDelegate.java
    mina/branches/1.1/core/src/main/java/org/apache/mina/util/NamePreservingRunnable.java
    mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoProcessor.java
    mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioDatagramAcceptor.java
    mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketAcceptor.java
    mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketConnector.java
    mina/trunk/core/src/main/java/org/apache/mina/util/NamePreservingRunnable.java

Modified: mina/branches/1.0/core/src/main/java/org/apache/mina/common/ExecutorThreadModel.java
URL: http://svn.apache.org/viewvc/mina/branches/1.0/core/src/main/java/org/apache/mina/common/ExecutorThreadModel.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/branches/1.0/core/src/main/java/org/apache/mina/common/ExecutorThreadModel.java (original)
+++ mina/branches/1.0/core/src/main/java/org/apache/mina/common/ExecutorThreadModel.java Wed
Oct 24 23:20:04 2007
@@ -23,6 +23,7 @@
 import java.util.Map;
 
 import org.apache.mina.filter.executor.ExecutorFilter;
+import org.apache.mina.util.NamePreservingRunnable;
 
 import edu.emory.mathcs.backport.java.util.concurrent.Executor;
 import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory;
@@ -89,9 +90,11 @@
             private final AtomicInteger threadId = new AtomicInteger(0);
 
             public Thread newThread(Runnable runnable) {
-                Thread t = originalThreadFactory.newThread(runnable);
-                t.setName(ExecutorThreadModel.this.threadNamePrefix + '-'
-                        + threadId.incrementAndGet());
+                Thread t = originalThreadFactory.newThread(
+                        new NamePreservingRunnable(
+                                runnable, 
+                                ExecutorThreadModel.this.threadNamePrefix + '-' +
+                                threadId.incrementAndGet()));
                 t.setDaemon(true);
                 return t;
             }

Modified: mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java
URL: http://svn.apache.org/viewvc/mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java
(original)
+++ mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java
Wed Oct 24 23:20:04 2007
@@ -179,7 +179,7 @@
                 selector = Selector.open();
                 worker = new Worker();
 
-                executor.execute(new NamePreservingRunnable(worker));
+                executor.execute(new NamePreservingRunnable(worker, threadName));
             }
         }
     }
@@ -237,8 +237,6 @@
 
     private class Worker implements Runnable {
         public void run() {
-            Thread.currentThread().setName(SocketAcceptor.this.threadName);
-
             Selector selector = getSelector();
             for (;;) {
                 try {

Modified: mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java
URL: http://svn.apache.org/viewvc/mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java
(original)
+++ mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java
Wed Oct 24 23:20:04 2007
@@ -245,7 +245,7 @@
             if (worker == null) {
                 selector = Selector.open();
                 worker = new Worker();
-                executor.execute(new NamePreservingRunnable(worker));
+                executor.execute(new NamePreservingRunnable(worker, threadName));
             }
         }
     }
@@ -374,8 +374,6 @@
         private long lastActive = System.currentTimeMillis();
 
         public void run() {
-            Thread.currentThread().setName(SocketConnector.this.threadName);
-
             Selector selector = getSelector();
             for (;;) {
                 try {

Modified: mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketIoProcessor.java
URL: http://svn.apache.org/viewvc/mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketIoProcessor.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketIoProcessor.java
(original)
+++ mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketIoProcessor.java
Wed Oct 24 23:20:04 2007
@@ -94,7 +94,7 @@
             if (worker == null) {
                 selector = Selector.open();
                 worker = new Worker();
-                executor.execute(new NamePreservingRunnable(worker));
+                executor.execute(new NamePreservingRunnable(worker, threadName));
             }
             selector.wakeup();
         }
@@ -506,8 +506,6 @@
 
     private class Worker implements Runnable {
         public void run() {
-            Thread.currentThread().setName(SocketIoProcessor.this.threadName);
-
             Selector selector = getSelector();
             for (;;) {
                 try {

Modified: mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java
URL: http://svn.apache.org/viewvc/mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java
(original)
+++ mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java
Wed Oct 24 23:20:04 2007
@@ -282,7 +282,8 @@
         if (worker == null) {
             selector = Selector.open();
             worker = new Worker();
-            executor.execute(new NamePreservingRunnable(worker));
+            executor.execute(
+                    new NamePreservingRunnable(worker, "DatagramAcceptor-" + id));
         }
     }
 
@@ -311,8 +312,6 @@
 
     private class Worker implements Runnable {
         public void run() {
-            Thread.currentThread().setName("DatagramAcceptor-" + id);
-
             Selector selector = DatagramAcceptorDelegate.this.getSelector();
             for (;;) {
                 try {

Modified: mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramConnectorDelegate.java
URL: http://svn.apache.org/viewvc/mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramConnectorDelegate.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramConnectorDelegate.java
(original)
+++ mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramConnectorDelegate.java
Wed Oct 24 23:20:04 2007
@@ -203,7 +203,8 @@
         if (worker == null) {
             selector = Selector.open();
             worker = new Worker();
-            executor.execute(new NamePreservingRunnable(worker));
+            executor.execute(
+                    new NamePreservingRunnable(worker, "DatagramConnector-" + id));
         }
     }
 
@@ -308,8 +309,6 @@
 
     private class Worker implements Runnable {
         public void run() {
-            Thread.currentThread().setName("DatagramConnector-" + id);
-
             Selector selector = getSelector();
             for (;;) {
                 try {

Modified: mina/branches/1.0/core/src/main/java/org/apache/mina/util/NamePreservingRunnable.java
URL: http://svn.apache.org/viewvc/mina/branches/1.0/core/src/main/java/org/apache/mina/util/NamePreservingRunnable.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/branches/1.0/core/src/main/java/org/apache/mina/util/NamePreservingRunnable.java
(original)
+++ mina/branches/1.0/core/src/main/java/org/apache/mina/util/NamePreservingRunnable.java
Wed Oct 24 23:20:04 2007
@@ -18,27 +18,54 @@
  */
 package org.apache.mina.util;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
- * A Runnable wrapper that preserves the name of the thread after the runnable is complete
(for Runnables
- * that change the name of the Thread they use)
+ * A {@link Runnable} wrapper that preserves the name of the thread after the runnable is
+ * complete (for {@link Runnable}s that change the name of the Thread they use.)
  * 
- * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @author The Apache MINA Project (dev@mina.apache.org)
  * @version $Rev: 446581 $, $Date: 2006-09-15 11:36:12Z $,
  */
 public class NamePreservingRunnable implements Runnable {
+    private final Logger logger = LoggerFactory.getLogger(NamePreservingRunnable.class);
+
+    private final String newName;
     private final Runnable runnable;
 
-    public NamePreservingRunnable(Runnable runnable) {
+    public NamePreservingRunnable(Runnable runnable, String newName) {
         this.runnable = runnable;
+        this.newName = newName;
     }
 
     public void run() {
-        String name = Thread.currentThread().getName();
+        Thread currentThread = Thread.currentThread();
+        String oldName = currentThread.getName();
+        
+        if (newName != null) {
+            setName(currentThread, newName);
+        }
 
         try {
             runnable.run();
         } finally {
-            Thread.currentThread().setName(name);
+            setName(currentThread, oldName);
+        }
+    }
+    
+    /**
+     * Wraps {@link Thread#setName(String)} to catch a possible {@link Exception}s such as
+     * {@link SecurityException} in sandbox environments, such as applets
+     */
+    private void setName(Thread thread, String name) {
+        try {
+            thread.setName(name);
+        } catch (Exception e) {
+            // Probably SecurityException.
+            if (logger.isWarnEnabled()) {
+                logger.warn("Failed to set the thread name.", e);
+            }
         }
     }
 }

Modified: mina/branches/1.1/core/src/main/java/org/apache/mina/common/ExecutorThreadModel.java
URL: http://svn.apache.org/viewvc/mina/branches/1.1/core/src/main/java/org/apache/mina/common/ExecutorThreadModel.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/branches/1.1/core/src/main/java/org/apache/mina/common/ExecutorThreadModel.java (original)
+++ mina/branches/1.1/core/src/main/java/org/apache/mina/common/ExecutorThreadModel.java Wed
Oct 24 23:20:04 2007
@@ -27,6 +27,7 @@
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.mina.filter.executor.ExecutorFilter;
+import org.apache.mina.util.NamePreservingRunnable;
 
 /**
  * A {@link ThreadModel} which represents a thread model with an {@link Executor}
@@ -87,9 +88,11 @@
             private final AtomicInteger threadId = new AtomicInteger(0);
 
             public Thread newThread(Runnable runnable) {
-                Thread t = originalThreadFactory.newThread(runnable);
-                t.setName(ExecutorThreadModel.this.threadNamePrefix + '-'
-                        + threadId.incrementAndGet());
+                Thread t = originalThreadFactory.newThread(
+                        new NamePreservingRunnable(
+                                runnable, 
+                                ExecutorThreadModel.this.threadNamePrefix + '-' +
+                                threadId.incrementAndGet()));
                 t.setDaemon(true);
                 return t;
             }

Modified: mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java
URL: http://svn.apache.org/viewvc/mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java
(original)
+++ mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketAcceptor.java
Wed Oct 24 23:20:04 2007
@@ -162,7 +162,7 @@
                 selector = Selector.open();
                 worker = new Worker();
 
-                executor.execute(new NamePreservingRunnable(worker));
+                executor.execute(new NamePreservingRunnable(worker, threadName));
             }
         }
     }
@@ -214,8 +214,6 @@
 
     private class Worker implements Runnable {
         public void run() {
-            Thread.currentThread().setName(SocketAcceptor.this.threadName);
-
             Selector selector = SocketAcceptor.this.selector;
             for (;;) {
                 try {

Modified: mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java
URL: http://svn.apache.org/viewvc/mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java
(original)
+++ mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java
Wed Oct 24 23:20:04 2007
@@ -232,7 +232,7 @@
             if (worker == null) {
                 selector = Selector.open();
                 worker = new Worker();
-                executor.execute(new NamePreservingRunnable(worker));
+                executor.execute(new NamePreservingRunnable(worker, threadName));
             }
         }
     }
@@ -351,8 +351,6 @@
         private long lastActive = System.currentTimeMillis();
 
         public void run() {
-            Thread.currentThread().setName(SocketConnector.this.threadName);
-
             Selector selector = SocketConnector.this.selector;
             for (;;) {
                 try {

Modified: mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketIoProcessor.java
URL: http://svn.apache.org/viewvc/mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketIoProcessor.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketIoProcessor.java
(original)
+++ mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketIoProcessor.java
Wed Oct 24 23:20:04 2007
@@ -82,7 +82,7 @@
             if (worker == null) {
                 selector = Selector.open();
                 worker = new Worker();
-                executor.execute(new NamePreservingRunnable(worker));
+                executor.execute(new NamePreservingRunnable(worker, threadName));
             }
             selector.wakeup();
         }
@@ -453,8 +453,6 @@
 
     private class Worker implements Runnable {
         public void run() {
-            Thread.currentThread().setName(SocketIoProcessor.this.threadName);
-
             Selector selector = SocketIoProcessor.this.selector;
             for (;;) {
                 try {

Modified: mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java
URL: http://svn.apache.org/viewvc/mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java
(original)
+++ mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramAcceptorDelegate.java
Wed Oct 24 23:20:04 2007
@@ -280,7 +280,8 @@
             if (worker == null) {
                 selector = Selector.open();
                 worker = new Worker();
-                executor.execute(new NamePreservingRunnable(worker));
+                executor.execute(
+                        new NamePreservingRunnable(worker, "DatagramAcceptor-" + id));
             }
         }
     }
@@ -308,8 +309,6 @@
 
     private class Worker implements Runnable {
         public void run() {
-            Thread.currentThread().setName("DatagramAcceptor-" + id);
-
             Selector selector = DatagramAcceptorDelegate.this.selector;
             for (;;) {
                 try {

Modified: mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramConnectorDelegate.java
URL: http://svn.apache.org/viewvc/mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramConnectorDelegate.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramConnectorDelegate.java
(original)
+++ mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/support/DatagramConnectorDelegate.java
Wed Oct 24 23:20:04 2007
@@ -200,7 +200,8 @@
             if (worker == null) {
                 selector = Selector.open();
                 worker = new Worker();
-                executor.execute(new NamePreservingRunnable(worker));
+                executor.execute(
+                        new NamePreservingRunnable(worker, "DatagramConnector-" + id));
             }
         }
     }
@@ -292,8 +293,6 @@
 
     private class Worker implements Runnable {
         public void run() {
-            Thread.currentThread().setName("DatagramConnector-" + id);
-
             Selector selector = DatagramConnectorDelegate.this.selector;
             for (;;) {
                 try {

Modified: mina/branches/1.1/core/src/main/java/org/apache/mina/util/NamePreservingRunnable.java
URL: http://svn.apache.org/viewvc/mina/branches/1.1/core/src/main/java/org/apache/mina/util/NamePreservingRunnable.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/branches/1.1/core/src/main/java/org/apache/mina/util/NamePreservingRunnable.java
(original)
+++ mina/branches/1.1/core/src/main/java/org/apache/mina/util/NamePreservingRunnable.java
Wed Oct 24 23:20:04 2007
@@ -18,27 +18,54 @@
  */
 package org.apache.mina.util;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
- * A Runnable wrapper that preserves the name of the thread after the runnable is complete
(for Runnables
- * that change the name of the Thread they use)
+ * A {@link Runnable} wrapper that preserves the name of the thread after the runnable is
+ * complete (for {@link Runnable}s that change the name of the Thread they use.)
  * 
- * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @author The Apache MINA Project (dev@mina.apache.org)
  * @version $Rev: 446581 $, $Date: 2006-09-15 11:36:12Z $,
  */
 public class NamePreservingRunnable implements Runnable {
+    private final Logger logger = LoggerFactory.getLogger(NamePreservingRunnable.class);
+
+    private final String newName;
     private final Runnable runnable;
 
-    public NamePreservingRunnable(Runnable runnable) {
+    public NamePreservingRunnable(Runnable runnable, String newName) {
         this.runnable = runnable;
+        this.newName = newName;
     }
 
     public void run() {
-        String name = Thread.currentThread().getName();
+        Thread currentThread = Thread.currentThread();
+        String oldName = currentThread.getName();
+        
+        if (newName != null) {
+            setName(currentThread, newName);
+        }
 
         try {
             runnable.run();
         } finally {
-            Thread.currentThread().setName(name);
+            setName(currentThread, oldName);
+        }
+    }
+    
+    /**
+     * Wraps {@link Thread#setName(String)} to catch a possible {@link Exception}s such as
+     * {@link SecurityException} in sandbox environments, such as applets
+     */
+    private void setName(Thread thread, String name) {
+        try {
+            thread.setName(name);
+        } catch (Exception e) {
+            // Probably SecurityException.
+            if (logger.isWarnEnabled()) {
+                logger.warn("Failed to set the thread name.", e);
+            }
         }
     }
 }

Modified: mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoProcessor.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoProcessor.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoProcessor.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoProcessor.java Wed Oct
24 23:20:04 2007
@@ -158,7 +158,7 @@
         synchronized (lock) {
             if (worker == null) {
                 worker = new Worker();
-                executor.execute(new NamePreservingRunnable(worker));
+                executor.execute(new NamePreservingRunnable(worker, threadName));
             }
         }
         wakeup();
@@ -519,8 +519,6 @@
     private class Worker implements Runnable {
         public void run() {
             int nSessions = 0;
-
-            Thread.currentThread().setName(AbstractIoProcessor.this.threadName);
             lastIdleCheckTime = System.currentTimeMillis();
 
             for (;;) {

Modified: mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioDatagramAcceptor.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioDatagramAcceptor.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioDatagramAcceptor.java
(original)
+++ mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioDatagramAcceptor.java
Wed Oct 24 23:20:04 2007
@@ -32,11 +32,11 @@
 import java.util.concurrent.Executor;
 
 import org.apache.mina.common.AbstractIoAcceptor;
-import org.apache.mina.common.IoBuffer;
 import org.apache.mina.common.ExceptionMonitor;
 import org.apache.mina.common.ExpiringSessionRecycler;
 import org.apache.mina.common.IdleStatusChecker;
 import org.apache.mina.common.IoAcceptor;
+import org.apache.mina.common.IoBuffer;
 import org.apache.mina.common.IoProcessor;
 import org.apache.mina.common.IoServiceListenerSupport;
 import org.apache.mina.common.IoSession;
@@ -256,7 +256,8 @@
     private synchronized void startupWorker() {
         if (worker == null) {
             worker = new Worker();
-            executor.execute(new NamePreservingRunnable(worker));
+            executor.execute(
+                    new NamePreservingRunnable(worker, "DatagramAcceptor-" + id));
         }
     }
 
@@ -271,7 +272,6 @@
 
     private class Worker implements Runnable {
         public void run() {
-            Thread.currentThread().setName("DatagramAcceptor-" + id);
             lastIdleCheckTime = System.currentTimeMillis();
 
             for (; ;) {

Modified: mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketAcceptor.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketAcceptor.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketAcceptor.java
(original)
+++ mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketAcceptor.java
Wed Oct 24 23:20:04 2007
@@ -259,7 +259,7 @@
             if (worker == null) {
                 worker = new Worker();
 
-                executor.execute(new NamePreservingRunnable(worker));
+                executor.execute(new NamePreservingRunnable(worker, threadName));
             }
         }
     }
@@ -284,8 +284,6 @@
      */
     private class Worker implements Runnable {
         public void run() {
-            Thread.currentThread().setName(NioSocketAcceptor.this.threadName);
-
             for (; ;) {
                 try {
                     // gets the number of keys that are ready to go

Modified: mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketConnector.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketConnector.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketConnector.java
(original)
+++ mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketConnector.java
Wed Oct 24 23:20:04 2007
@@ -202,7 +202,7 @@
         synchronized (lock) {
             if (worker == null) {
                 worker = new Worker();
-                executor.execute(new NamePreservingRunnable(worker));
+                executor.execute(new NamePreservingRunnable(worker, threadName));
             }
         }
     }
@@ -327,8 +327,6 @@
         private long lastActive = System.currentTimeMillis();
 
         public void run() {
-            Thread.currentThread().setName(NioSocketConnector.this.threadName);
-
             for (; ;) {
                 try {
                     int nKeys = selector.select(1000);

Modified: mina/trunk/core/src/main/java/org/apache/mina/util/NamePreservingRunnable.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/util/NamePreservingRunnable.java?rev=588150&r1=588149&r2=588150&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/util/NamePreservingRunnable.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/util/NamePreservingRunnable.java Wed Oct
24 23:20:04 2007
@@ -18,27 +18,54 @@
  */
 package org.apache.mina.util;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
- * A Runnable wrapper that preserves the name of the thread after the runnable is complete
(for Runnables
- * that change the name of the Thread they use)
- *
+ * A {@link Runnable} wrapper that preserves the name of the thread after the runnable is
+ * complete (for {@link Runnable}s that change the name of the Thread they use.)
+ * 
  * @author The Apache MINA Project (dev@mina.apache.org)
  * @version $Rev: 446581 $, $Date: 2006-09-15 11:36:12Z $,
  */
 public class NamePreservingRunnable implements Runnable {
+    private final Logger logger = LoggerFactory.getLogger(NamePreservingRunnable.class);
+
+    private final String newName;
     private final Runnable runnable;
 
-    public NamePreservingRunnable(Runnable runnable) {
+    public NamePreservingRunnable(Runnable runnable, String newName) {
         this.runnable = runnable;
+        this.newName = newName;
     }
 
     public void run() {
-        String name = Thread.currentThread().getName();
+        Thread currentThread = Thread.currentThread();
+        String oldName = currentThread.getName();
+        
+        if (newName != null) {
+            setName(currentThread, newName);
+        }
 
         try {
             runnable.run();
         } finally {
-            Thread.currentThread().setName(name);
+            setName(currentThread, oldName);
+        }
+    }
+    
+    /**
+     * Wraps {@link Thread#setName(String)} to catch a possible {@link Exception}s such as
+     * {@link SecurityException} in sandbox environments, such as applets
+     */
+    private void setName(Thread thread, String name) {
+        try {
+            thread.setName(name);
+        } catch (Exception e) {
+            // Probably SecurityException.
+            if (logger.isWarnEnabled()) {
+                logger.warn("Failed to set the thread name.", e);
+            }
         }
     }
 }



Mime
View raw message