ThreadGroupRenderer xref

View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.log4j.or;
    19
    20  import org.apache.log4j.Layout;
    21
    22
    23  /**
    24     Render {@link ThreadGroup} objects in a format similar to the
    25     information output by the {@link ThreadGroup#list} method.
    26     @author Ceki G&uuml;lc&uuml;
    27     @since 1.0 */
    28  public class ThreadGroupRenderer implements ObjectRenderer {
    29
    30    public
    31    ThreadGroupRenderer() {
    32    }
    33
    34    /**
    35       Render a {@link ThreadGroup} object similar to the way that the
    36       {@link ThreadGroup#list} method output information.
    37
    38       <p>The output of a simple program consisting of one
    39       <code>main</code> thread is:
    40       <pre>
    41       java.lang.ThreadGroup[name=main, maxpri=10]
    42           Thread=[main,5,false]
    43       </pre>
    44
    45       <p>The boolean value in thread information is the value returned
    46       by {@link Thread#isDaemon}.
    47
    48    */
    49    public
    50    String  doRender(Object o) {
    51      if(o instanceof ThreadGroup) {
    52        StringBuffer sbuf = new StringBuffer();
    53        ThreadGroup tg = (ThreadGroup) o;
    54        sbuf.append("java.lang.ThreadGroup[name=");
    55        sbuf.append(tg.getName());
    56        sbuf.append(", maxpri=");
    57        sbuf.append(tg.getMaxPriority());
    58        sbuf.append("]");
    59        Thread[] t = new Thread[tg.activeCount()];
    60        tg.enumerate(t);
    61        for(int i = 0; i < t.length; i++) {
    62  	sbuf.append(Layout.LINE_SEP);
    63  	sbuf.append("   Thread=[");
    64  	sbuf.append(t[i].getName());
    65  	sbuf.append(",");
    66  	sbuf.append(t[i].getPriority());
    67  	sbuf.append(",");
    68  	sbuf.append(t[i].isDaemon());
    69  	sbuf.append("]");
    70        }
    71        return sbuf.toString();
    72      } else {
    73        try {
    74          // this is the best we can do
    75          return o.toString();
    76        } catch(Exception ex) {
    77            return ex.toString();
    78        }
    79      }
    80    }
    81  }