Mercurial > hg > heapstats
changeset 155:f8b5f871b0cb
Bug 2895: Code duplication for HeapStats MBean
Reviewed-by: ykubota
GitHub: https://github.com/HeapStats/heapstats/pull/23
author | Yasumasa Suenaga <yasuenag@gmail.com> |
---|---|
date | Mon, 28 Mar 2016 21:40:24 +0900 |
parents | 96e06914c625 |
children | 6a568c9776c3 |
files | ChangeLog mbean/java/src/META-INF/jboss-service.xml mbean/java/src/jp/co/ntt/oss/heapstats/mbean/HeapStats.java mbean/java/src/jp/co/ntt/oss/heapstats/mbean/HeapStatsMBean.java |
diffstat | 4 files changed, 4 insertions(+), 416 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Tue Mar 15 12:49:24 2016 +0900 +++ b/ChangeLog Mon Mar 28 21:40:24 2016 +0900 @@ -1,3 +1,7 @@ +2016-03-28 Yasumasa Suenaga <yasuenag@gmail.com> + + * Bug 2895: Code duplication for HeapStats MBean + 2016-03-15 Yasumasa Suenaga <yasuenag@gmail.com> * Bug 2794: Remove dependency on net-snmp-libs.
--- a/mbean/java/src/META-INF/jboss-service.xml Tue Mar 15 12:49:24 2016 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> - -<!-- - Copyright (C) 2015 Yasumasa Suenaga - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. ---> - -<server> - <mbean code="jp.co.ntt.oss.heapstats.mbean.HeapStats" - name="heapstats:service=HeapStats" /> -</server>
--- a/mbean/java/src/jp/co/ntt/oss/heapstats/mbean/HeapStats.java Tue Mar 15 12:49:24 2016 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,254 +0,0 @@ -/* - * HeapStats.java - * - * Copyright (C) 2015 Yasumasa Suenaga - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -package jp.co.ntt.oss.heapstats.mbean; - -import java.io.File; -import java.io.IOException; -import java.io.Closeable; -import java.io.FileInputStream; -import java.nio.channels.FileChannel; -import java.nio.channels.SocketChannel; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.util.Map; - - -/** - * Implementation of HeapStatsMBean - * - * @author Yasumasa Suenaga - */ -public class HeapStats implements HeapStatsMBean{ - - static{ - System.loadLibrary("heapstats-mbean"); - registerNatives(); - } - - /** - * Register JNI functions in libheapstats. - */ - public static native void registerNatives(); - - /** - * Get HeapStats version string from libheapstats. - * - * @return Version string which is attached. - */ - private native String getHeapStatsVersion0(); - - /** - * Get HeapStats agent configuration from libheapstats. - * - * @param key Name of configuration. - * @return Current value of configuration key. - */ - private native Object getConfiguration0(String key); - - /** - * Get all configurations in libheapstats. - * Key is configuration name in heapstats.conf, value is current value. - * - * @return Current configuration list. - */ - private native Map<String, Object> getConfigurationList0(); - - /** - * Change HeapStats agent configuration in libheapstats. - * - * @param key Name of configuration. - * @param value New configuration value. - * @return Result of this change. - */ - private native boolean changeConfiguration0(String key, Object value); - - /** - * Invoke Resource Log collection at libheapstats. - * - * @return Result of this call. - */ - private native boolean invokeLogCollection0(); - - /** - * Invoke All Log collection at libheapstats. - * - * @return Result of this call. - */ - private native boolean invokeAllLogCollection0(); - - /** - * {@inheritDoc} - */ - @Override - public String getHeapStatsVersion(){ - return getHeapStatsVersion0(); - } - - /** - * Close Closeable resource silently. - * - * @param resource Resource to be closed. - */ - private void closeSilently(Closeable resource){ - try{ - resource.close(); - } - catch(NullPointerException e){ - // Do nothing - } - catch(Exception e){ - e.printStackTrace(); - } - } - - /** - * Send file through socket. - * - * @param fname Filename to be sent. - * @param address InetAddress of receiver. - * @param port Port number of receiver. - */ - private void sendFile(String fname, InetAddress address, int port){ - File file = new File(fname); - if(!file.isFile()){ - throw new RuntimeException(new IOException(fname + " does not exist.")); - } - - FileInputStream stream = null; - SocketChannel sock = null; - InetSocketAddress remote = new InetSocketAddress(address, port); - try{ - stream = new FileInputStream(file); - FileChannel ch = stream.getChannel(); - sock = SocketChannel.open(remote); - - ch.transferTo(0, file.length(), sock); - } - catch(IOException e){ - throw new RuntimeException(e); - } - finally{ - closeSilently(stream); - closeSilently(sock); - } - - } - - /** - * {@inheritDoc} - */ - @Override - public void getSnapShot(InetAddress address, int port){ - sendFile((String)getConfiguration("file"), address, port); - } - - /** - * {@inheritDoc} - */ - @Override - public void getResourceLog(InetAddress address, int port){ - sendFile((String)getConfiguration("heaplogfile"), address, port); - } - - /** - * {@inheritDoc} - */ - @Override - public Object getConfiguration(String key){ - return getConfiguration0(key); - } - - /** - * {@inheritDoc} - */ - @Override - public Map<String, Object> getConfigurationList(){ - return getConfigurationList0(); - } - - /** - * {@inheritDoc} - */ - @Override - public boolean changeConfiguration(String key, Object value){ - return changeConfiguration0(key, value); - } - - /** - * Call System.gc() to get Heap SnapShot. - */ - @Override - public void invokeSnapShotCollection(){ - // Invoke GC to collect SnapShot. - System.gc(); - } - - /** - * {@inheritDoc} - */ - @Override - public boolean invokeLogCollection(){ - return invokeLogCollection0(); - } - - /** - * {@inheritDoc} - */ - @Override - public boolean invokeAllLogCollection(){ - return invokeAllLogCollection0(); - } - - /** - * {@inheritDoc} - */ - @Override - public void create() throws Exception{ - // Do nothing; - } - - /** - * {@inheritDoc} - */ - @Override - public void start() throws Exception{ - // Do nothing; - } - - /** - * {@inheritDoc} - */ - @Override - public void stop() throws Exception{ - // Do nothing; - } - - /** - * {@inheritDoc} - */ - @Override - public void destroy() throws Exception{ - // Do nothing; - } - -} -
--- a/mbean/java/src/jp/co/ntt/oss/heapstats/mbean/HeapStatsMBean.java Tue Mar 15 12:49:24 2016 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,137 +0,0 @@ -/* - * HeapStatsMBean.java - * - * Copyright (C) 2015 Yasumasa Suenaga - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -package jp.co.ntt.oss.heapstats.mbean; - -import java.net.InetAddress; -import java.util.Map; - - -/** - * MBean interface for HeapStats. - * - * @author Yasumasa Suenaga - */ -public interface HeapStatsMBean{ - - public static enum LogLevel{ - UNKNOWN, - CRIT, - WARN, - INFO, - DEBUG - } - - public static enum RankOrder{ - DELTA, - USAGE - } - - /** - * Get HeapStats version string. - * - * @return Version string which is attached. - */ - public String getHeapStatsVersion(); - - /** - * Get SnapShot data through socket. - * - * @param address InetAddress of receiver. - * @param port Port number of receiver. - */ - public void getSnapShot(InetAddress address, int port); - - /** - * Get Resource log data (CSV) through socket. - * - * @param address InetAddress of receiver. - * @param port Port number of receiver. - */ - public void getResourceLog(InetAddress address, int port); - - /** - * Get HeapStats agent configuration. - * - * @param key Name of configuration. - * @return Current value of configuration key. - */ - public Object getConfiguration(String key); - - /** - * Get all configurations in HeapStats agent. - * Key is configuration name in heapstats.conf, value is current value. - * - * @return Current configuration list. - */ - public Map<String, Object> getConfigurationList(); - - /** - * Change HeapStats agent configuration. - * - * @param key Name of configuration. - * @param value New configuration value. - * @return Result of this change. - */ - public boolean changeConfiguration(String key, Object value); - - /** - * Invoke Heap SnapShot collection. - * This function might just call System.gc() . - */ - public void invokeSnapShotCollection(); - - /** - * Invoke Resource Log collection. - * - * @return Result of this call. - */ - public boolean invokeLogCollection(); - - /** - * Invoke All Log collection. - * - * @return Result of this call. - */ - public boolean invokeAllLogCollection(); - - /** - * This function is for WildFly/JBoss. - */ - public void create() throws Exception; - - /** - * This function is for WildFly/JBoss. - */ - public void start() throws Exception; - - /** - * This function is for WildFly/JBoss. - */ - public void stop() throws Exception; - - /** - * This function is for WildFly/JBoss. - */ - public void destroy() throws Exception; - -} -