1 /* 2 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 27 28 package com.sun.org.glassfish.external.statistics.impl; 29 import com.sun.org.glassfish.external.statistics.RangeStatistic; 30 import java.util.concurrent.atomic.AtomicLong; 31 import java.util.Map; 32 import java.lang.reflect.*; 33 34 /** 35 * @author Sreenivas Munnangi 36 */ 37 public final class RangeStatisticImpl extends StatisticImpl 38 implements RangeStatistic, InvocationHandler { 39 40 private long currentVal = 0L; 41 private long highWaterMark = Long.MIN_VALUE; 42 private long lowWaterMark = Long.MAX_VALUE; 43 private final long initCurrentVal; 44 private final long initHighWaterMark; 45 private final long initLowWaterMark; 46 47 private final RangeStatistic rs = 48 (RangeStatistic) Proxy.newProxyInstance( 49 RangeStatistic.class.getClassLoader(), 50 new Class[] { RangeStatistic.class }, 51 this); 52 53 public RangeStatisticImpl(long curVal, long highMark, long lowMark, 54 String name, String unit, String desc, 55 long startTime, long sampleTime) { 56 super(name, unit, desc, startTime, sampleTime); 57 currentVal = curVal; 58 initCurrentVal = curVal; 59 highWaterMark = highMark; 60 initHighWaterMark = highMark; 61 lowWaterMark = lowMark; 62 initLowWaterMark = lowMark; 63 } 64 65 public synchronized RangeStatistic getStatistic() { 66 return rs; 67 } 68 69 public synchronized Map getStaticAsMap() { 70 Map m = super.getStaticAsMap(); 71 m.put("current", getCurrent()); 72 m.put("lowwatermark", getLowWaterMark()); 73 m.put("highwatermark", getHighWaterMark()); 74 return m; 75 } 76 77 public synchronized long getCurrent() { 78 return currentVal; 79 } 80 81 public synchronized void setCurrent(long curVal) { 82 currentVal = curVal; 83 lowWaterMark = (curVal >= lowWaterMark ? lowWaterMark : curVal); 84 highWaterMark = (curVal >= highWaterMark ? curVal : highWaterMark); 85 sampleTime = System.currentTimeMillis(); 86 } 87 88 /** 89 * Returns the highest value of this statistic, since measurement started. 90 */ 91 public synchronized long getHighWaterMark() { 92 return highWaterMark; 93 } 94 95 public synchronized void setHighWaterMark(long hwm) { 96 highWaterMark = hwm; 97 } 98 99 /** 100 * Returns the lowest value of this statistic, since measurement started. 101 */ 102 public synchronized long getLowWaterMark() { 103 return lowWaterMark; 104 } 105 106 public synchronized void setLowWaterMark(long lwm) { 107 lowWaterMark = lwm; 108 } 109 110 @Override 111 public synchronized void reset() { 112 super.reset(); 113 currentVal = initCurrentVal; 114 highWaterMark = initHighWaterMark; 115 lowWaterMark = initLowWaterMark; 116 sampleTime = -1L; 117 } 118 119 public synchronized String toString() { 120 return super.toString() + NEWLINE + 121 "Current: " + getCurrent() + NEWLINE + 122 "LowWaterMark: " + getLowWaterMark() + NEWLINE + 123 "HighWaterMark: " + getHighWaterMark(); 124 } 125 126 // todo: equals implementation 127 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { 128 Object result; 129 try { 130 result = m.invoke(this, args); 131 } catch (InvocationTargetException e) { 132 throw e.getTargetException(); 133 } catch (Exception e) { 134 throw new RuntimeException("unexpected invocation exception: " + 135 e.getMessage()); 136 } finally { 137 } 138 return result; 139 } 140 }