Author: rmannibucau Date: Mon Aug 12 11:32:54 2013 New Revision: 1513110 URL: http://svn.apache.org/r1513110 Log: adding thread plugin
Added: commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/plugin/thread/ commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/plugin/thread/ThreadEndpoints.java commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/plugin/thread/ThreadPlugin.java commons/sandbox/monitoring/trunk/reporting/src/main/resources/templates/threads/ commons/sandbox/monitoring/trunk/reporting/src/main/resources/templates/threads/thread.vm commons/sandbox/monitoring/trunk/reporting/src/main/resources/templates/threads/threads.vm Modified: commons/sandbox/monitoring/trunk/reporting/src/main/resources/META-INF/services/org.apache.commons.monitoring.reporting.web.plugin.Plugin Added: commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/plugin/thread/ThreadEndpoints.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/plugin/thread/ThreadEndpoints.java?rev=1513110&view=auto ============================================================================== --- commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/plugin/thread/ThreadEndpoints.java (added) +++ commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/plugin/thread/ThreadEndpoints.java Mon Aug 12 11:32:54 2013 @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.monitoring.reporting.web.plugin.thread; + +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.lang3.StringEscapeUtils; +import org.apache.commons.monitoring.reporting.web.handler.api.Regex; +import org.apache.commons.monitoring.reporting.web.handler.api.Template; +import org.apache.commons.monitoring.reporting.web.template.MapBuilder; + +import java.util.Map; +import java.util.TreeMap; + +public class ThreadEndpoints { + @Regex + public Template home() { + return new Template("threads/threads.vm", new MapBuilder<String, Object>() + .set("threads", listThreads()) + .build()); + } + + @Regex("/([^/]*)") + public Template dump(final String name) { + final Thread thread = findThread(new String(Base64.decodeBase64(name))); + if (thread == null) { + return new Template("templates/threads/thread.vm", new MapBuilder<String, Object>().set("state", "Not found").build(), false); + } + + return new Template( + "templates/threads/thread.vm", + new MapBuilder<String, Object>().set("state", thread.getState().name()).set("dump", StringEscapeUtils.escapeHtml4(dump(thread))).build(), + false); + } + + private static Thread findThread(final String name) { + int count = Thread.activeCount(); + final Thread[] threads = new Thread[count]; + count = Thread.enumerate(threads); + + for (int i = 0; i < count; i++) { + if (threads[i].getName().equals(name)) { + return threads[i]; + } + } + + return null; + } + + private static String dump(final Thread thread) { + final StackTraceElement[] stack = thread.getStackTrace(); + final StringBuilder builder = new StringBuilder(); + for (final StackTraceElement element : stack) { + builder.append(element.toString()).append("\n"); // toString method is fine + } + return builder.toString(); + } + + private static Map<String, String> listThreads() { + final Map<String, String> out = new TreeMap<String, String>(); + + int count = Thread.activeCount(); + final Thread[] threads = new Thread[count]; + count = Thread.enumerate(threads); + + for (int i = 0; i < count; i++) { + final String name = threads[i].getName(); + out.put(name, Base64.encodeBase64String(name.getBytes())); + } + + return out; + } +} Added: commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/plugin/thread/ThreadPlugin.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/plugin/thread/ThreadPlugin.java?rev=1513110&view=auto ============================================================================== --- commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/plugin/thread/ThreadPlugin.java (added) +++ commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/plugin/thread/ThreadPlugin.java Mon Aug 12 11:32:54 2013 @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.monitoring.reporting.web.plugin.thread; + +import org.apache.commons.monitoring.reporting.web.plugin.Plugin; + +public class ThreadPlugin implements Plugin { + @Override + public String name() { + return "Threads"; + } + + @Override + public Class<?> endpoints() { + return ThreadEndpoints.class; + } + + @Override + public String mapping() { + return "/threads"; + } +} Modified: commons/sandbox/monitoring/trunk/reporting/src/main/resources/META-INF/services/org.apache.commons.monitoring.reporting.web.plugin.Plugin URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/resources/META-INF/services/org.apache.commons.monitoring.reporting.web.plugin.Plugin?rev=1513110&r1=1513109&r2=1513110&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/reporting/src/main/resources/META-INF/services/org.apache.commons.monitoring.reporting.web.plugin.Plugin (original) +++ commons/sandbox/monitoring/trunk/reporting/src/main/resources/META-INF/services/org.apache.commons.monitoring.reporting.web.plugin.Plugin Mon Aug 12 11:32:54 2013 @@ -2,3 +2,4 @@ org.apache.commons.monitoring.reporting. org.apache.commons.monitoring.reporting.web.plugin.jmx.JMXPlugin org.apache.commons.monitoring.reporting.web.plugin.jvm.JVMPlugin org.apache.commons.monitoring.reporting.web.plugin.jta.JTAPlugin +org.apache.commons.monitoring.reporting.web.plugin.thread.ThreadPlugin Added: commons/sandbox/monitoring/trunk/reporting/src/main/resources/templates/threads/thread.vm URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/resources/templates/threads/thread.vm?rev=1513110&view=auto ============================================================================== --- commons/sandbox/monitoring/trunk/reporting/src/main/resources/templates/threads/thread.vm (added) +++ commons/sandbox/monitoring/trunk/reporting/src/main/resources/templates/threads/thread.vm Mon Aug 12 11:32:54 2013 @@ -0,0 +1,24 @@ +#* + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*# +<p> + State: $state +</p> +<p> + #if ($dump) +$dump + #end +</p> \ No newline at end of file Added: commons/sandbox/monitoring/trunk/reporting/src/main/resources/templates/threads/threads.vm URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/resources/templates/threads/threads.vm?rev=1513110&view=auto ============================================================================== --- commons/sandbox/monitoring/trunk/reporting/src/main/resources/templates/threads/threads.vm (added) +++ commons/sandbox/monitoring/trunk/reporting/src/main/resources/templates/threads/threads.vm Mon Aug 12 11:32:54 2013 @@ -0,0 +1,49 @@ +#* + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*# +<div class="page-header"> + <h2>Threads</h2> +</div> + +<div class="container-fluid"> + <div class="row-fluid"> + <div class="span2" style="word-break: break-all; word-wrap: break-word;"> + #foreach ($thread in $threads.entrySet()) + <ul> + <li><a id="$thread.value" class="thread" href="#">$thread.key</a></li> + </ul> + #end + </div> + <div class="span6"> + <div id="thread-info" class="span10"> + Click on the left list to select a thread to show. + </div> + </div> + </div> +</div> + +<script type="text/javascript"> + $(function() { + $('.thread').click(function (evt) { + evt.preventDefault(); + + var url = '$mapping/threads/' + $(this).attr("id"); + $.get(url, function(data) { + $("#thread-info").html(data); + }); + }); + }); +</script>