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  package org.apache.commons.monitoring.reporting.web.plugin.thread;
18  
19  import org.apache.commons.codec.binary.Base64;
20  import org.apache.commons.lang3.StringEscapeUtils;
21  import org.apache.commons.monitoring.reporting.web.handler.api.Regex;
22  import org.apache.commons.monitoring.reporting.web.handler.api.Template;
23  import org.apache.commons.monitoring.reporting.web.template.MapBuilder;
24  
25  import java.util.Map;
26  import java.util.TreeMap;
27  
28  public class ThreadEndpoints {
29      @Regex
30      public Template home() {
31          return new Template("threads/threads.vm", new MapBuilder<String, Object>()
32                                  .set("threads", listThreads())
33                                  .build());
34      }
35  
36      @Regex("/([^/]*)")
37      public Template dump(final String name) {
38          final Thread thread = findThread(new String(Base64.decodeBase64(name)));
39          if (thread == null) {
40              return new Template("templates/threads/thread.vm", new MapBuilder<String, Object>().set("state", "Not found").build(), false);
41          }
42  
43          return new Template(
44                  "templates/threads/thread.vm",
45                  new MapBuilder<String, Object>().set("state", thread.getState().name()).set("dump", StringEscapeUtils.escapeHtml4(dump(thread))).build(),
46                  false);
47      }
48  
49      private static Thread findThread(final String name) {
50          int count = Thread.activeCount();
51          final Thread[] threads = new Thread[count];
52          count = Thread.enumerate(threads);
53  
54          for (int i = 0; i < count; i++) {
55              if (threads[i].getName().equals(name)) {
56                  return threads[i];
57              }
58          }
59  
60          return null;
61      }
62  
63      private static String dump(final Thread thread) {
64          final StackTraceElement[] stack = thread.getStackTrace();
65          final StringBuilder builder = new StringBuilder();
66          for (final StackTraceElement element : stack) {
67              builder.append(element.toString()).append("\n"); // toString method is fine
68          }
69          return builder.toString();
70      }
71  
72      private static Map<String, String> listThreads() {
73          final Map<String, String> out = new TreeMap<String, String>();
74  
75          int count = Thread.activeCount();
76          final Thread[] threads = new Thread[count];
77          count = Thread.enumerate(threads);
78  
79          for (int i = 0; i < count; i++) {
80              final String name = threads[i].getName();
81              out.put(name, Base64.encodeBase64String(name.getBytes()));
82          }
83  
84          return out;
85      }
86  }