Skip to content

Commit

Permalink
Added StackTraceUtils (renaming misnamed StackTraceUtilsTest in the p…
Browse files Browse the repository at this point in the history
…rocess)
  • Loading branch information
CodeByDrescher committed Nov 8, 2023
1 parent 2967718 commit f946045
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 58 deletions.
4 changes: 2 additions & 2 deletions vcell-core/src/main/java/cbit/vcell/resource/ErrorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.vcell.util.BeanUtils;
import org.vcell.util.StackTraceUtils;
import org.vcell.util.document.UserLoginInfo;
import org.vcell.util.document.VCellSoftwareVersion;

Expand Down Expand Up @@ -71,7 +71,7 @@ public ErrorReport(String username, String message, String exceptionMessage, Str
public static void sendErrorReport(Throwable exception, String message) throws RuntimeException {
String softwareVersion = PropertyLoader.getRequiredProperty(PropertyLoader.vcellSoftwareVersion);
String exceptionMessage = exception!=null ? exception.getMessage() : "null";
String stackTrace = exception!=null ? BeanUtils.getStackTrace(exception) : "null";
String stackTrace = exception!=null ? StackTraceUtils.getStackTrace(exception) : "null";
String platform = "Running under Java "+(System.getProperty("java.version"))+
", published by "+(System.getProperty("java.vendor"))+", on the "+ (System.getProperty("os.arch"))+" architecture running version "+(System.getProperty("os.version"))+
" of the "+(System.getProperty("os.name"))+" operating system";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.vcell.util.BeanUtils;
import org.vcell.util.StackTraceUtils;

/**
* Start and monitor Monitor library loading in thread, show error message when done
Expand Down Expand Up @@ -47,7 +47,7 @@ public void run() {
}
sb.append(librarySet);
sb.append(": ");
sb.append(BeanUtils.getMessageRecursive(e));
sb.append(StackTraceUtils.getMessageRecursive(e));
sb.append(newline);
if (isGui){
ErrorUtils.sendErrorReport(e, sb.toString());
Expand Down
45 changes: 0 additions & 45 deletions vcell-util/src/main/java/org/vcell/util/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.net.InetSocketAddress;
import java.net.URL;
Expand All @@ -30,7 +28,6 @@
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.net.ssl.HttpsURLConnection;
Expand All @@ -49,7 +46,6 @@
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpVersion;

import edu.uchc.connjur.wb.ExecutionTrace;
/**
* Insert the type's description here.
* Creation date: (8/18/2000 2:29:31 AM)
Expand Down Expand Up @@ -206,47 +202,6 @@ public static boolean triggersPropertyChangeEvent(Object a, Object b) {
return (a == null || !a.equals(b));
}

public static String getStackTrace(Throwable throwable) {
StringWriter out = new StringWriter();
PrintWriter pw = new PrintWriter(out);
Throwable t = throwable;
while (t != null) {
t.printStackTrace(pw);
t = t.getCause( );
}
pw.close();
return out.getBuffer().toString();
}

/**
* @return String for current stack trace
*/
public static String getStackTrace() {
StackTraceElement[] stackTraceArray = Thread.currentThread().getStackTrace();
StringBuilder sb = new StringBuilder();
//0 and 1 are "getStackTrace" -- start with callers invocation
for (int i = 2; i < stackTraceArray.length; i++) {
sb.append(stackTraceArray[i]);
sb.append('\n');
}
return sb.toString();
}
/**
* recursive assemble exception message
* @param throwable exception / error to recursively get the message from
* @return {@link Throwable#getMessage()}, recursively
*/
public static String getMessageRecursive(Throwable throwable) {
StringBuilder rval = new StringBuilder(throwable.getMessage());
Throwable cause = throwable.getCause();
while (cause != null) {
rval.append(" caused by ").append(cause.getMessage());
cause = cause.getCause();
}

return rval.toString();
}


public static void sendSMTP(String smtpHost, int smtpPort,String from, String to,String subject, String content)
throws MessagingException {
Expand Down
48 changes: 48 additions & 0 deletions vcell-util/src/main/java/org/vcell/util/StackTraceUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.vcell.util;

import java.io.PrintWriter;
import java.io.StringWriter;

public class StackTraceUtils {
public static String getStackTrace(Throwable throwable) {
StringWriter out = new StringWriter();
PrintWriter pw = new PrintWriter(out);
Throwable t = throwable;
while (t != null) {
t.printStackTrace(pw);
t = t.getCause( );
}
pw.close();
return out.getBuffer().toString();
}

/**
* @return String for current stack trace
*/
public static String getStackTrace() {
StackTraceElement[] stackTraceArray = Thread.currentThread().getStackTrace();
StringBuilder sb = new StringBuilder();
//0 and 1 are "getStackTrace" -- start with callers invocation
for (int i = 2; i < stackTraceArray.length; i++) {
sb.append(stackTraceArray[i]);
sb.append('\n');
}
return sb.toString();
}

/**
* recursive assemble exception message
* @param throwable exception / error to recursively get the message from
* @return {@link Throwable#getMessage()}, recursively
*/
public static String getMessageRecursive(Throwable throwable) {
StringBuilder rval = new StringBuilder(throwable.getMessage());
Throwable cause = throwable.getCause();
while (cause != null) {
rval.append(" caused by ").append(cause.getMessage());
cause = cause.getCause();
}

return rval.toString();
}
}
6 changes: 1 addition & 5 deletions vcell-util/src/test/java/org/vcell/util/BeanUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.vcell.test.Fast;

import static org.junit.Assert.*;

@Category(Fast.class)
public class BeanUtilsTest {

Expand All @@ -22,7 +18,7 @@ public class BeanUtilsTest {
private void miney() {mo();}

private void mo( ) {
System.out.println(BeanUtils.getStackTrace());
System.out.println(StackTraceUtils.getStackTrace());
}

@Ignore
Expand Down
4 changes: 0 additions & 4 deletions vcell-util/src/test/java/org/vcell/util/StackTraceUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.vcell.util;

public class StackTraceUtilsTest {
}

0 comments on commit f946045

Please sign in to comment.