View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.mina.util;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  /**
25   * A {@link Runnable} wrapper that preserves the name of the thread after the runnable is
26   * complete (for {@link Runnable}s that change the name of the Thread they use.)
27   *
28   * @author The Apache MINA Project (dev@mina.apache.org)
29   * @version $Rev: 446581 $, $Date: 2006-09-15 11:36:12Z $,
30   */
31  public class NamePreservingRunnable implements Runnable {
32      private final Logger logger = LoggerFactory.getLogger(NamePreservingRunnable.class);
33  
34      /** The runnable name */
35      private final String newName;
36      
37      /** The runnable task */
38      private final Runnable runnable;
39  
40      /**
41       * Creates a new instance of NamePreservingRunnable.
42       *
43       * @param runnable The underlying runnable
44       * @param newName The runnable's name
45       */
46      public NamePreservingRunnable(Runnable runnable, String newName) {
47          this.runnable = runnable;
48          this.newName = newName;
49      }
50  
51      /**
52       * Run the runnable after having renamed the current thread's name 
53       * to the new name. When the runnable has completed, set back the 
54       * current thread name back to its origin. 
55       */
56      public void run() {
57          Thread currentThread = Thread.currentThread();
58          String oldName = currentThread.getName();
59  
60          if (newName != null) {
61              setName(currentThread, newName);
62          }
63  
64          try {
65              runnable.run();
66          } finally {
67              setName(currentThread, oldName);
68          }
69      }
70  
71      /**
72       * Wraps {@link Thread#setName(String)} to catch a possible {@link Exception}s such as
73       * {@link SecurityException} in sandbox environments, such as applets
74       */
75      private void setName(Thread thread, String name) {
76          try {
77              thread.setName(name);
78          } catch (SecurityException se) {
79              if (logger.isWarnEnabled()) {
80                  logger.warn("Failed to set the thread name.", se);
81              }
82          }
83      }
84  }