KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > stats > spi > JVMThreadStatsImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.monitor.stats.spi;
25 import java.lang.management.ManagementFactory JavaDoc;
26 import java.lang.management.ThreadMXBean JavaDoc;
27 import javax.management.j2ee.statistics.Statistic JavaDoc;
28 import javax.management.j2ee.statistics.CountStatistic JavaDoc;
29 import com.sun.enterprise.admin.monitor.stats.JVMThreadStats;
30 import com.sun.enterprise.admin.monitor.stats.MutableCountStatistic;
31 import com.sun.enterprise.admin.monitor.stats.MutableCountStatisticImpl;
32 import com.sun.enterprise.admin.monitor.stats.GenericStatsImpl;
33 import com.sun.enterprise.admin.monitor.stats.CountStatisticImpl;
34 import com.sun.enterprise.admin.monitor.stats.StringStatistic;
35 import com.sun.enterprise.admin.monitor.stats.StringStatisticImpl;
36 import com.sun.enterprise.admin.monitor.stats.StatisticImpl;
37 import com.sun.enterprise.util.i18n.StringManager;
38
39
40 public class JVMThreadStatsImpl implements JVMThreadStats {
41     
42     private GenericStatsImpl baseStatsImpl;
43     private static final String JavaDoc STATS_INTERFACE_NAME =
44                         "com.sun.enterprise.admin.monitor.stats.JVMThreadStats";
45     private ThreadMXBean JavaDoc bean;
46     private MutableCountStatistic curThreadCpuTime;
47     private MutableCountStatistic daemonThreadCount;
48     private MutableCountStatistic peakThreadCount;
49     private MutableCountStatistic threadCount;
50     private MutableCountStatistic totalStartedThreadCount;
51     final long initTime;
52     static String JavaDoc DELIMITER = ",";
53     
54
55     private static final StringManager localStrMgr =
56                 StringManager.getManager(JVMThreadStatsImpl.class);
57
58     /** Creates a new instance of JVMThreadStatsImpl */
59     public JVMThreadStatsImpl() {
60         initTime = System.currentTimeMillis ();
61         try {
62             baseStatsImpl = new GenericStatsImpl(STATS_INTERFACE_NAME, this);
63         } catch(Exception JavaDoc e) {
64             
65         }
66         
67         bean = ManagementFactory.getThreadMXBean();
68         // initialize all the MutableStatistic Classes
69
initializeStatistics();
70     }
71     
72     public StringStatistic getAllThreadIds() {
73         long[] ids = bean.getAllThreadIds();
74         String JavaDoc idString = new String JavaDoc();
75         for(int i = 0; i < ids.length; i++)
76         {
77             idString = idString.concat(String.valueOf(ids[i]));
78             idString = idString.concat(DELIMITER);
79         }
80
81         return new StringStatisticImpl(idString,
82                    localStrMgr.getString("monitor.stats.live_threads"),
83                    localStrMgr.getString("monitor.stats.string_units"),
84                    localStrMgr.getString("monitor.stats.live_threads_desc"),
85                    initTime,
86                    System.currentTimeMillis());
87     }
88     
89     public CountStatistic JavaDoc getCurrentThreadCPUTime() {
90         curThreadCpuTime.setCount(bean.getCurrentThreadCpuTime());
91         return (CountStatistic JavaDoc) curThreadCpuTime.unmodifiableView ();
92     }
93     
94     public CountStatistic JavaDoc getDaemonThreadCount() {
95         daemonThreadCount.setCount(bean.getDaemonThreadCount());
96         return (CountStatistic JavaDoc) daemonThreadCount.unmodifiableView();
97     }
98     
99     public StringStatistic getMonitorDeadlockedThreads() {
100         long[] ids = bean.findMonitorDeadlockedThreads();
101         String JavaDoc idString = new String JavaDoc();
102         if(ids != null) {
103             for(int i = 0; i < ids.length; i++) {
104                 idString = idString.concat(String.valueOf(ids[i]));
105                 idString = idString.concat(DELIMITER);
106             }
107         }
108         if((ids == null) || (ids.length == 0)) {
109             idString = idString.concat(
110                 localStrMgr.getString("monitor.stats.no_thread_deadlock"));
111         }
112         return new StringStatisticImpl(idString,
113                    localStrMgr.getString("monitor.stats.dlocked_threads"),
114                    localStrMgr.getString("monitor.stats.string_units"),
115                    localStrMgr.getString("monitor.stats.dlocked_threads_desc"),
116                    initTime,
117                    System.currentTimeMillis());
118
119     }
120     
121     public CountStatistic JavaDoc getPeakThreadCount() {
122         peakThreadCount.setCount(bean.getPeakThreadCount());
123         return (CountStatistic JavaDoc) peakThreadCount.unmodifiableView();
124     }
125     
126     public CountStatistic JavaDoc getThreadCount() {
127         threadCount.setCount(bean.getThreadCount());
128         return (CountStatistic JavaDoc) threadCount.unmodifiableView();
129     }
130     
131     public CountStatistic JavaDoc getTotalStartedThreadCount() {
132         totalStartedThreadCount.setCount(bean.getTotalStartedThreadCount());
133         return (CountStatistic JavaDoc) totalStartedThreadCount.unmodifiableView();
134     }
135             
136
137     
138     /**
139      * This method can be used to retrieve all the Statistics, exposed
140      * by this implementation of Stats
141      * @return Statistic[]
142      */

143     public Statistic JavaDoc[] getStatistics() {
144         return baseStatsImpl.getStatistics();
145     }
146     
147     /**
148      * queries for a Statistic by name.
149      * @return Statistic
150      */

151     public Statistic JavaDoc getStatistic(String JavaDoc str) {
152         return baseStatsImpl.getStatistic(str);
153     }
154     
155     /**
156      * returns an array of names of all the Statistics, that can be
157      * retrieved from this implementation of Stats
158      * @return String[]
159      */

160     public String JavaDoc[] getStatisticNames() {
161         return baseStatsImpl.getStatisticNames();
162     }
163     
164     private void initializeStatistics() {
165         
166         // Initialize the MutableCountStatistic for CurrentThreadCpuTime
167
CountStatistic JavaDoc c = new CountStatisticImpl(
168                 localStrMgr.getString("thread_cpu_time"),
169                 localStrMgr.getString("monitor.stats.nano_sec_units"),
170                 localStrMgr.getString("thread_cpu_time_desc"));
171         curThreadCpuTime = new MutableCountStatisticImpl(c);
172
173         // Initialize the MutableCountStatistic for DaemonThreadCount
174
c= new CountStatisticImpl(
175             localStrMgr.getString("monitor.stats.daemon_thread_count"),
176             StatisticImpl.DEFAULT_UNIT,
177             localStrMgr.getString("monitor.stats.daemon_thread_count_desc"));
178         daemonThreadCount = new MutableCountStatisticImpl(c);
179
180         // Initialize the MutableCountStatistic for PeakThreadCount
181
c = new CountStatisticImpl(
182             localStrMgr.getString("monitor.stats.peak_thread_count"),
183             StatisticImpl.DEFAULT_UNIT,
184             localStrMgr.getString("monitor.stats.peak_thread_count_desc"));
185         peakThreadCount = new MutableCountStatisticImpl(c);
186         
187         // Initialize the MutableCountStatistic for ThreadCount
188
c = new CountStatisticImpl(
189                 localStrMgr.getString("monitor.stats.thread_count"),
190                 StatisticImpl.DEFAULT_UNIT,
191                 localStrMgr.getString("monitor.stats.thread_count_desc"));
192         threadCount = new MutableCountStatisticImpl(c);
193         
194         // Initialize the MutableCountStatistic for TotalStartedThreadCount
195
c = new CountStatisticImpl(
196             localStrMgr.getString("monitor.stats.started_thread_count"),
197             StatisticImpl.DEFAULT_UNIT,
198             localStrMgr.getString("monitor.stats.started_thread_count_desc"));
199         totalStartedThreadCount = new MutableCountStatisticImpl(c);
200     }
201 }
202
Popular Tags