All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.ms.log4jdbc.utils.Log4JdbcStackTrace Maven / Gradle / Ivy

/*
 * This file is part of Log4Jdbc.
 *
 * Log4Jdbc 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 3 of the License, or
 * (at your option) any later version.
 *
 * Log4Jdbc 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 Log4Jdbc.  If not, see .
 *
 */
package fr.ms.log4jdbc.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import fr.ms.log4jdbc.operator.ResultSetOperationInvocationHandler;

/**
 *
 * @see Marco4J
 *
 *
 * @author Marco Semiao
 *
 */
public final class Log4JdbcStackTrace {

    private final static String CLASS = ResultSetOperationInvocationHandler.class.getName();

    private final static Log4JdbcProperties props = Log4JdbcProperties.getInstance();

    private final static String nl = System.getProperty("line.separator");

    public static String getStackTraceFilter(final StackTraceElement[] stackTrace) {
	final StringBuilder sb = new StringBuilder();
	if (props.logStackTrace()) {

	    final List logStackTraceStartPackages = logStackTraceStartPackages();
	    StackTraceElement[] stackTraceDump;
	    if (logStackTraceStartPackages.isEmpty()) {
		stackTraceDump = Log4JdbcStackTrace.getStackTrace(stackTrace, false);
	    } else {
		stackTraceDump = Log4JdbcStackTrace.getStackTrace(stackTrace, logStackTraceStartPackages);
	    }
	    for (final StackTraceElement stackTraceElement : stackTraceDump) {
		sb.append(stackTraceElement);
		sb.append(nl);
	    }
	}
	return sb.toString();
    }

    private static List logStackTraceStartPackages() {
	final String property = props.getStacktraceStartPackages();
	if (property == null || property.isEmpty()) {
	    return new ArrayList();
	}
	final String[] split = property.split(",");
	final List asList = Arrays.asList(split);
	return asList;
    }

    public static StackTraceElement[] getStackTrace(final List packages) {
	final StackTraceElement[] stackTraceDump = getStackTrace(false);

	final List stackTracePackage = new ArrayList();

	for (final StackTraceElement stackTraceElement : stackTraceDump) {
	    final String className = stackTraceElement.getClassName();
	    for (final String p : packages) {
		final boolean matches = className.startsWith(p);
		if (matches) {
		    stackTracePackage.add(stackTraceElement);
		    break;
		}
	    }
	}

	return stackTracePackage.toArray(new StackTraceElement[0]);
    }

    public static StackTraceElement[] getStackTrace(final StackTraceElement[] stackTrace, final List packages) {
	final StackTraceElement[] stackTraceDump = getStackTrace(stackTrace, false);

	final List stackTracePackage = new ArrayList();

	for (final StackTraceElement stackTraceElement : stackTraceDump) {
	    final String className = stackTraceElement.getClassName();
	    for (final String p : packages) {
		final boolean matches = className.startsWith(p.trim());
		if (matches) {
		    stackTracePackage.add(stackTraceElement);
		    break;
		}
	    }
	}

	return stackTracePackage.toArray(new StackTraceElement[0]);
    }

    public static StackTraceElement[] getStackTrace() {
	final Throwable t = new Throwable();
	t.fillInStackTrace();

	final StackTraceElement[] stackTrace = t.getStackTrace();
	return stackTrace;
    }

    public static StackTraceElement[] getStackTrace(final boolean log4jdbcTrace) {
	final StackTraceElement[] stackTrace = getStackTrace();

	final StackTraceElement[] stackTraceDump = getStackTrace(stackTrace, log4jdbcTrace);

	return stackTraceDump;
    }

    public static StackTraceElement[] getStackTrace(final StackTraceElement[] stackTrace, final boolean log4jdbcTrace) {
	if (stackTrace == null || log4jdbcTrace) {
	    return stackTrace;
	}
	int position = 0;
	for (position = 0; position < stackTrace.length; position++) {
	    final String className = stackTrace[position].getClassName();
	    if (CLASS.equals(className)) {
		break;
	    }
	}
	position = position + 2;
	final int stackTraceLength = stackTrace.length - position;

	if (stackTraceLength < 1) {
	    return stackTrace;
	}
	final StackTraceElement[] stackTraceDump = new StackTraceElement[stackTraceLength];

	System.arraycopy(stackTrace, position, stackTraceDump, 0, stackTraceLength);

	return stackTraceDump;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy