001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.log4j.or;
019
020import org.apache.log4j.Layout;
021
022
023/**
024   Render {@link ThreadGroup} objects in a format similar to the
025   information output by the {@link ThreadGroup#list} method.
026   @author Ceki Gülcü
027   @since 1.0 */
028public class ThreadGroupRenderer implements ObjectRenderer {
029
030  public
031  ThreadGroupRenderer() {
032  }
033  
034  /**
035     Render a {@link ThreadGroup} object similar to the way that the
036     {@link ThreadGroup#list} method output information. 
037
038     <p>The output of a simple program consisting of one
039     <code>main</code> thread is:
040     <pre>
041     java.lang.ThreadGroup[name=main, maxpri=10]
042         Thread=[main,5,false]
043     </pre>
044     
045     <p>The boolean value in thread information is the value returned
046     by {@link Thread#isDaemon}.
047     
048  */
049  public
050  String  doRender(Object o) {
051    if(o instanceof ThreadGroup) {
052      StringBuffer sbuf = new StringBuffer();
053      ThreadGroup tg = (ThreadGroup) o;
054      sbuf.append("java.lang.ThreadGroup[name=");
055      sbuf.append(tg.getName());
056      sbuf.append(", maxpri=");
057      sbuf.append(tg.getMaxPriority());
058      sbuf.append("]");
059      Thread[] t = new Thread[tg.activeCount()];
060      tg.enumerate(t);
061      for(int i = 0; i < t.length; i++) {
062        sbuf.append(Layout.LINE_SEP);   
063        sbuf.append("   Thread=[");
064        sbuf.append(t[i].getName());
065        sbuf.append(",");
066        sbuf.append(t[i].getPriority());
067        sbuf.append(",");
068        sbuf.append(t[i].isDaemon());
069        sbuf.append("]");
070      }
071      return sbuf.toString();
072    } else {
073      try {
074        // this is the best we can do
075        return o.toString();
076      } catch(Exception ex) {
077          return ex.toString();
078      }
079    }    
080  }
081}