Package org.jboss.profiler.aop.profiler.control

Source Code of org.jboss.profiler.aop.profiler.control.ThreadContext

/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.profiler.aop.profiler.control;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.jboss.profiler.aop.logger.ProfileLogger;
import org.jboss.profiler.aop.util.ProfilerUtil;

/**
* This is a class that will be inserted into the ThreadLocal.
* This class will contains all the currentThreadInformation.
* @author Clebert Suconic
*/
public class ThreadContext {

    static ThreadLocal threadContext = new ThreadLocal();

    public static ThreadContext getThreadContext() {
        if (threadContext.get()==null) {
            try {
                ProfileLogger logger = ProfilerController.getInstance().createLogger();
                ThreadContext context = new ThreadContext(logger);
                threadContext.set(context);
            } catch (IOException e) {
                // TODO: Error handling according JBoss
                e.printStackTrace();
            }
        }
        return (ThreadContext)threadContext.get();
    }


    ProfileLogger logger;
    HashKey key = new HashKey(0);

    private ThreadContext(ProfileLogger logger) throws IOException {
    super();
    this.logger=logger;
    logger.startThread(System.currentTimeMillis(),Thread.currentThread().hashCode(),Thread.currentThread().getName());
  }

    public void enterMethod(Method method) throws IOException {
        verifyClassAndMethod(method);
        logger.enterMethod(System.currentTimeMillis(),Thread.currentThread().hashCode(),method.getDeclaringClass().hashCode(),method.hashCode());
    }

    /**
     * Verify if a class was already loaded into logger
     * @param method
     */
    private void verifyClassAndMethod(Method method) throws IOException {
        key.setHash(method.getDeclaringClass().hashCode());
        if (!ProfilerController.getInstance().verifyLoadedClass(key)) {
            long time = System.currentTimeMillis();
            Class clazz = method.getDeclaringClass();
            logger.loadClass(time,clazz.hashCode(),clazz.getName(),clazz.getDeclaredMethods().length+clazz.getDeclaredConstructors().length);
            Method[] methods = clazz.getDeclaredMethods();
            for (int i=0;i<methods.length;i++) {
                logger.loadMethod(time,clazz.hashCode(),methods[i].hashCode(),
                                  methods[i].getName(),ProfilerUtil.getMethodSignature(method));
            }
            Constructor constructors[] = clazz.getDeclaredConstructors();
            for (int i=0;i<constructors.length;i++) {
                logger.loadMethod(time,clazz.hashCode(),constructors[i].hashCode(),constructors[i].getName(),"()");
            }
        }
    }

    public void exitMethod(Method method) throws IOException {
        logger.exitMethod(System.currentTimeMillis(),Thread.currentThread().hashCode(),method.hashCode());
    }


}
TOP

Related Classes of org.jboss.profiler.aop.profiler.control.ThreadContext

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.