1 /* 2 * Copyright (c) 2009, 2013, 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 30 import com.sun.org.glassfish.external.statistics.BoundedRangeStatistic; 31 import java.util.Map; 32 import java.lang.reflect.*; 33 34 35 /** 36 * @author Sreenivas Munnangi 37 */ 38 public final class BoundedRangeStatisticImpl extends StatisticImpl 39 implements BoundedRangeStatistic, InvocationHandler { 40 41 private long lowerBound = 0L; 42 private long upperBound = 0L; 43 private long currentVal = 0L; 44 private long highWaterMark = Long.MIN_VALUE; 45 private long lowWaterMark = Long.MAX_VALUE; 46 47 private final long initLowerBound; 48 private final long initUpperBound; 49 private final long initCurrentVal; 50 private final long initHighWaterMark; 51 private final long initLowWaterMark; 52 53 private final BoundedRangeStatistic bs = 54 (BoundedRangeStatistic) Proxy.newProxyInstance( 55 BoundedRangeStatistic.class.getClassLoader(), 56 new Class[] { BoundedRangeStatistic.class }, 57 this); 58 59 public synchronized String toString() { 60 return super.toString() + NEWLINE + 61 "Current: " + getCurrent() + NEWLINE + 62 "LowWaterMark: " + getLowWaterMark() + NEWLINE + 63 "HighWaterMark: " + getHighWaterMark() + NEWLINE + 64 "LowerBound: " + getLowerBound() + NEWLINE + 65 "UpperBound: " + getUpperBound(); 66 } 67 68 69 public BoundedRangeStatisticImpl(long curVal, long highMark, long lowMark, 70 long upper, long lower, String name, 71 String unit, String desc, long startTime, 72 long sampleTime) { 73 super(name, unit, desc, startTime, sampleTime); 74 currentVal = curVal; 75 initCurrentVal = curVal; 76 highWaterMark = highMark; 77 initHighWaterMark = highMark; 78 lowWaterMark = lowMark; 79 initLowWaterMark = lowMark; 80 upperBound = upper; 81 initUpperBound = upper; 82 lowerBound = lower; 83 initLowerBound = lower; 84 } 85 86 public synchronized BoundedRangeStatistic getStatistic() { 87 return bs; 88 } 89 90 public synchronized Map getStaticAsMap() { 91 Map m = super.getStaticAsMap(); 92 m.put("current", getCurrent()); 93 m.put("lowerbound", getLowerBound()); 94 m.put("upperbound", getUpperBound()); 95 m.put("lowwatermark", getLowWaterMark()); 96 m.put("highwatermark", getHighWaterMark()); 97 return m; 98 } 99 100 public synchronized long getCurrent() { 101 return currentVal; 102 } 103 104 public synchronized void setCurrent(long curVal) { 105 currentVal = curVal; 106 lowWaterMark = (curVal >= lowWaterMark ? lowWaterMark : curVal); 107 highWaterMark = (curVal >= highWaterMark ? curVal : highWaterMark); 108 sampleTime = System.currentTimeMillis(); 109 } 110 111 public synchronized long getHighWaterMark() { 112 return highWaterMark; 113 } 114 115 public synchronized void setHighWaterMark(long hwm) { 116 highWaterMark = hwm; 117 } 118 119 public synchronized long getLowWaterMark() { 120 return lowWaterMark; 121 } 122 123 public synchronized void setLowWaterMark(long lwm) { 124 lowWaterMark = lwm; 125 } 126 127 public synchronized long getLowerBound() { 128 return lowerBound; 129 } 130 131 public synchronized long getUpperBound() { 132 return upperBound; 133 } 134 135 @Override 136 public synchronized void reset() { 137 super.reset(); 138 lowerBound = initLowerBound; 139 upperBound = initUpperBound; 140 currentVal = initCurrentVal; 141 highWaterMark = initHighWaterMark; 142 lowWaterMark = initLowWaterMark; 143 sampleTime = -1L; 144 } 145 146 // todo: equals implementation 147 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { 148 checkMethod(m); 149 150 Object result; 151 try { 152 result = m.invoke(this, args); 153 } catch (InvocationTargetException e) { 154 throw e.getTargetException(); 155 } catch (Exception e) { 156 throw new RuntimeException("unexpected invocation exception: " + 157 e.getMessage()); 158 } 159 return result; 160 } 161 }