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 <a href="http://mina.apache.org">Apache MINA Project</a>
29   */
30  public class NamePreservingRunnable implements Runnable {
31      private final static Logger LOGGER = LoggerFactory.getLogger(NamePreservingRunnable.class);
32  
33      /** The runnable name */
34      private final String newName;
35  
36      /** The runnable task */
37      private final Runnable runnable;
38  
39      /**
40       * Creates a new instance of NamePreservingRunnable.
41       *
42       * @param runnable The underlying runnable
43       * @param newName The runnable's name
44       */
45      public NamePreservingRunnable(Runnable runnable, String newName) {
46          this.runnable = runnable;
47          this.newName = newName;
48      }
49  
50      /**
51       * Run the runnable after having renamed the current thread's name 
52       * to the new name. When the runnable has completed, set back the 
53       * current thread name back to its origin. 
54       */
55      public void run() {
56          Thread currentThread = Thread.currentThread();
57          String oldName = currentThread.getName();
58  
59          if (newName != null) {
60              setName(currentThread, newName);
61          }
62  
63          try {
64              runnable.run();
65          } finally {
66              setName(currentThread, oldName);
67          }
68      }
69  
70      /**
71       * Wraps {@link Thread#setName(String)} to catch a possible {@link Exception}s such as
72       * {@link SecurityException} in sandbox environments, such as applets
73       */
74      private void setName(Thread thread, String name) {
75          try {
76              thread.setName(name);
77          } catch (SecurityException se) {
78              if (LOGGER.isWarnEnabled()) {
79                  LOGGER.warn("Failed to set the thread name.", se);
80              }
81          }
82      }
83  }