/*
* $Id: DefaultStopwatchEngine.java,v 1.2 2006/03/06 11:30:53 azzazzel Exp $
*
* Copyright 2006 Commsen International
*
* Licensed under the Common Public License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.opensource.org/licenses/cpl1.0.txt
*
* THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS
* OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
*/
package com.commsen.stopwatch.engines;
import org.apache.log4j.Logger;
import com.commsen.stopwatch.StopwatchEngine;
import com.commsen.stopwatch.StopwatchStorage;
import com.commsen.stopwatch.storages.DefaultHSQLInMemoryStorage;
import com.commsen.stopwatch.storages.StorageManager;
/**
* Default stopwatch engine.
* This engine simply measures how many times measured code was executed and how long it take.
*
* @author Milen Dyankov
*
*/
public class DefaultStopwatchEngine implements StopwatchEngine {
/**
* Logger for this class
*/
private static final Logger log = Logger.getLogger(DefaultStopwatchEngine.class);
private boolean started = false;
private boolean debugEnabled = false;
private StopwatchStorage storage;
private StorageManager storageManager;
private int persistenceMode;
/**
*
*/
public DefaultStopwatchEngine() {
setStorage(new DefaultHSQLInMemoryStorage());
}
/**
*
* @see com.commsen.stopwatch.StopwatchEngine#start()
*/
public synchronized void start() {
// ignore this call if already started
if (started) {
getLogger().warn("Attempt to start engine that is already started!");
return;
}
long t1 = System.currentTimeMillis();
if (getLogger().isInfoEnabled()) {
getLogger().info("Starting engine ... ");
}
if (isDebug()) {
getLogger().debug("engine will now attempt to initialize its storage ... ");
}
storageManager = new StorageManager(getStorage(), getPersistenceMode());
storageManager.start();
if (getLogger().isInfoEnabled()) {
getLogger().info("Engine started in " + (System.currentTimeMillis() - t1) + "ms. ");
}
started = true;
}
/**
*
* @see com.commsen.stopwatch.StopwatchEngine#pause()
*/
public synchronized void pause() {
getStorageManager().pause();
if (isDebug()) getLogger().debug("Engine paused !!!");
}
/**
*
* @see com.commsen.stopwatch.StopwatchEngine#resume()
*/
public synchronized void resume() {
getStorageManager().resume();
if (isDebug()) getLogger().debug("Engine resumed !!!");
}
/**
*
* @see com.commsen.stopwatch.StopwatchEngine#stop()
*/
public synchronized void stop() {
// ignore this call if not started
if (!started) return;
// close the storage manager
getStorageManager().stop();
started = false;
if (getLogger().isInfoEnabled()) getLogger().info("Engine stopped !!!");
}
/**
*
* @see com.commsen.stopwatch.StopwatchEngine#begin(java.lang.String, java.lang.String)
*/
public long begin(String group, String label) {
// first of all get the time
Long timestamp = new Long(System.currentTimeMillis());
return getStorageManager().newRecord(new Object[]{group, label, timestamp});
}
/**
*
* @see com.commsen.stopwatch.StopwatchEngine#end(long)
*/
public void end(long id) {
// first of all get the time
Long timestamp = new Long(System.currentTimeMillis());
getStorageManager().completeRecord(id, new Object[] {timestamp});
}
/**
*
* @see com.commsen.stopwatch.StopwatchEngine#skip(long)
*/
public void skip(long id) {
getStorageManager().removeRecord(id);
}
/**
* @return Returns the debugEnabled.
*/
public boolean isDebugEnabled() {
return debugEnabled;
}
/**
* @see com.commsen.stopwatch.StopwatchEngine#setDebugEnabled(boolean)
*/
public void setDebugEnabled(boolean debugEnabled) {
this.debugEnabled = debugEnabled;
}
protected Logger getLogger () {
return log;
}
protected boolean isDebug () {
return isDebugEnabled() && getLogger().isDebugEnabled();
}
/**
*
* @see com.commsen.stopwatch.StopwatchEngine#getStorage()
*/
public StopwatchStorage getStorage() {
return storage;
}
/**
* @param storage The storage to set.
*/
public void setStorage(StopwatchStorage storage) {
this.storage = storage;
}
public String getStorageClass() {
return storage.getClass().getName();
}
/**
* @return Returns the storageManager.
*/
public StorageManager getStorageManager() {
return storageManager;
}
/**
* @return Returns the persistenceMode.
*/
public int getPersistenceMode() {
return persistenceMode;
}
/**
* @param persistenceMode The persistenceMode to set.
*/
public void setPersistenceMode(int persistenceMode) {
this.persistenceMode = persistenceMode;
}
}