|
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 package com.sun.org.glassfish.external.statistics.impl; |
|
28 import com.sun.org.glassfish.external.statistics.Statistic; |
|
29 import java.lang.reflect.Method; |
|
30 import java.lang.reflect.Modifier; |
|
31 import java.util.Map; |
|
32 import java.util.concurrent.ConcurrentHashMap; |
|
33 |
|
34 /** |
|
35 * @author Sreenivas Munnangi |
|
36 */ |
|
37 public abstract class StatisticImpl implements Statistic { |
|
38 |
|
39 private final String statisticName; |
|
40 private final String statisticUnit; |
|
41 private final String statisticDesc; |
|
42 protected long sampleTime = -1L; |
|
43 private long startTime; |
|
44 public static final String UNIT_COUNT = "count"; |
|
45 public static final String UNIT_SECOND = "second"; |
|
46 public static final String UNIT_MILLISECOND = "millisecond"; |
|
47 public static final String UNIT_MICROSECOND = "microsecond"; |
|
48 public static final String UNIT_NANOSECOND = "nanosecond"; |
|
49 public static final String START_TIME = "starttime"; |
|
50 public static final String LAST_SAMPLE_TIME = "lastsampletime"; |
|
51 |
|
52 protected final Map<String, Object> statMap = new ConcurrentHashMap<String, Object> (); |
|
53 |
|
54 protected static final String NEWLINE = System.getProperty( "line.separator" ); |
|
55 |
|
56 protected StatisticImpl(String name, String unit, String desc, |
|
57 long start_time, long sample_time) { |
|
58 |
|
59 if (isValidString(name)) { |
|
60 statisticName = name; |
|
61 } else { |
|
62 statisticName = "name"; |
|
63 } |
|
64 |
|
65 if (isValidString(unit)) { |
|
66 statisticUnit = unit; |
|
67 } else { |
|
68 statisticUnit = "unit"; |
|
69 } |
|
70 |
|
71 if (isValidString(desc)) { |
|
72 statisticDesc = desc; |
|
73 } else { |
|
74 statisticDesc = "description"; |
|
75 } |
|
76 |
|
77 startTime = start_time; |
|
78 sampleTime = sample_time; |
|
79 } |
|
80 |
|
81 protected StatisticImpl(String name, String unit, String desc) { |
|
82 this(name, unit, desc, System.currentTimeMillis(), System.currentTimeMillis()); |
|
83 } |
|
84 |
|
85 public synchronized Map getStaticAsMap() { |
|
86 if (isValidString(statisticName)) { |
|
87 statMap.put("name", statisticName); |
|
88 } |
|
89 if (isValidString(statisticUnit)) { |
|
90 statMap.put("unit", statisticUnit); |
|
91 } |
|
92 if (isValidString(statisticDesc)) { |
|
93 statMap.put("description", statisticDesc); |
|
94 } |
|
95 statMap.put(StatisticImpl.START_TIME, startTime); |
|
96 statMap.put(StatisticImpl.LAST_SAMPLE_TIME, sampleTime); |
|
97 return statMap; |
|
98 } |
|
99 |
|
100 public String getName() { |
|
101 return this.statisticName; |
|
102 } |
|
103 |
|
104 public String getDescription() { |
|
105 return this.statisticDesc; |
|
106 } |
|
107 |
|
108 public String getUnit() { |
|
109 return this.statisticUnit; |
|
110 } |
|
111 |
|
112 public synchronized long getLastSampleTime() { |
|
113 return sampleTime; |
|
114 } |
|
115 |
|
116 public synchronized long getStartTime() { |
|
117 return startTime; |
|
118 } |
|
119 |
|
120 public synchronized void reset() { |
|
121 startTime = System.currentTimeMillis(); |
|
122 } |
|
123 |
|
124 public synchronized String toString() { |
|
125 return "Statistic " + getClass().getName() + NEWLINE + |
|
126 "Name: " + getName() + NEWLINE + |
|
127 "Description: " + getDescription() + NEWLINE + |
|
128 "Unit: " + getUnit() + NEWLINE + |
|
129 "LastSampleTime: " + getLastSampleTime() + NEWLINE + |
|
130 "StartTime: " + getStartTime(); |
|
131 } |
|
132 |
|
133 protected static boolean isValidString(String str) { |
|
134 return (str!=null && str.length()>0); |
|
135 } |
|
136 |
|
137 protected void checkMethod(Method method) { |
|
138 if (method == null || method.getDeclaringClass() == null |
|
139 || !Statistic.class.isAssignableFrom(method.getDeclaringClass()) |
|
140 || Modifier.isStatic(method.getModifiers())) { |
|
141 throw new RuntimeException("Invalid method on invoke"); |
|
142 } |
|
143 } |
|
144 |
|
145 } |