001: /*
002: * $Id: DefaultStopwatchEngine.java,v 1.2 2006/03/06 11:30:53 azzazzel Exp $
003: *
004: * Copyright 2006 Commsen International
005: *
006: * Licensed under the Common Public License, Version 1.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.opensource.org/licenses/cpl1.0.txt
011: *
012: * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013: * EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS
014: * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
015: *
016: */
017: package com.commsen.stopwatch.engines;
018:
019: import org.apache.log4j.Logger;
020:
021: import com.commsen.stopwatch.StopwatchEngine;
022: import com.commsen.stopwatch.StopwatchStorage;
023: import com.commsen.stopwatch.storages.DefaultHSQLInMemoryStorage;
024: import com.commsen.stopwatch.storages.StorageManager;
025:
026: /**
027: * Default stopwatch engine.
028: * This engine simply measures how many times measured code was executed and how long it take.
029: *
030: * @author Milen Dyankov
031: *
032: */
033: public class DefaultStopwatchEngine implements StopwatchEngine {
034:
035: /**
036: * Logger for this class
037: */
038: private static final Logger log = Logger
039: .getLogger(DefaultStopwatchEngine.class);
040:
041: private boolean started = false;
042: private boolean debugEnabled = false;
043: private StopwatchStorage storage;
044: private StorageManager storageManager;
045: private int persistenceMode;
046:
047: /**
048: *
049: */
050: public DefaultStopwatchEngine() {
051: setStorage(new DefaultHSQLInMemoryStorage());
052: }
053:
054: /**
055: *
056: * @see com.commsen.stopwatch.StopwatchEngine#start()
057: */
058: public synchronized void start() {
059:
060: // ignore this call if already started
061: if (started) {
062: getLogger().warn(
063: "Attempt to start engine that is already started!");
064: return;
065: }
066:
067: long t1 = System.currentTimeMillis();
068:
069: if (getLogger().isInfoEnabled()) {
070: getLogger().info("Starting engine ... ");
071: }
072: if (isDebug()) {
073: getLogger()
074: .debug(
075: "engine will now attempt to initialize its storage ... ");
076: }
077:
078: storageManager = new StorageManager(getStorage(),
079: getPersistenceMode());
080: storageManager.start();
081:
082: if (getLogger().isInfoEnabled()) {
083: getLogger().info(
084: "Engine started in "
085: + (System.currentTimeMillis() - t1)
086: + "ms. ");
087: }
088:
089: started = true;
090: }
091:
092: /**
093: *
094: * @see com.commsen.stopwatch.StopwatchEngine#pause()
095: */
096: public synchronized void pause() {
097: getStorageManager().pause();
098: if (isDebug())
099: getLogger().debug("Engine paused !!!");
100: }
101:
102: /**
103: *
104: * @see com.commsen.stopwatch.StopwatchEngine#resume()
105: */
106: public synchronized void resume() {
107: getStorageManager().resume();
108: if (isDebug())
109: getLogger().debug("Engine resumed !!!");
110: }
111:
112: /**
113: *
114: * @see com.commsen.stopwatch.StopwatchEngine#stop()
115: */
116: public synchronized void stop() {
117: // ignore this call if not started
118: if (!started)
119: return;
120:
121: // close the storage manager
122: getStorageManager().stop();
123:
124: started = false;
125: if (getLogger().isInfoEnabled())
126: getLogger().info("Engine stopped !!!");
127: }
128:
129: /**
130: *
131: * @see com.commsen.stopwatch.StopwatchEngine#begin(java.lang.String, java.lang.String)
132: */
133: public long begin(String group, String label) {
134: // first of all get the time
135: Long timestamp = new Long(System.currentTimeMillis());
136: return getStorageManager().newRecord(
137: new Object[] { group, label, timestamp });
138: }
139:
140: /**
141: *
142: * @see com.commsen.stopwatch.StopwatchEngine#end(long)
143: */
144: public void end(long id) {
145: // first of all get the time
146: Long timestamp = new Long(System.currentTimeMillis());
147: getStorageManager().completeRecord(id,
148: new Object[] { timestamp });
149: }
150:
151: /**
152: *
153: * @see com.commsen.stopwatch.StopwatchEngine#skip(long)
154: */
155: public void skip(long id) {
156: getStorageManager().removeRecord(id);
157: }
158:
159: /**
160: * @return Returns the debugEnabled.
161: */
162: public boolean isDebugEnabled() {
163: return debugEnabled;
164: }
165:
166: /**
167: * @see com.commsen.stopwatch.StopwatchEngine#setDebugEnabled(boolean)
168: */
169: public void setDebugEnabled(boolean debugEnabled) {
170: this .debugEnabled = debugEnabled;
171: }
172:
173: protected Logger getLogger() {
174: return log;
175: }
176:
177: protected boolean isDebug() {
178: return isDebugEnabled() && getLogger().isDebugEnabled();
179: }
180:
181: /**
182: *
183: * @see com.commsen.stopwatch.StopwatchEngine#getStorage()
184: */
185: public StopwatchStorage getStorage() {
186: return storage;
187: }
188:
189: /**
190: * @param storage The storage to set.
191: */
192: public void setStorage(StopwatchStorage storage) {
193: this .storage = storage;
194: }
195:
196: public String getStorageClass() {
197: return storage.getClass().getName();
198: }
199:
200: /**
201: * @return Returns the storageManager.
202: */
203: public StorageManager getStorageManager() {
204: return storageManager;
205: }
206:
207: /**
208: * @return Returns the persistenceMode.
209: */
210: public int getPersistenceMode() {
211: return persistenceMode;
212: }
213:
214: /**
215: * @param persistenceMode The persistenceMode to set.
216: */
217: public void setPersistenceMode(int persistenceMode) {
218: this.persistenceMode = persistenceMode;
219: }
220:
221: }
|