1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/org/glassfish/external/statistics/impl/StatisticImpl.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,145 @@ 1.4 +/* 1.5 + * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 + 1.30 +package com.sun.org.glassfish.external.statistics.impl; 1.31 +import com.sun.org.glassfish.external.statistics.Statistic; 1.32 +import java.lang.reflect.Method; 1.33 +import java.lang.reflect.Modifier; 1.34 +import java.util.Map; 1.35 +import java.util.concurrent.ConcurrentHashMap; 1.36 + 1.37 +/** 1.38 + * @author Sreenivas Munnangi 1.39 + */ 1.40 +public abstract class StatisticImpl implements Statistic { 1.41 + 1.42 + private final String statisticName; 1.43 + private final String statisticUnit; 1.44 + private final String statisticDesc; 1.45 + protected long sampleTime = -1L; 1.46 + private long startTime; 1.47 + public static final String UNIT_COUNT = "count"; 1.48 + public static final String UNIT_SECOND = "second"; 1.49 + public static final String UNIT_MILLISECOND = "millisecond"; 1.50 + public static final String UNIT_MICROSECOND = "microsecond"; 1.51 + public static final String UNIT_NANOSECOND = "nanosecond"; 1.52 + public static final String START_TIME = "starttime"; 1.53 + public static final String LAST_SAMPLE_TIME = "lastsampletime"; 1.54 + 1.55 + protected final Map<String, Object> statMap = new ConcurrentHashMap<String, Object> (); 1.56 + 1.57 + protected static final String NEWLINE = System.getProperty( "line.separator" ); 1.58 + 1.59 + protected StatisticImpl(String name, String unit, String desc, 1.60 + long start_time, long sample_time) { 1.61 + 1.62 + if (isValidString(name)) { 1.63 + statisticName = name; 1.64 + } else { 1.65 + statisticName = "name"; 1.66 + } 1.67 + 1.68 + if (isValidString(unit)) { 1.69 + statisticUnit = unit; 1.70 + } else { 1.71 + statisticUnit = "unit"; 1.72 + } 1.73 + 1.74 + if (isValidString(desc)) { 1.75 + statisticDesc = desc; 1.76 + } else { 1.77 + statisticDesc = "description"; 1.78 + } 1.79 + 1.80 + startTime = start_time; 1.81 + sampleTime = sample_time; 1.82 + } 1.83 + 1.84 + protected StatisticImpl(String name, String unit, String desc) { 1.85 + this(name, unit, desc, System.currentTimeMillis(), System.currentTimeMillis()); 1.86 + } 1.87 + 1.88 + public synchronized Map getStaticAsMap() { 1.89 + if (isValidString(statisticName)) { 1.90 + statMap.put("name", statisticName); 1.91 + } 1.92 + if (isValidString(statisticUnit)) { 1.93 + statMap.put("unit", statisticUnit); 1.94 + } 1.95 + if (isValidString(statisticDesc)) { 1.96 + statMap.put("description", statisticDesc); 1.97 + } 1.98 + statMap.put(StatisticImpl.START_TIME, startTime); 1.99 + statMap.put(StatisticImpl.LAST_SAMPLE_TIME, sampleTime); 1.100 + return statMap; 1.101 + } 1.102 + 1.103 + public String getName() { 1.104 + return this.statisticName; 1.105 + } 1.106 + 1.107 + public String getDescription() { 1.108 + return this.statisticDesc; 1.109 + } 1.110 + 1.111 + public String getUnit() { 1.112 + return this.statisticUnit; 1.113 + } 1.114 + 1.115 + public synchronized long getLastSampleTime() { 1.116 + return sampleTime; 1.117 + } 1.118 + 1.119 + public synchronized long getStartTime() { 1.120 + return startTime; 1.121 + } 1.122 + 1.123 + public synchronized void reset() { 1.124 + startTime = System.currentTimeMillis(); 1.125 + } 1.126 + 1.127 + public synchronized String toString() { 1.128 + return "Statistic " + getClass().getName() + NEWLINE + 1.129 + "Name: " + getName() + NEWLINE + 1.130 + "Description: " + getDescription() + NEWLINE + 1.131 + "Unit: " + getUnit() + NEWLINE + 1.132 + "LastSampleTime: " + getLastSampleTime() + NEWLINE + 1.133 + "StartTime: " + getStartTime(); 1.134 + } 1.135 + 1.136 + protected static boolean isValidString(String str) { 1.137 + return (str!=null && str.length()>0); 1.138 + } 1.139 + 1.140 + protected void checkMethod(Method method) { 1.141 + if (method == null || method.getDeclaringClass() == null 1.142 + || !Statistic.class.isAssignableFrom(method.getDeclaringClass()) 1.143 + || Modifier.isStatic(method.getModifiers())) { 1.144 + throw new RuntimeException("Invalid method on invoke"); 1.145 + } 1.146 + } 1.147 + 1.148 +}