Skip to content

Commit

Permalink
Reformat code. Add .editorconfig file.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryatoa committed Sep 20, 2019
1 parent d557169 commit 1f51b27
Show file tree
Hide file tree
Showing 64 changed files with 4,823 additions and 4,758 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8

[*.java]
indent_style = space
indent_size = 4
continuation_indent_size = 8
74 changes: 37 additions & 37 deletions top4j-cli/src/main/java/io/top4j/cli/ConsoleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.*;
import java.util.logging.Logger;

public class ConsoleController extends TimerTask {
public class ConsoleController extends TimerTask {

private final ConsoleReader consoleReader;
private final UserInput userInput;
Expand Down Expand Up @@ -64,14 +64,14 @@ public class ConsoleController extends TimerTask {

private static final Logger LOGGER = Logger.getLogger(ConsoleController.class.getName());

public ConsoleController ( ConsoleReader consoleReader, UserInput userInput, MBeanServerConnection mbsc, DisplayConfig displayConfig ) {
public ConsoleController(ConsoleReader consoleReader, UserInput userInput, MBeanServerConnection mbsc, DisplayConfig displayConfig) {

this.consoleReader = consoleReader;
this.userInput = userInput;
this.displayConfig = displayConfig;
int displayThreadCount = displayConfig.getThreadCount();
try {
this.threadHelper = new ThreadHelper( mbsc );
this.threadHelper = new ThreadHelper(mbsc);
} catch (IOException e) {
String errorMessage = INIT_ERROR_MESSAGE + e.getMessage();
LOGGER.severe(errorMessage);
Expand All @@ -81,7 +81,7 @@ public ConsoleController ( ConsoleReader consoleReader, UserInput userInput, MBe
// create GCStats objectName
ObjectName gcStatsObjectName = null;
try {
gcStatsObjectName = new ObjectName(Constants.DOMAIN + ":type=" + Constants.JVM_STATS_TYPE + ",statsType=" + Constants.GC_STATS_TYPE );
gcStatsObjectName = new ObjectName(Constants.DOMAIN + ":type=" + Constants.JVM_STATS_TYPE + ",statsType=" + Constants.GC_STATS_TYPE);
} catch (MalformedObjectNameException e) {
String errorMessage = INIT_ERROR_MESSAGE + e.getMessage();
LOGGER.severe(errorMessage);
Expand All @@ -95,7 +95,7 @@ public ConsoleController ( ConsoleReader consoleReader, UserInput userInput, MBe
// create MemoryStats objectName
ObjectName memoryStatsObjectName = null;
try {
memoryStatsObjectName = new ObjectName(Constants.DOMAIN + ":type=" + Constants.JVM_STATS_TYPE + ",statsType=" + Constants.MEMORY_STATS_TYPE );
memoryStatsObjectName = new ObjectName(Constants.DOMAIN + ":type=" + Constants.JVM_STATS_TYPE + ",statsType=" + Constants.MEMORY_STATS_TYPE);
} catch (MalformedObjectNameException e) {
String errorMessage = INIT_ERROR_MESSAGE + e.getMessage();
LOGGER.severe(errorMessage);
Expand All @@ -109,7 +109,7 @@ public ConsoleController ( ConsoleReader consoleReader, UserInput userInput, MBe
// create HeapStats objectName
ObjectName heapStatsObjectName = null;
try {
heapStatsObjectName = new ObjectName(Constants.DOMAIN + ":type=" + Constants.JVM_STATS_TYPE + ",statsType=" + Constants.HEAP_STATS_TYPE );
heapStatsObjectName = new ObjectName(Constants.DOMAIN + ":type=" + Constants.JVM_STATS_TYPE + ",statsType=" + Constants.HEAP_STATS_TYPE);
} catch (MalformedObjectNameException e) {
String errorMessage = INIT_ERROR_MESSAGE + e.getMessage();
LOGGER.severe(errorMessage);
Expand All @@ -123,7 +123,7 @@ public ConsoleController ( ConsoleReader consoleReader, UserInput userInput, MBe
// create ThreadStats objectName
ObjectName threadStatsObjectName = null;
try {
threadStatsObjectName = new ObjectName(Constants.DOMAIN + ":type=" + Constants.JVM_STATS_TYPE + ",statsType=" + Constants.THREADS_STATS_TYPE );
threadStatsObjectName = new ObjectName(Constants.DOMAIN + ":type=" + Constants.JVM_STATS_TYPE + ",statsType=" + Constants.THREADS_STATS_TYPE);
} catch (MalformedObjectNameException e) {
String errorMessage = INIT_ERROR_MESSAGE + e.getMessage();
LOGGER.severe(errorMessage);
Expand All @@ -135,7 +135,7 @@ public ConsoleController ( ConsoleReader consoleReader, UserInput userInput, MBe
this.jvmStatsMBeans.add(this.threadStatsMXBean);

// populate topThread MBean list
for (int rank =1; rank <=displayThreadCount; rank++) {
for (int rank = 1; rank <= displayThreadCount; rank++) {

// create TopThread objectName
ObjectName topThreadObjectName = null;
Expand All @@ -151,7 +151,7 @@ public ConsoleController ( ConsoleReader consoleReader, UserInput userInput, MBe
}

// populate blockedThread MBean list
for (int rank =1; rank <=displayThreadCount; rank++) {
for (int rank = 1; rank <= displayThreadCount; rank++) {

// create BlockedThread objectName
ObjectName blockedThreadObjectName = null;
Expand Down Expand Up @@ -210,7 +210,7 @@ public void run() {
private void updateScreen() throws ScreenUpdateException {

// first things first, check MBean Server Connection is still alive
if (! isMBSConnectionAlive()) {
if (!isMBSConnectionAlive()) {
// the connection to the remote MBean Server has been lost - we have no choice but to exit gracefully
throw new ScreenUpdateException("ERROR: The connection to the remote MBean Server has been lost. Check if the target JVM is still running.");
}
Expand All @@ -224,10 +224,10 @@ private void updateScreen() throws ScreenUpdateException {
mainScreenId = screenId;
String screen;
try {
if ( userInput.isDigit() ) {
if (userInput.isDigit()) {
// create thread stack trace screen
screen = createThreadStackTraceScreen(Integer.valueOf(userText).intValue());
} else if ( screenId.equals("b") ) {
} else if (screenId.equals("b")) {
// create blocked threads screen
screen = createBlockedThreadsScreen();
} else {
Expand All @@ -251,7 +251,7 @@ private String createTop4JHeader() {
StringBuilder sb = new StringBuilder();
String displayName = displayConfig.getDisplayName();
if (displayName != null && displayName.length() > MAX_DISPLAY_NAME_LENGTH) {
displayName = displayName.substring(0, MAX_DISPLAY_NAME_LENGTH-1);
displayName = displayName.substring(0, MAX_DISPLAY_NAME_LENGTH - 1);
}
sb.append("top4j - " + timeFormat.format(date) + " up " + getUptime() + ", load average: " + osMXBean.getSystemLoadAverage() + "\n");
sb.append("Attached to: " + displayName + " [PID=" + displayConfig.getJvmPid() + "]" + "\n");
Expand Down Expand Up @@ -292,16 +292,16 @@ private String createTopThreadsScreen() {
continue;
}
Long threadId = topThreadMXBean.getThreadId();
String threadState = abbreviateThreadState( threadHelper.getThreadState( threadId ));
String threadState = abbreviateThreadState(threadHelper.getThreadState(threadId));
Double threadCpuUsage = topThreadMXBean.getThreadCpuUsage();
if (threadName != null && threadName.length() > MAX_THREAD_NAME_LENGTH) {
threadName = threadName.substring(0, MAX_THREAD_NAME_LENGTH-1);
threadName = threadName.substring(0, MAX_THREAD_NAME_LENGTH - 1);
}
sb.append( counter + " " +
String.format("%1$-8s", threadId ) +
String.format("%1$-3s", threadState ) +
String.format("%1$-6.1f", threadCpuUsage ) +
String.format("%1$-64s", threadName ) +
sb.append(counter + " " +
String.format("%1$-8s", threadId) +
String.format("%1$-3s", threadState) +
String.format("%1$-6.1f", threadCpuUsage) +
String.format("%1$-64s", threadName) +
"\n");

// store thread Id
Expand Down Expand Up @@ -350,16 +350,16 @@ private String createBlockedThreadsScreen() {
continue;
}
Long threadId = blockedThreadMXBean.getThreadId();
String threadState = abbreviateThreadState( threadHelper.getThreadState( threadId ));
String threadState = abbreviateThreadState(threadHelper.getThreadState(threadId));
Double threadBlockedPercentage = blockedThreadMXBean.getThreadBlockedPercentage();
if (threadName != null && threadName.length() > MAX_THREAD_NAME_LENGTH) {
threadName = threadName.substring(0, MAX_THREAD_NAME_LENGTH - 1);
}
sb.append(counter + " " +
String.format("%1$-8s", threadId ) +
String.format("%1$-3s", threadState ) +
String.format("%1$-10.1f", threadBlockedPercentage ) +
String.format("%1$-50s", threadName ) +
String.format("%1$-8s", threadId) +
String.format("%1$-3s", threadState) +
String.format("%1$-10.1f", threadBlockedPercentage) +
String.format("%1$-50s", threadName) +
"\n");

// store thread Id
Expand All @@ -373,7 +373,8 @@ private String createBlockedThreadsScreen() {
return sb.toString();

}
private String createThreadStackTraceScreen( int threadNumber ) {

private String createThreadStackTraceScreen(int threadNumber) {

Date date = new Date();
StringBuilder sb = new StringBuilder();
Expand All @@ -392,7 +393,7 @@ private String createThreadStackTraceScreen( int threadNumber ) {
return sb.toString();
}

private String getUptime( ) {
private String getUptime() {
Long uptimeSecs = runtimeMXBean.getUptime() / 1000;
String uptime;
if (uptimeSecs > 86400) {
Expand All @@ -403,7 +404,7 @@ private String getUptime( ) {
return uptime;
}

private String abbreviateThreadState( Thread.State state ) {
private String abbreviateThreadState(Thread.State state) {

if (state == null) {
return "X";
Expand Down Expand Up @@ -446,19 +447,17 @@ private boolean isMBSConnectionAlive() {
// use OS MX Bean to retrieve OS name
osMXBean.getName();
isAlive = true;
}
catch (Exception e) {
isAlive = false;
} catch (Exception e) {
isAlive = false;
}
return isAlive;
}

/**
*
* Highlight heading using ANSI escape characters
* Highlight heading using ANSI escape characters
*/

private String highlightHeading( String heading ) {
private String highlightHeading(String heading) {
int consoleWidth = consoleReader.getTerminal().getWidth();
StringBuilder sb = new StringBuilder();
// prepend heading with ANSI escape characters
Expand All @@ -477,19 +476,20 @@ private String highlightHeading( String heading ) {

/**
* Check if Top4J Java Agent is OK via JVM Stats MBean Enabled attribute
*
* @throws ScreenUpdateException If it detects a problem with the Top4J Java Agent
*/
private void checkJavaAgentStatus( ) throws ScreenUpdateException {
private void checkJavaAgentStatus() throws ScreenUpdateException {

// check jvmStats
for (StatsMXBean jvmStats : jvmStatsMBeans) {
// check jvmStats status via MBean Enabled attribute
if (!jvmStats.getEnabled()) {
// extract MBean Object Name from MBean proxy
String jvmStatsToString = jvmStats.toString();
String jvmStatsObjectName = jvmStatsToString.substring(jvmStatsToString.indexOf("[")+1,jvmStatsToString.indexOf("]"));
String jvmStatsObjectName = jvmStatsToString.substring(jvmStatsToString.indexOf("[") + 1, jvmStatsToString.indexOf("]"));
// throw ScreenUpdateException if any of the jvmStats MBeans are disabled
throw new ScreenUpdateException("ERROR: The Top4J Java Agent encountered a problem updating MBean [" + jvmStatsObjectName + "] due to: " + jvmStats.getFailureReason() );
throw new ScreenUpdateException("ERROR: The Top4J Java Agent encountered a problem updating MBean [" + jvmStatsObjectName + "] due to: " + jvmStats.getFailureReason());
}
}
}
Expand Down
42 changes: 20 additions & 22 deletions top4j-cli/src/main/java/io/top4j/cli/Top4J.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class Top4J {

private static final Logger LOGGER = Logger.getLogger(Top4J.class.getName());

public static void main( String[] args ) throws Exception {
public static void main(String[] args) throws Exception {

// run Top4J CLI
System.exit(new Top4J().execute(args));
Expand All @@ -79,8 +79,7 @@ private int execute(String[] args) throws Exception {
JavaProcessManager javaProcessManager;
if (isJava9Plus()) {
javaProcessManager = new Jdk9JavaProcessManager();
}
else {
} else {
ClassLoader classLoader = JConsoleClassLoaderFactory.getClassLoader();
javaProcessManager = new Jdk6JavaProcessManager(classLoader);
}
Expand All @@ -97,25 +96,25 @@ private int execute(String[] args) throws Exception {
CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;
try {
cmd = parser.parse( options, args);
cmd = parser.parse(options, args);
} catch (ParseException e) {
LOGGER.severe("ERROR: There was a problem parsing command-line arguments. Reason: " + e.getMessage() );
LOGGER.severe("ERROR: There was a problem parsing command-line arguments. Reason: " + e.getMessage());
return 1;
}

// interrogate command-line args
if (cmd.hasOption("h")) {
// automatically generate the help statement
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "top4j", options );
formatter.printHelp("top4j", options);
return 0;
}
if (cmd.hasOption("d")) {
// user has provided a screen refresh delay interval via the command-line
String userProvidedDelayInterval = cmd.getOptionValue("d", "3");
if (!isNumeric(userProvidedDelayInterval)) {
// user provided delay interval is *not* a number - return usage message and exit with error code
LOGGER.severe("ERROR: Delay interval provided via command-line argument is not a number: " + userProvidedDelayInterval );
LOGGER.severe("ERROR: Delay interval provided via command-line argument is not a number: " + userProvidedDelayInterval);
return 1;
}
// override default consoleRefreshPeriod
Expand All @@ -138,7 +137,7 @@ private int execute(String[] args) throws Exception {
String userProvidedThreadCacheSize = cmd.getOptionValue("S");
if (!isNumeric(userProvidedThreadCacheSize)) {
// user provided thread usage cache size is *not* a number - return usage message and exit with error code
LOGGER.severe("ERROR: Thread usage cache size provided via command-line argument is not a number: " + userProvidedThreadCacheSize );
LOGGER.severe("ERROR: Thread usage cache size provided via command-line argument is not a number: " + userProvidedThreadCacheSize);
return 1;
}
// override default threadCacheSize
Expand All @@ -149,7 +148,7 @@ private int execute(String[] args) throws Exception {
String userProvidedThreadCacheTTL = cmd.getOptionValue("T");
if (!isNumeric(userProvidedThreadCacheTTL)) {
// user provided thread usage cache TTL is *not* a number - return usage message and exit with error code
LOGGER.severe("ERROR: Thread usage cache TTL provided via command-line argument is not a number: " + userProvidedThreadCacheTTL );
LOGGER.severe("ERROR: Thread usage cache TTL provided via command-line argument is not a number: " + userProvidedThreadCacheTTL);
return 1;
}
// override default threadCacheTTL
Expand All @@ -160,7 +159,7 @@ private int execute(String[] args) throws Exception {
String userProvidedJvmPid = cmd.getOptionValue("p");
if (!isNumeric(userProvidedJvmPid)) {
// user provided JVM PID is *not* a number - return usage message and exit with error code
LOGGER.severe("ERROR: JVM PID provided via command-line argument is not a number: " + userProvidedJvmPid );
LOGGER.severe("ERROR: JVM PID provided via command-line argument is not a number: " + userProvidedJvmPid);
return 1;
}
// user has provided a command-line arg and it's a valid number - use the arg as the jvmPid
Expand Down Expand Up @@ -198,23 +197,22 @@ private int execute(String[] args) throws Exception {
} else {
// use System Console to read single digit character
String input = System.console().readLine();
jvmNumber = Integer.parseInt( input );
jvmNumber = Integer.parseInt(input);
}
System.out.println(jvmNumber);
// validate user input
if (!(jvmNumber >= 0 && jvmNumber <= jvmCount-1)) {
if (!(jvmNumber >= 0 && jvmNumber <= jvmCount - 1)) {
// user has entered an out-of-bounds jvmNumber - return error message and exit with error code
LOGGER.severe("ERROR: Please enter a JVM number between 0 and " + (jvmCount-1));
LOGGER.severe("ERROR: Please enter a JVM number between 0 and " + (jvmCount - 1));
return 1;
}
// set jvmPid according to user selection
jvmPid = jvms.get(jvmNumber).getProcessId();
// set jvmDisplayName according to user selection
jvmDisplayName = jvms.get(jvmNumber).getDisplayName();
}
catch (Exception e) {
} catch (Exception e) {
// user has entered an invalid jvmNumber - return error message and exit with error code
LOGGER.severe("ERROR: Please enter a JVM number between 0 and " + (jvmCount-1));
LOGGER.severe("ERROR: Please enter a JVM number between 0 and " + (jvmCount - 1));
return 1;
}
}
Expand Down Expand Up @@ -254,13 +252,13 @@ private int execute(String[] args) throws Exception {
"top.thread.count=" + displayThreadCount + "," +
"blocked.thread.count=" + displayThreadCount;
// initialise Top4J configurator
Configurator config = new Configurator( mbsc, configOverrides );
Configurator config = new Configurator(mbsc, configOverrides);

// create and start Top4J controller thread
// create and start Top4J controller thread
LOGGER.info("Top4J: Initialising Java agent.");
Controller controller = new Controller( config );
controller.start();
LOGGER.info("Top4J: Java agent activated.");
Controller controller = new Controller(config);
controller.start();
LOGGER.info("Top4J: Java agent activated.");

// create new DisplayConfig to pass to ConsoleController
DisplayConfig displayConfig = new DisplayConfig(displayThreadCount, jvmPid, jvmDisplayName);
Expand All @@ -286,7 +284,7 @@ private int execute(String[] args) throws Exception {
// exit Top4J
return 0;
}
if (Character.isDigit( inputChar )) {
if (Character.isDigit(inputChar)) {
userInput.setIsDigit(true);
} else if (inputText.equals("m")) {
userInput.setIsDigit(false);
Expand Down
Loading

0 comments on commit 1f51b27

Please sign in to comment.