BigAdmin System Administration Portal
Community-Submitted Tech Tips
Print-friendly VersionPrint-friendly Version
This content is submitted by a BigAdmin user. It has not been reviewed for technical accuracy by Sun Microsystems, though it may have been lightly edited to improve readability. If you find an error or would like to comment on the article, please contact the submitter or use the comment field at the bottom of the article. Community submissions may not follow Sun trademark guidelines. For information on Sun trademarks, please see http://www.sun.com/suntrademarks/.
 
 

Using Java Stack Trace as an Aid During Development Tasks

Thang Minh Le, July 2009

Contents:

Introduction

This tech tip describes how to use Java stack traces as an aid during development tasks. We explore the use of Java stack trace to build the Component Audit Tool, which reports the audit messages generated while executing a software component.

Java stack trace contains information that is very useful in the process of fixing bugs. When your Java application experiences an unexpected behavior that causes a runtime exception, you expect to receive a Java stack trace in the error output stream. When your server crashes, you search for Java stack traces in the Java core file. A Java stack trace is a piece of information that attracts your attention when you are resolving complex problems. However, the use of Java stack trace does not always relate to bugs and defects. Java stack trace is also useful for development tasks, as we will see in this tech tip.

Platforms and Prerequisites

The code for this tip uses method getId() of the Thread class, which was not available until Java Platform, Standard Edition (Java SE) 5. However, this tip is generally applicable for Java 2 Platform, Standard Edition (J2SE) 1.4.2 and later, because the method getStackTrace() of the Exception class has been valid since J2SE 1.4.2. To use this technical tip in a J2SE 1.4.2 environment, developers can devise their own way to create a replacement for the getId() method of the Thread class.

If you are new to using Java stack trace, you can refer to the article An Introduction to Java Stack Traces on java.sun.com.

Creating the Component Audit Tool

What exactly do we mean by Component Audit Tool?

Your task is to build a new component for your application. A component can be anything, such as a business rule module, a workflow engine, a validation framework, and so on. Then, you want to audit your component by building a text report out of all the audit messages during the execution of the component. Component Audit Tool is built to serve this purpose.

For simplicity, we are going to assume an execution of your component per a request occurs within the same Java thread. Then, we expect this tool to capture all audit messages per thread-based execution.

All audit messages of related classes of a component must be in the same audit report. We don't want audit messages of other components to get into this report even though our component is interacting with the other components.

In the object-oriented world, a class is the most specific entity of software. During the design process, software is broken into different components. Each component is built from a list of related classes. We can think of an audit report for a component as a report of all audit messages for the classes related to the component. ComponentAuditTool.java encapsulates this idea by defining its constructor method with a list of class names as its input parameter:

public class ComponentAuditTool
{
private List<String> componentClassNames;

	  private ComponentAuditTool(List<String> classNames){
	    this.componentClassNames = classNames;
	    auditReport = new StringBuffer();
	  }
	}

Then, a ComponentAuditTool instance is uniquely identified by the value stored in its attribute componentClassNames.

To have a single instance of ComponentAuditTool per thread, we declare:

public class ComponentAuditTool
{
	private static final Hashtable<String,
		List<ComponentAuditTool>>
			auditToolTable = new Hashtable<String,
				List<ComponentAuditTool>>();
}

Hashtable auditToolTable is used to store pairs that consist of the current thread ID (as a hash key) and a corresponding list of ComponentAuditTool instances (as hash values). Since a single thread might execute the code of more than one component, we need to associate each current thread ID with a list of ComponentAuditTool instances.

Below are the method APIs of the ComponentAuditTool class:

Method:

In method createComponentAudit(), we are going to either create a new entry (if there is no existing current thread ID in the hashtable) or add a new ComponentAuditTool instance into an existing list associated with the current thread ID in the hash table (if there is no existing one in the list associated with the current thread ID). The method uses the parameter List<Class> componentClasses to define whether a ComponentAuditTool instance already exists. Following this methodology, our ComponentAuditTool instances will be able to log all audit messages per thread and per component.

Getting a component audit report is just a task of searching the hashtable for a hash key that is the current thread ID and using List<Class> componentClasses to retrieve the correct ComponentAuditTool instance as it was created in the createComponentAudit() method. Once a match is found, we return the content of auditReport for the ComponentAuditTool instance.

Method:

Now, we come to the method that uses the information of the Java stack trace.

Method:

Method logAudit() has to know the caller class name so that it can find related ComponentAuditTool instances stored in the hash table. The way to get the caller class name is to do this:

  • First, execute fillInStackTrace() on the dummyException so that the dummyException obtains the current Java stack trace.

  • Second, call getStackTrace() on dummyException to get an array of StackTraceElement[], which contains all current Java stack trace information. We are only interested in the second element in this array since this element has all the information for the caller class. We obtain the class name of the caller class by executing stacks[1].getClassName().

  • Once the caller class name is retrieved, we need to examine the list of ComponentAuditTool instances associated with the current thread ID in the hashtable and get all ComponentAuditTool instances, which have their componentClassNames containing the caller class name. Then, we append the audit message into these ComponentAuditTool objects.

The implementation of ComponentAuditTool.java and the sample code are provided below in the Appendix: Sample Code section. So you can experiment with this tool at your convenience.

Conclusion

We have seen that Java stack trace is not only useful for bug-related issues but also for the development process. In this technical tip, we looked into the use of the second element (stacks[1]) of the Java stack trace (StackTraceElement[] stacks). There is still a lot of information captured in the Java stack trace that was not discussed. I hope that this is a guide to help you further explore the use of Java stack trace in your daily development tasks.

Appendix: Sample Code

To build this tool, copy each of the following sections into a .java file. You will need these six files to build the tool:
  • ComponentAuditTool.java
  • ComponentAuditToolTest.java
  • ComponentClassA.java
  • ComponentClassB.java
  • ComponentClassC.java
  • ComponentClassD.java

Here are the contents of the ComponentAuditTool.java file:

Here are the contents of the ComponentAuditToolTest.java file:

Here are the contents of the ComponentClassA.java file:

Here are the contents of the ComponentClassB.java file:

Here are the contents of the ComponentClassC.java file:

Here are the contents of the ComponentClassD.java file:

 

About the Author

Thang Minh Le has been working with Java technology for more than five years. He is currently working as a solution developer at The Economical Insurance Group, an insurance company located in Waterloo, Ontario. He can be contacted at thangmle [at] gmail.com.

The information and links on this page have been provided by a BigAdmin user. The submitter is solely responsible for such information and links. Sun is not responsible for the availability of external sites or resources, and does not endorse and is not responsible or liable for any content, advertising, products, or other materials on or available from such sites or resources. Sun will not be responsible or liable, directly or indirectly, for any actual or alleged damage or loss caused by or in connection with use of or reliance on the information posted here, or goods or services available on or through any external site or resource.
 
 

Comments (latest comments first)

Discuss and comment on this resource in the BigAdmin Wiki

Unless otherwise licensed, code in all technical manuals herein (including articles, FAQs, samples) is provided under this License.


BigAdmin
  
 
BigAdmin Upgrade Hub