001: /*
002: * MCS Media Computer Software Copyright (c) 2005 by MCS
003: * -------------------------------------- Created on 23.04.2005 by w.klaas
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package de.mcs.jmeasurement;
018:
019: import java.text.SimpleDateFormat;
020: import java.util.Date;
021:
022: import de.mcs.utils.StringFormat;
023:
024: /**
025: * This is the default implementation of the monitor class. Here all measurement
026: * will be done. This monitor is not thread safe. Threadsafty will be
027: * implemented thru the monitorFactory and the MeasurePoint class
028: * implementation.
029: *
030: * @author w.klaas
031: */
032: public class DefaultMonitor implements Monitor {
033:
034: /** time when the monitor starts measurement. */
035: private long startTime;
036:
037: /** time when the monitor starts measurement. */
038: private long pauseTime;
039:
040: /** measurement is actually running. */
041: private boolean running;
042:
043: /** measurement is actually paused. */
044: private boolean paused;
045:
046: /** time elapsed since last measuremtent. */
047: private long accrued;
048:
049: /** the parent MEasurePoint (for call back functions). */
050: private MeasurePoint point;
051:
052: /** id of this monitor. */
053: private String monitorId;
054:
055: /** this monitor has recorded an exception. */
056: private boolean isException;
057:
058: /** the text of the exception. */
059: private String exceptionText;
060:
061: /**
062: * This is the default constructor.
063: */
064: public DefaultMonitor() {
065: reset();
066: point = null;
067: monitorId = "";
068: }
069:
070: /**
071: * This is the default constructor.
072: *
073: * @param aPoint
074: * the measure point (for callback functions)
075: */
076: public DefaultMonitor(final MeasurePoint aPoint) {
077: this ();
078: point = aPoint;
079: }
080:
081: /**
082: * This is the default constructor.
083: *
084: * @param aPoint
085: * the measure point (for callback functions)
086: * @param aMonitorId
087: * id of this monitor
088: */
089: public DefaultMonitor(final MeasurePoint aPoint,
090: final String aMonitorId) {
091: this (aPoint);
092: monitorId = aMonitorId;
093: }
094:
095: /**
096: * This is the default constructor.
097: *
098: * @param aMonitorId
099: * id of this monitor
100: */
101: public DefaultMonitor(final String aMonitorId) {
102: this ();
103: monitorId = aMonitorId;
104: }
105:
106: /**
107: * starting the measurement of this monitor.
108: *
109: * @return boolean <code>true</code> if the monitor could be startet,
110: * otherwise <code>false</code>
111: * @see de.mcs.jmeasurement.Monitor#start()
112: */
113: public final boolean start() {
114: startTime = System.currentTimeMillis();
115: running = true;
116: if (null != point) {
117: point.activateMonitor(this );
118: }
119: return true;
120: }
121:
122: /**
123: * pausing the measurement.
124: *
125: * @return boolean <code>true</code> if the monitor could be paused,
126: * otherwise <code>false</code>
127: * @see de.mcs.jmeasurement.Monitor#pause()
128: */
129: public final boolean pause() {
130: if (running) {
131: pauseTime = System.currentTimeMillis();
132: paused = true;
133: return true;
134: } else {
135: return false;
136: }
137: }
138:
139: /**
140: * resume the measurement.
141: *
142: * @return boolean <code>true</code> if the monitor could be resumed,
143: * otherwise <code>false</code>
144: * @see de.mcs.jmeasurement.Monitor#resume()
145: */
146: public final boolean resume() {
147: if (running) {
148: accrued += pauseTime - startTime;
149: startTime = System.currentTimeMillis();
150: paused = false;
151: return true;
152: } else {
153: return false;
154: }
155: }
156:
157: /**
158: * @see de.mcs.jmeasurement.Monitor#increase(long)
159: * @param msec
160: * time to increase
161: */
162: public final void increase(final long msec) {
163: accrued += msec;
164: }
165:
166: /**
167: * @see de.mcs.jmeasurement.Monitor#decrease(long)
168: * @param msec
169: * time to decrease
170: */
171: public final void decrease(final long msec) {
172: accrued -= msec;
173: }
174:
175: /**
176: * stopping the measurment.
177: *
178: * @return boolean <code>true</code> if the monitor could be stopped,
179: * otherwise <code>false</code>
180: * @see de.mcs.jmeasurement.Monitor#stop()
181: */
182: public final boolean stop() {
183: if (running) {
184: accrued = accrued + timeElapsed();
185: running = false;
186: if (null != point) {
187: point.processMonitor(this );
188: }
189: return true;
190: } else {
191: return false;
192: }
193: }
194:
195: /**
196: * @return long the time elapsed since last call to start() or resume().
197: */
198: private long timeElapsed() {
199: return System.currentTimeMillis() - startTime;
200: }
201:
202: /**
203: * @return long getting the measured time.
204: * @see de.mcs.jmeasurement.Monitor#getAccrued()
205: */
206: public final long getAccrued() {
207: return accrued;
208: }
209:
210: /**
211: * @see de.mcs.jmeasurement.Monitor#reset()
212: */
213: public final void reset() {
214: accrued = 0;
215: running = false;
216: startTime = 0;
217: isException = false;
218: }
219:
220: /**
221: * @return boolean the monitor is actual running
222: * @see de.mcs.jmeasurement.Monitor#isRunning()
223: */
224: public final boolean isRunning() {
225: return running;
226: }
227:
228: /**
229: * @return boolean the monitor is actual paused
230: * @see de.mcs.jmeasurement.Monitor#isPaused()
231: */
232: public final boolean isPaused() {
233: return paused;
234: }
235:
236: /**
237: * This methode will be called if the garbage collector will be remove this
238: * object. So we can use the callback to add a death object if the monitor
239: * is still runnning.
240: *
241: * @throws Throwable
242: * if something goes wrong
243: * @see java.lang.Object#finalize()
244: */
245: protected final void finalize() throws Throwable {
246: super .finalize();
247: if (running) {
248: point.deathMonitor(this );
249: }
250: }
251:
252: /**
253: * @return the id of this monitor
254: * @see de.mcs.jmeasurement.Monitor#getMonitoId()
255: */
256: public final String getMonitoId() {
257: return monitorId;
258: }
259:
260: /**
261: * @return the striong representation of this monitor.
262: * @see java.lang.Object#toString()
263: */
264: public final String toString() {
265: return getMonitoId() + ":" + getAccrued();
266: }
267:
268: /**
269: * @return if this monitor has get an exception.
270: * @see de.mcs.jmeasurement.Monitor#hasException()
271: * @since 0.64
272: */
273: public final boolean hasException() {
274: return isException;
275: }
276:
277: /**
278: * @return the exception text.
279: * @see de.mcs.jmeasurement.Monitor#getException()
280: * @since 0.64
281: */
282: public final String getException() {
283: return exceptionText;
284: }
285:
286: /**
287: * @param text
288: * the text to set for the exception.
289: * @see de.mcs.jmeasurement.Monitor#setException(String)
290: * @since 0.64
291: */
292: public final void setException(final String text) {
293: isException = true;
294: exceptionText = text;
295: stop();
296: }
297:
298: /**
299: * @param throwable
300: * the exception to set.
301: * @see de.mcs.jmeasurement.Monitor#setException(String)
302: * @since 0.64
303: */
304: public final void setException(final Throwable throwable) {
305: isException = true;
306: SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
307: switch (MeasureFactory.getExceptionHandling()) {
308: case JMConfig.EXCEPTION_COUNT:
309: exceptionText = "";
310: break;
311: case JMConfig.EXCEPTION_NAME:
312: exceptionText = simpleDateFormat.format(new Date()) + ':'
313: + throwable.toString();
314: break;
315: case JMConfig.EXCEPTION_TRACE:
316: exceptionText = simpleDateFormat.format(new Date()) + ':'
317: + StringFormat.getStackTrace(throwable);
318: break;
319:
320: default:
321: exceptionText = "";
322: break;
323: }
324: ;
325: stop();
326: }
327: }
|