FileDocCategorySizeDatePackage
CallerInfo.javaAPI DocGlassfish v2 API10688Fri May 04 22:35:54 BST 2007com.sun.enterprise.tools.common.util.diagnostics

CallerInfo

public class CallerInfo extends Object
The class CallerInfo is a simple C-struct type of data structure that contains a caller's class name, line number and method name. It is used by Reporter classes in the util package of iAB
author
Byron Nevins
version
1.1, 05/31/00
see
netscape.blizzard.util.Reporter
since
iAB 6.0

Fields Summary
private String
className
private String
fileName
private String
methodName
private int
lineNumber
private final String
badFormat
private Vector
ignoreVec
private static Vector
globalIgnoreVec
Constructors Summary
public CallerInfo()

		this(null);
	
public CallerInfo(Object[] ignoreUsToo)

		// STUPID PROGRAMMER TRICKS -- create an Exception and make it print a stack trace to an
		// output stream that we can convert to a String -- then look for the first line that does *not* have
		// our classname in it for the needed information...
		// 

		ByteArrayOutputStream	baos		= new ByteArrayOutputStream();
		PrintWriter				pw			= new PrintWriter(baos);
		Throwable				t			= new Throwable();
		String					me			= getClass().getName() + ".";//NOI18N

		ignoreVec.addElement(me);

		if(ignoreUsToo != null)
		{
			for(int i = 0; i < ignoreUsToo.length; i++)
				ignoreVec.addElement(ignoreUsToo[i].getClass().getName() + ".");//NOI18N
		}

		if(globalIgnoreVec.size() > 0)
		{
			for(Enumeration e = globalIgnoreVec.elements(); e.hasMoreElements(); )
				ignoreVec.addElement(e.nextElement().getClass().getName() + ".");//NOI18N
		}
		
		/*
		System.out.println("**** Ignore List Dump...*****");
		for(Enumeration e = ignoreVec.elements(); e.hasMoreElements(); )
		{
			System.out.println(e.nextElement().toString());
		}
		*/

		// create the stack trace
		t.printStackTrace(pw);
		pw.flush();
		
		// parse it...
		StringTokenizer st = new StringTokenizer(baos.toString(), "\n\r\t");//NOI18N

		if(st.countTokens() < 3)
		{
			// minimum:
			// 1. java.lang.Throwable:
			// 2. this method
			// 3. The method from who-knows-where that is calling a Reporter method...
			throw new CallerInfoException("Expected at least 3 lines from the stack dump -- only saw " + st.countTokens() + " lines");//NOI18N
		}
		
		st.nextToken();	// throw away first line ("java.lang.Throwable")//NOI18N
		
		while(st.hasMoreTokens())
		{
			String s = st.nextToken();

			if(!ignoreVec(s))
			{
				parseCallerInfo(s);
				return;
			}
		}
		
		throw new CallerInfoException("Couldn't find a caller method");//NOI18N
	
Methods Summary
public static voidaddToGlobalIgnore(java.lang.Object o)

		// check for dupes...
		for(Enumeration e = globalIgnoreVec.elements(); e.hasMoreElements(); )
			if(o == e.nextElement())
				return;

		globalIgnoreVec.addElement(o);
	
public java.lang.StringgetClassName()

		Assertion.check(className);
		return className;
	
public java.lang.StringgetFileName()

		Assertion.check(fileName != null);
		return fileName;
	
public intgetLineNumber()

		return lineNumber;
	
public java.lang.StringgetMethodName()

		Assertion.check(methodName);
		return methodName;
	
private booleanignoreVec(java.lang.String s)

		Assertion.check(ignoreVec);
		final int size = ignoreVec.size();
		Assertion.check(size > 0);

		for(int i = 0; i < size; i++)
			if(s.indexOf((String)(ignoreVec.elementAt(i))) >= 0)
				return true;

		return false;
	
private java.lang.StringparseAndRemoveLineInfo(java.lang.String s)

		// lineInfo --> debug build:  "(ReporterTester.java:26)"
		// release build:  "(Unknown Source)"
		// I think this is a possibility: (foo.java) or (foo.java:) -- but can't reproduce...
		
		fileName = "";//NOI18N
		lineNumber = -1;
		
		int left	= s.indexOf('(");
		int right	= s.indexOf(')");
		
		if(left < 0 || right < 0 || right <= left)
			throw new CallerInfoException(badFormat + " -- no parenthesis in line:" + s);//NOI18N

		String lineInfo = s.substring(left + 1, right);
		s = s.substring(0, left);

		if(lineInfo.length() <= 0)
			return s;	//()

		if(lineInfo.equals("Unknown Source"))//NOI18N
			return s; 

		int colon = lineInfo.indexOf(':");

		if(colon < 0)
		{
			// (foo.java)
			fileName = lineInfo;
			return s;
		}
		if(colon == lineInfo.length() - 1)
		{
			// (foo.java:)
			fileName = lineInfo.substring(0, colon);
			return s;
		}
		// (foo.java:125)

		fileName = lineInfo.substring(0, colon);
		
		try
		{
			lineNumber = Integer.parseInt(lineInfo.substring(colon + 1));
		}
		catch(NumberFormatException e)
		{
			// it's already set to -1
		}
		
		return s;
	
voidparseCallerInfo(java.lang.String stackDumpLine)

		/* format of line:
		// "at netscape.blizzard.util.ReporterTester.goo(ReporterTester.java:26)"
		// "at netscape.blizzard.util.ReporterTester.goo(ReporterTester.java:)"
		// "at netscape.blizzard.util.ReporterTester.goo(ReporterTester.java)"
		// "at netscape.blizzard.util.ReporterTester.goo(Unknown Source)"*/
 
		if(!stackDumpLine.startsWith("at "))//NOI18N
			throw new CallerInfoException(badFormat + " -- no \"at \" at start of line (" + stackDumpLine +")");//NOI18N

		stackDumpLine = stackDumpLine.substring(3);
		String classInfo = parseAndRemoveLineInfo(stackDumpLine);
		parseClassInfo(classInfo);
	
private voidparseClassInfo(java.lang.String s)

		// format:  netscape.blizzard.foo.goo.aMethod

		if(s.indexOf('.") < 0)
			throw new CallerInfoException(badFormat + " -- no \".\" in the fully-qualified method name");//NOI18N

		if(s.indexOf('.") == 0)
			throw new CallerInfoException(badFormat + " fully-qualified method name starts with a dot");//NOI18N

		int index = s.lastIndexOf('.");

		className	= s.substring(0, index);
		methodName	= s.substring(index + 1);
	
public java.lang.StringtoString()

		Assertion.check(className);
		Assertion.check(methodName);
		Assertion.check(fileName != null);

		StringBuffer sb = new StringBuffer();
		sb.append(className);
		sb.append(".");//NOI18N
		sb.append(methodName);

		if(fileName.length() > 0)
			sb.append("(" + fileName + ":" + lineNumber + ")");//NOI18N
		else
			sb.append("(Unknown Source");//NOI18N

		return sb.toString();
	
public java.lang.StringtoStringDebug()

		Assertion.check(className);
		Assertion.check(methodName);
		Assertion.check(fileName != null);

		StringBuffer sb = new StringBuffer(getClass().getName());
		sb.append("  dump:");//NOI18N
		sb.append("\nClass Name: ");//NOI18N
		sb.append(className);
		sb.append("\nMethod Name: ");//NOI18N
		sb.append(methodName);
		sb.append("\nFile Name: ");//NOI18N

		if(fileName.length() > 0)
		{
			sb.append(fileName);
			sb.append("\nLine Number: ");//NOI18N
			sb.append(lineNumber);
		}
		else
			sb.append("unknown");//NOI18N

		return sb.toString();