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

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

/*
* 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.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashSet;

import org.jboss.profiler.aop.logger.FileProfileLogger;
import org.jboss.profiler.aop.logger.ProfileLogger;

/**
*  Controls the life cycle of a thread and performs the Profiler
*  * @author clebert
*/
public class ProfilerController {
    private String outputLocal = System.getProperty("java.io.tmpdir");
    private String filePrefix;

    boolean started=false;
    boolean initialized=false;

    HashSet loadedClasses = new HashSet();


    private static ProfilerController instance;
    public static ProfilerController getInstance() {
       if (instance==null) {
           synchronized (ProfilerController.class) {
               instance = new ProfilerController();
           }
       }
       return instance;
    }

    private ProfilerController() {
    }

    ProfileLogger mainLog;

    /** If a class is not loaded, returns false and add to return true on next time.
     * So, it will be an obligation to load the class after this operation. */
    public boolean verifyLoadedClass(HashKey key) {
        return verifyHashset(key, loadedClasses);
    }

  /**
     * @param key
     * @param loadedClasses
     * @return
     */
    private boolean verifyHashset(HashKey key, HashSet loadedClasses) {
        if (!loadedClasses.contains(key)) {
            synchronized (loadedClasses) {
                if (!loadedClasses.contains(key)) {
                    loadedClasses.add(new HashKey(key.hashCode()));
                    return false;
                } else {
                    return true;
                }
            }
        } else {
            return true;
        }
    }

    /** Read configuration for output generation for this class.*/
  private void initialize() throws IOException {
      filePrefix = "serverspy";

        String fileName=filePrefix + "_1_main_" + System.currentTimeMillis() + ".log";

        System.out.println(outputLocal + "/" + fileName);

        mainLog = new FileProfileLogger(new FileOutputStream(new File(outputLocal,fileName)));
        mainLog.headerSignature(System.currentTimeMillis());

        initialized=true;
  }


  /** Create the appropriate instance of ProfileLogger based on configurations.
   * TODO: create differente Loggers based on configurations. */
  public ProfileLogger createLogger() throws IOException {

      String fileName=filePrefix + "_1_thread_" + System.currentTimeMillis() + "_" + Thread.currentThread().hashCode() +"_0.log";

      File file = new File (outputLocal,fileName);
      // TODO: Use BufferedOutputStream + GzipOutputStream
      FileOutputStream output = new FileOutputStream(file);
      FileProfileLogger logger = new FileProfileLogger(output);

      return logger;
  }

  public void enterMethod(Method method) throws IOException {
      ThreadContext.getThreadContext().enterMethod(method);
  }

  public void exitMethod(Method method) throws IOException {
      ThreadContext.getThreadContext().exitMethod(method);
  }

    /**
     * The directory where the information will be generated
     * @return Returns the outputLocal.
     */
    public String getOutputLocal() {
        return outputLocal;
    }
    /**
     * The directory where the information will be generated
     * @param outputLocal The outputLocal to set.
     */
    public void setOutputLocal(String outputLocal) {
        this.outputLocal = outputLocal;
    }

    /**
     * The state of the profiler.
     * Data would not be captured if started=false
     * @return Returns the started.
     */
    public boolean isStarted() {
        return started;
    }
    /**
     * The state of the profiler.
     * Data would not be captured if started=false
     * @param started The started to set.
     */
    public void setStarted(boolean started) throws IOException {
        if (!this.started && started) {
            initialize();
        }
        this.started = started;
    }
}
TOP

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

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.