1 /*
   2  * Copyright (c) 1997, 2007, 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 package com.sun.org.glassfish.external.statistics.impl;
  27 import com.sun.org.glassfish.external.statistics.BoundaryStatistic;
  28 import java.util.concurrent.atomic.AtomicLong;
  29 import java.util.Map;
  30 import java.lang.reflect.*;
  31 
  32 /**
  33  * @author Sreenivas Munnangi
  34  */
  35 public final class BoundaryStatisticImpl extends StatisticImpl
  36     implements BoundaryStatistic, InvocationHandler {
  37 
  38     private final long lowerBound;
  39     private final long upperBound;
  40 
  41     private final BoundaryStatistic bs =
  42             (BoundaryStatistic) Proxy.newProxyInstance(
  43             BoundaryStatistic.class.getClassLoader(),
  44             new Class[] { BoundaryStatistic.class },
  45             this);
  46 
  47     public BoundaryStatisticImpl(long lower, long upper, String name,
  48                                  String unit, String desc, long startTime,
  49                                  long sampleTime) {
  50         super(name, unit, desc, startTime, sampleTime);
  51         upperBound = upper;
  52         lowerBound = lower;
  53     }
  54 
  55     public synchronized BoundaryStatistic getStatistic() {
  56         return bs;
  57     }
  58 
  59     public synchronized Map getStaticAsMap() {
  60         Map m = super.getStaticAsMap();
  61         m.put("lowerbound", getLowerBound());
  62         m.put("upperbound", getUpperBound());
  63         return m;
  64     }
  65 
  66     public synchronized long getLowerBound() {
  67         return lowerBound;
  68     }
  69 
  70     public synchronized long getUpperBound() {
  71         return upperBound;
  72     }
  73 
  74     @Override
  75     public synchronized void reset() {
  76         super.reset();
  77         sampleTime = -1L;
  78     }
  79 
  80     // todo: equals implementation
  81     public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
  82         Object result;
  83         try {
  84             result = m.invoke(this, args);
  85         } catch (InvocationTargetException e) {
  86             throw e.getTargetException();
  87         } catch (Exception e) {
  88             throw new RuntimeException("unexpected invocation exception: " +
  89                        e.getMessage());
  90         } finally {
  91         }
  92         return result;
  93     }
  94 }