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: package com.sun.org.glassfish.external.statistics.impl; aoqi@0: import com.sun.org.glassfish.external.statistics.Statistic; aoqi@0: import java.lang.reflect.Method; aoqi@0: import java.lang.reflect.Modifier; aoqi@0: import java.util.Map; aoqi@0: import java.util.concurrent.ConcurrentHashMap; aoqi@0: aoqi@0: /** aoqi@0: * @author Sreenivas Munnangi aoqi@0: */ aoqi@0: public abstract class StatisticImpl implements Statistic { aoqi@0: aoqi@0: private final String statisticName; aoqi@0: private final String statisticUnit; aoqi@0: private final String statisticDesc; aoqi@0: protected long sampleTime = -1L; aoqi@0: private long startTime; aoqi@0: public static final String UNIT_COUNT = "count"; aoqi@0: public static final String UNIT_SECOND = "second"; aoqi@0: public static final String UNIT_MILLISECOND = "millisecond"; aoqi@0: public static final String UNIT_MICROSECOND = "microsecond"; aoqi@0: public static final String UNIT_NANOSECOND = "nanosecond"; aoqi@0: public static final String START_TIME = "starttime"; aoqi@0: public static final String LAST_SAMPLE_TIME = "lastsampletime"; aoqi@0: aoqi@0: protected final Map statMap = new ConcurrentHashMap (); aoqi@0: aoqi@0: protected static final String NEWLINE = System.getProperty( "line.separator" ); aoqi@0: aoqi@0: protected StatisticImpl(String name, String unit, String desc, aoqi@0: long start_time, long sample_time) { aoqi@0: aoqi@0: if (isValidString(name)) { aoqi@0: statisticName = name; aoqi@0: } else { aoqi@0: statisticName = "name"; aoqi@0: } aoqi@0: aoqi@0: if (isValidString(unit)) { aoqi@0: statisticUnit = unit; aoqi@0: } else { aoqi@0: statisticUnit = "unit"; aoqi@0: } aoqi@0: aoqi@0: if (isValidString(desc)) { aoqi@0: statisticDesc = desc; aoqi@0: } else { aoqi@0: statisticDesc = "description"; aoqi@0: } aoqi@0: aoqi@0: startTime = start_time; aoqi@0: sampleTime = sample_time; aoqi@0: } aoqi@0: aoqi@0: protected StatisticImpl(String name, String unit, String desc) { aoqi@0: this(name, unit, desc, System.currentTimeMillis(), System.currentTimeMillis()); aoqi@0: } aoqi@0: aoqi@0: public synchronized Map getStaticAsMap() { aoqi@0: if (isValidString(statisticName)) { aoqi@0: statMap.put("name", statisticName); aoqi@0: } aoqi@0: if (isValidString(statisticUnit)) { aoqi@0: statMap.put("unit", statisticUnit); aoqi@0: } aoqi@0: if (isValidString(statisticDesc)) { aoqi@0: statMap.put("description", statisticDesc); aoqi@0: } aoqi@0: statMap.put(StatisticImpl.START_TIME, startTime); aoqi@0: statMap.put(StatisticImpl.LAST_SAMPLE_TIME, sampleTime); aoqi@0: return statMap; aoqi@0: } aoqi@0: aoqi@0: public String getName() { aoqi@0: return this.statisticName; aoqi@0: } aoqi@0: aoqi@0: public String getDescription() { aoqi@0: return this.statisticDesc; aoqi@0: } aoqi@0: aoqi@0: public String getUnit() { aoqi@0: return this.statisticUnit; aoqi@0: } aoqi@0: aoqi@0: public synchronized long getLastSampleTime() { aoqi@0: return sampleTime; aoqi@0: } aoqi@0: aoqi@0: public synchronized long getStartTime() { aoqi@0: return startTime; aoqi@0: } aoqi@0: aoqi@0: public synchronized void reset() { aoqi@0: startTime = System.currentTimeMillis(); aoqi@0: } aoqi@0: aoqi@0: public synchronized String toString() { aoqi@0: return "Statistic " + getClass().getName() + NEWLINE + aoqi@0: "Name: " + getName() + NEWLINE + aoqi@0: "Description: " + getDescription() + NEWLINE + aoqi@0: "Unit: " + getUnit() + NEWLINE + aoqi@0: "LastSampleTime: " + getLastSampleTime() + NEWLINE + aoqi@0: "StartTime: " + getStartTime(); aoqi@0: } aoqi@0: aoqi@0: protected static boolean isValidString(String str) { aoqi@0: return (str!=null && str.length()>0); aoqi@0: } aoqi@0: aoqi@0: protected void checkMethod(Method method) { aoqi@0: if (method == null || method.getDeclaringClass() == null aoqi@0: || !Statistic.class.isAssignableFrom(method.getDeclaringClass()) aoqi@0: || Modifier.isStatic(method.getModifiers())) { aoqi@0: throw new RuntimeException("Invalid method on invoke"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: }