KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
26 import java.lang.management.ThreadInfo JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.management.MBeanServerFactory JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.j2ee.statistics.Statistic JavaDoc;
31 import javax.management.j2ee.statistics.CountStatistic JavaDoc;
32 import com.sun.enterprise.admin.monitor.stats.JVMThreadInfoStats;
33 import com.sun.enterprise.admin.monitor.stats.StringStatistic;
34 import com.sun.enterprise.admin.monitor.stats.MutableCountStatistic;
35 import com.sun.enterprise.admin.monitor.stats.MutableCountStatisticImpl;
36 import com.sun.enterprise.admin.monitor.stats.GenericStatsImpl;
37 import com.sun.enterprise.admin.monitor.stats.CountStatisticImpl;
38 import com.sun.enterprise.admin.monitor.stats.StringStatisticImpl;
39 import com.sun.enterprise.admin.monitor.stats.StatisticImpl;
40 import com.sun.enterprise.util.i18n.StringManager;
41
42 public class JVMThreadInfoStatsImpl implements JVMThreadInfoStats {
43     
44     private GenericStatsImpl baseStatsImpl;
45     private static final String JavaDoc STATS_INTERFACE_NAME =
46                         "com.sun.enterprise.admin.monitor.stats.JVMThreadInfoStats";
47     private static final StringManager localStrMgr =
48                 StringManager.getManager(JVMThreadStatsImpl.class);
49
50     private ThreadInfo JavaDoc info;
51     private long initTime;
52     MutableCountStatistic blockedCount;
53     MutableCountStatistic blockedTime;
54     MutableCountStatistic lockOwnerId;
55     MutableCountStatistic threadId;
56     MutableCountStatistic waitingCount;
57     MutableCountStatistic waitingTime;
58     static String JavaDoc NEWLINE = "\n";
59     
60
61     /** Creates a new instance of JVMThreadInfoStatsImpl */
62     public JVMThreadInfoStatsImpl(ThreadInfo JavaDoc tInfo) {
63         try {
64             baseStatsImpl = new GenericStatsImpl(STATS_INTERFACE_NAME, this);
65         } catch(Exception JavaDoc e) {
66         }
67         initTime = System.currentTimeMillis();
68         info = tInfo;
69         // initialize all the MutableStatistic Classes
70
initializeStatistics();
71     }
72     
73     public CountStatistic JavaDoc getBlockedCount() {
74         long blockCount = info.getBlockedCount();
75         blockedCount.setCount(blockCount);
76         return (CountStatistic JavaDoc)blockedCount.unmodifiableView ();
77     }
78     
79     public CountStatistic JavaDoc getBlockedTime() {
80         long blockTime = info.getBlockedTime();
81         blockedTime.setCount(blockTime);
82         return (CountStatistic JavaDoc)blockedTime.unmodifiableView ();
83     }
84     
85     public StringStatistic getLockName() {
86              
87         String JavaDoc lockName = info.getLockName();
88         if((lockName == null) || ("".equals(lockName)))
89             lockName = localStrMgr.getString("monitor.stats.no_lock");
90
91         return new StringStatisticImpl(lockName,
92                    localStrMgr.getString("monitor.stats.lock_name"),
93                    localStrMgr.getString("monitor.stats.string_units"),
94                    localStrMgr.getString("monitor.stats.lock_desc"),
95                    initTime,
96                    System.currentTimeMillis());
97
98     }
99     
100     public CountStatistic JavaDoc getLockOwnerId() {
101         long id = info.getLockOwnerId();
102         lockOwnerId.setCount(id);
103         return (CountStatistic JavaDoc)lockOwnerId.unmodifiableView ();
104     }
105     
106     public StringStatistic getLockOwnerName() {
107         String JavaDoc lockOwnerName = info.getLockOwnerName();
108         if((lockOwnerName == null) || ( "".equals(lockOwnerName)))
109             lockOwnerName = localStrMgr.getString("monitor.stats.no_owner");
110
111         return new StringStatisticImpl(lockOwnerName,
112                    localStrMgr.getString("monitor.stats.lock_owner_name"),
113                    localStrMgr.getString("monitor.stats.string_units"),
114                    localStrMgr.getString("monitor.stats.lock_owner_desc"),
115                    initTime,
116                    System.currentTimeMillis());
117     }
118     
119     public StringStatistic getStackTrace() {
120         StackTraceElement JavaDoc[] trace = info.getStackTrace();
121         String JavaDoc traceString = new String JavaDoc();
122         if(trace != null) {
123             for(int i = 0; i < trace.length; i++) {
124                 traceString = traceString.concat(trace[i].toString());
125                 traceString = traceString.concat(NEWLINE);
126             }
127         }
128         return new StringStatisticImpl(traceString,
129                    localStrMgr.getString("monitor.stats.stack_trace_name"),
130                    localStrMgr.getString("monitor.stats.string_units"),
131                    localStrMgr.getString("monitor.stats.stack_trace_desc"),
132                    initTime,
133                    System.currentTimeMillis());
134         
135         
136     }
137     
138     public CountStatistic JavaDoc getThreadId() {
139         long id = info.getThreadId();
140         threadId.setCount(id);
141         return (CountStatistic JavaDoc)threadId.unmodifiableView ();
142     }
143     
144     public StringStatistic getThreadName() {
145      
146         String JavaDoc name = info.getThreadName();
147
148         return new StringStatisticImpl(name,
149                    localStrMgr.getString("monitor.stats.thread_name"),
150                    localStrMgr.getString("monitor.stats.string_units"),
151                    localStrMgr.getString("monitor.stats.thread_name_desc"),
152                    initTime,
153                    System.currentTimeMillis());
154     }
155     
156     public StringStatistic getThreadState() {
157         
158         return new StringStatisticImpl(info.getThreadState().toString(),
159                    localStrMgr.getString("monitor.stats.thread_state"),
160                    localStrMgr.getString("monitor.stats.string_units"),
161                    localStrMgr.getString("monitor.stats.thread_state_desc"),
162                    initTime,
163                    System.currentTimeMillis());
164     }
165     
166     public CountStatistic JavaDoc getWaitedCount() {
167         long waitCount = info.getWaitedCount();
168         waitingCount.setCount(waitCount);
169         return (CountStatistic JavaDoc)waitingCount.unmodifiableView ();
170     }
171     
172     public CountStatistic JavaDoc getWaitedTime() {
173         long waitTime = info.getWaitedTime();
174         waitingTime.setCount(waitTime);
175         return (CountStatistic JavaDoc)waitingTime.unmodifiableView ();
176     }
177     
178     /**
179      * This method can be used to retrieve all the Statistics, exposed
180      * by this implementation of Stats
181      * @return Statistic[]
182      */

183     public Statistic JavaDoc[] getStatistics() {
184         return baseStatsImpl.getStatistics();
185     }
186     
187     /**
188      * queries for a Statistic by name.
189      * @return Statistic
190      */

191     public Statistic JavaDoc getStatistic(String JavaDoc str) {
192         return baseStatsImpl.getStatistic(str);
193     }
194     
195     /**
196      * returns an array of names of all the Statistics, that can be
197      * retrieved from this implementation of Stats
198      * @return String[]
199      */

200     public String JavaDoc[] getStatisticNames() {
201         return baseStatsImpl.getStatisticNames();
202     }
203     
204     private void initializeStatistics() {
205         
206         // Initialize the MutableCountStatistic for BlockedTime
207
CountStatistic JavaDoc c = new CountStatisticImpl(
208             localStrMgr.getString("monitor.stats.blocked_time"),
209             localStrMgr.getString("monitor.stats.milli_sec_units"),
210             localStrMgr.getString("monitor.stats.blocked_time_desc"));
211         blockedTime = new MutableCountStatisticImpl(c);
212         
213         // Initialize the MutableCountStatistic for BlockedCount
214
c = new CountStatisticImpl(
215             localStrMgr.getString("monitor.stats.blocked_count"),
216             StatisticImpl.DEFAULT_UNIT,
217             localStrMgr.getString("monitor.stats.blocked_count_desc"));
218         blockedCount = new MutableCountStatisticImpl(c);
219         
220         // Initialize the MutableCountStatistic for LockOwnerId
221
c = new CountStatisticImpl(
222             localStrMgr.getString("monitor.stats.lock_owner_id"),
223             StatisticImpl.DEFAULT_UNIT,
224             localStrMgr.getString("monitor.stats.lock_owner_id_desc"));
225         blockedCount = new MutableCountStatisticImpl(c);
226         lockOwnerId = new MutableCountStatisticImpl(c);
227         
228         // Initialize the MutableCountStatistic for ThreadId
229
c = new CountStatisticImpl(
230             localStrMgr.getString("monitor.stats.thread_id"),
231             StatisticImpl.DEFAULT_UNIT,
232             localStrMgr.getString("monitor.stats.thread_id_desc"));
233         threadId = new MutableCountStatisticImpl(c);
234         
235         // Initialize the MutableCountStatistic for WaitingCount
236
c = new CountStatisticImpl(
237             localStrMgr.getString("monitor.stats.waiting_count"),
238             StatisticImpl.DEFAULT_UNIT,
239             localStrMgr.getString("monitor.stats.waiting_count_desc"));
240         waitingCount = new MutableCountStatisticImpl(c);
241         
242         // Initialize the MutableCountStatistic for WaitingTime
243
c = new CountStatisticImpl(
244             localStrMgr.getString("monitor.stats.waiting_time"),
245             localStrMgr.getString("monitor.stats.milli_sec_units"),
246             localStrMgr.getString("monitor.stats.waiting_time_desc"));
247         waitingTime = new MutableCountStatisticImpl(c);
248     }
249 }
250
Popular Tags