KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > or > ThreadGroupRenderer


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.or;
18
19 import org.apache.log4j.Layout;
20
21
22 /**
23    Render {@link ThreadGroup} objects in a format similar to the
24    information output by the {@link ThreadGroup#list} method.
25    @author Ceki Gülcü
26    @since 1.0 */

27 public class ThreadGroupRenderer implements ObjectRenderer {
28
29   public
30   ThreadGroupRenderer() {
31   }
32   
33   /**
34      Render a {@link ThreadGroup} object similar to the way that the
35      {@link ThreadGroup#list} method output information.
36
37      <p>The output of a simple program consisting of one
38      <code>main</code> thread is:
39      <pre>
40      java.lang.ThreadGroup[name=main, maxpri=10]
41          Thread=[main,5,false]
42      </pre>
43      
44      <p>The boolean value in thread information is the value returned
45      by {@link Thread#isDaemon}.
46      
47   */

48   public
49   String JavaDoc doRender(Object JavaDoc o) {
50     if(o instanceof ThreadGroup JavaDoc) {
51       StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
52       ThreadGroup JavaDoc tg = (ThreadGroup JavaDoc) o;
53       sbuf.append("java.lang.ThreadGroup[name=");
54       sbuf.append(tg.getName());
55       sbuf.append(", maxpri=");
56       sbuf.append(tg.getMaxPriority());
57       sbuf.append("]");
58       Thread JavaDoc[] t = new Thread JavaDoc[tg.activeCount()];
59       tg.enumerate(t);
60       for(int i = 0; i < t.length; i++) {
61     sbuf.append(Layout.LINE_SEP);
62     sbuf.append(" Thread=[");
63     sbuf.append(t[i].getName());
64     sbuf.append(",");
65     sbuf.append(t[i].getPriority());
66     sbuf.append(",");
67     sbuf.append(t[i].isDaemon());
68     sbuf.append("]");
69       }
70       return sbuf.toString();
71     } else {
72       // this is the best we can do
73
return o.toString();
74     }
75   }
76 }
77
Popular Tags