aoqi@0: /* aoqi@0: * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: aoqi@0: aoqi@0: package com.sun.org.glassfish.external.statistics.impl; aoqi@0: import com.sun.org.glassfish.external.statistics.RangeStatistic; aoqi@0: import java.util.Map; aoqi@0: import java.lang.reflect.*; aoqi@0: aoqi@0: /** aoqi@0: * @author Sreenivas Munnangi aoqi@0: */ aoqi@0: public final class RangeStatisticImpl extends StatisticImpl aoqi@0: implements RangeStatistic, InvocationHandler { aoqi@0: aoqi@0: private long currentVal = 0L; aoqi@0: private long highWaterMark = Long.MIN_VALUE; aoqi@0: private long lowWaterMark = Long.MAX_VALUE; aoqi@0: private final long initCurrentVal; aoqi@0: private final long initHighWaterMark; aoqi@0: private final long initLowWaterMark; aoqi@0: aoqi@0: private final RangeStatistic rs = aoqi@0: (RangeStatistic) Proxy.newProxyInstance( aoqi@0: RangeStatistic.class.getClassLoader(), aoqi@0: new Class[] { RangeStatistic.class }, aoqi@0: this); aoqi@0: aoqi@0: public RangeStatisticImpl(long curVal, long highMark, long lowMark, aoqi@0: String name, String unit, String desc, aoqi@0: long startTime, long sampleTime) { aoqi@0: super(name, unit, desc, startTime, sampleTime); aoqi@0: currentVal = curVal; aoqi@0: initCurrentVal = curVal; aoqi@0: highWaterMark = highMark; aoqi@0: initHighWaterMark = highMark; aoqi@0: lowWaterMark = lowMark; aoqi@0: initLowWaterMark = lowMark; aoqi@0: } aoqi@0: aoqi@0: public synchronized RangeStatistic getStatistic() { aoqi@0: return rs; aoqi@0: } aoqi@0: aoqi@0: public synchronized Map getStaticAsMap() { aoqi@0: Map m = super.getStaticAsMap(); aoqi@0: m.put("current", getCurrent()); aoqi@0: m.put("lowwatermark", getLowWaterMark()); aoqi@0: m.put("highwatermark", getHighWaterMark()); aoqi@0: return m; aoqi@0: } aoqi@0: aoqi@0: public synchronized long getCurrent() { aoqi@0: return currentVal; aoqi@0: } aoqi@0: aoqi@0: public synchronized void setCurrent(long curVal) { aoqi@0: currentVal = curVal; aoqi@0: lowWaterMark = (curVal >= lowWaterMark ? lowWaterMark : curVal); aoqi@0: highWaterMark = (curVal >= highWaterMark ? curVal : highWaterMark); aoqi@0: sampleTime = System.currentTimeMillis(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the highest value of this statistic, since measurement started. aoqi@0: */ aoqi@0: public synchronized long getHighWaterMark() { aoqi@0: return highWaterMark; aoqi@0: } aoqi@0: aoqi@0: public synchronized void setHighWaterMark(long hwm) { aoqi@0: highWaterMark = hwm; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the lowest value of this statistic, since measurement started. aoqi@0: */ aoqi@0: public synchronized long getLowWaterMark() { aoqi@0: return lowWaterMark; aoqi@0: } aoqi@0: aoqi@0: public synchronized void setLowWaterMark(long lwm) { aoqi@0: lowWaterMark = lwm; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public synchronized void reset() { aoqi@0: super.reset(); aoqi@0: currentVal = initCurrentVal; aoqi@0: highWaterMark = initHighWaterMark; aoqi@0: lowWaterMark = initLowWaterMark; aoqi@0: sampleTime = -1L; aoqi@0: } aoqi@0: aoqi@0: public synchronized String toString() { aoqi@0: return super.toString() + NEWLINE + aoqi@0: "Current: " + getCurrent() + NEWLINE + aoqi@0: "LowWaterMark: " + getLowWaterMark() + NEWLINE + aoqi@0: "HighWaterMark: " + getHighWaterMark(); aoqi@0: } aoqi@0: aoqi@0: // todo: equals implementation aoqi@0: public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { aoqi@0: checkMethod(m); aoqi@0: aoqi@0: Object result; aoqi@0: try { aoqi@0: result = m.invoke(this, args); aoqi@0: } catch (InvocationTargetException e) { aoqi@0: throw e.getTargetException(); aoqi@0: } catch (Exception e) { aoqi@0: throw new RuntimeException("unexpected invocation exception: " + aoqi@0: e.getMessage()); aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: }