jp.primecloud.auto.common.interceptor.TraceInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for jp.primecloud.auto.common.interceptor.TraceInterceptor.java

Source

/*
 * Copyright 2014 by SCSK Corporation.
 * 
 * This file is part of PrimeCloud Controller(TM).
 * 
 * PrimeCloud Controller(TM) 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 2 of the License, or
 * (at your option) any later version.
 * 
 * PrimeCloud Controller(TM) 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 PrimeCloud Controller(TM). If not, see <http://www.gnu.org/licenses/>.
 */
package jp.primecloud.auto.common.interceptor;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>
 * ???Class??
 * </p>
 *
 */
public class TraceInterceptor implements MethodInterceptor {

    protected Log log = LogFactory.getLog(TraceInterceptor.class);

    protected ThreadLocal<Integer> indents = new ThreadLocal<Integer>() {
        @Override
        protected Integer initialValue() {
            return 0;
        };
    };

    /**
     * {@inheritDoc}
     */
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        if (!log.isDebugEnabled()) {
            return invocation.proceed();
        }

        // ?
        log.debug(startLog(invocation));

        // ?
        long startTime = System.nanoTime();

        // ?
        indents.set(indents.get() + 1);

        try {
            return invocation.proceed();
        } finally {
            // ?
            indents.set(indents.get() - 1);

            // ?
            long stopTime = System.nanoTime();

            // ?
            log.debug(stopLog(invocation, stopTime - startTime));
        }
    }

    protected String startLog(MethodInvocation invocation) {
        StringBuilder sb = new StringBuilder();

        // 
        for (int i = 0; i < indents.get(); i++) {
            sb.append("\t");
        }

        sb.append(invocation.getThis().getClass().getSimpleName());
        sb.append(".");
        sb.append(invocation.getMethod().getName());
        sb.append(" start.");

        return sb.toString();
    }

    protected String stopLog(MethodInvocation invocation, long time) {
        StringBuilder sb = new StringBuilder();

        // 
        for (int i = 0; i < indents.get(); i++) {
            sb.append("\t");
        }

        sb.append(invocation.getThis().getClass().getSimpleName());
        sb.append(".");
        sb.append(invocation.getMethod().getName());
        sb.append(" stop.\t");
        sb.append(time / 1000000.0).append("[ms]");

        return sb.toString();
    }

}