001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of 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,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.activemq.management;
018
019 import javax.management.j2ee.statistics.Statistic;
020
021 /**
022 * Base class for a Statistic implementation
023 *
024 *
025 */
026 public class StatisticImpl implements Statistic, Resettable {
027
028 protected boolean enabled;
029
030 private String name;
031 private String unit;
032 private String description;
033 private long startTime;
034 private long lastSampleTime;
035 private boolean doReset = true;
036
037 public StatisticImpl(String name, String unit, String description) {
038 this.name = name;
039 this.unit = unit;
040 this.description = description;
041 this.startTime = System.currentTimeMillis();
042 this.lastSampleTime = this.startTime;
043 }
044
045 public synchronized void reset() {
046 if(isDoReset()) {
047 this.startTime = System.currentTimeMillis();
048 this.lastSampleTime = this.startTime;
049 }
050 }
051
052 protected synchronized void updateSampleTime() {
053 this.lastSampleTime = System.currentTimeMillis();
054 }
055
056 public synchronized String toString() {
057 StringBuffer buffer = new StringBuffer();
058 buffer.append(name);
059 buffer.append("{");
060 appendFieldDescription(buffer);
061 buffer.append(" }");
062 return buffer.toString();
063 }
064
065 public String getName() {
066 return this.name;
067 }
068
069 public String getUnit() {
070 return this.unit;
071 }
072
073 public String getDescription() {
074 return this.description;
075 }
076
077 public synchronized long getStartTime() {
078 return this.startTime;
079 }
080
081 public synchronized long getLastSampleTime() {
082 return this.lastSampleTime;
083 }
084
085 /**
086 * @return the enabled
087 */
088 public boolean isEnabled() {
089 return this.enabled;
090 }
091
092 /**
093 * @param enabled the enabled to set
094 */
095 public void setEnabled(boolean enabled) {
096 this.enabled = enabled;
097 }
098
099 /**
100 * @return the doReset
101 */
102 public boolean isDoReset() {
103 return this.doReset;
104 }
105
106 /**
107 * @param doReset the doReset to set
108 */
109 public void setDoReset(boolean doReset) {
110 this.doReset = doReset;
111 }
112
113
114 protected synchronized void appendFieldDescription(StringBuffer buffer) {
115 buffer.append(" unit: ");
116 buffer.append(this.unit);
117 buffer.append(" startTime: ");
118 // buffer.append(new Date(startTime));
119 buffer.append(this.startTime);
120 buffer.append(" lastSampleTime: ");
121 // buffer.append(new Date(lastSampleTime));
122 buffer.append(this.lastSampleTime);
123 buffer.append(" description: ");
124 buffer.append(this.description);
125 }
126 }