KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > stats > JVMStatsImpl


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 /*
25  * $Id: JVMStatsImpl.java,v 1.3 2005/12/25 04:16:39 tcfujii Exp $
26  * $Date: 2005/12/25 04:16:39 $
27  * $Revision: 1.3 $
28  */

29
30 package com.sun.enterprise.server.stats;
31 import javax.management.j2ee.statistics.Statistic JavaDoc;
32 import javax.management.j2ee.statistics.BoundedRangeStatistic JavaDoc;
33 import javax.management.j2ee.statistics.CountStatistic JavaDoc;
34 import javax.management.j2ee.statistics.JVMStats JavaDoc;
35 import com.sun.enterprise.admin.monitor.stats.GenericStatsImpl;
36 import com.sun.enterprise.admin.monitor.stats.CountStatisticImpl;
37 import com.sun.enterprise.admin.monitor.stats.BoundedRangeStatisticImpl;
38 import com.sun.enterprise.admin.monitor.stats.MutableStatistic;
39 import com.sun.enterprise.admin.monitor.stats.MutableCountStatistic;
40 import com.sun.enterprise.admin.monitor.stats.MutableCountStatisticImpl;
41 import com.sun.enterprise.admin.monitor.stats.MutableBoundedRangeStatisticImpl;
42 import com.sun.enterprise.util.i18n.StringManager;
43
44
45 /**
46  * Provides an implementation of the JVMStats interface, defined as part
47  * of JSR77.
48  * It specifies the monitoring statistics for a JVM
49  */

50
51 public class JVMStatsImpl implements JVMStats JavaDoc {
52     
53     private final long initTime;
54     private GenericStatsImpl baseStatsImpl;
55     private MutableCountStatistic uptime;
56     private MutableBoundedRangeStatisticImpl heapSize;
57     private static final String JavaDoc STATS_INTERFACE_NAME =
58                         "javax.management.j2ee.statistics.JVMStats";
59     private static StringManager sm =
60                 StringManager.getManager(JVMStatsImpl.class);
61
62     /**
63      * Constructor for JVMStatsImpl
64      * In addition to initializing the MutableStatistic objects, we also
65      * instantiate a GenericStatsImpl object here. All requests for
66      * getStatistics(), getStatistic() & getStatisticNames() are
67      * delegated to this instance of GenericStatsImpl.
68      */

69     public JVMStatsImpl() {
70         
71         initTime = System.currentTimeMillis();
72          try {
73             baseStatsImpl = new GenericStatsImpl(STATS_INTERFACE_NAME, this);
74         }catch(ClassNotFoundException JavaDoc cnfe){
75             // TODO: Handle ClassNotFoundException
76
}
77         
78         // Initialize a MutableCountStatistic
79
CountStatistic JavaDoc u = new CountStatisticImpl(
80                     sm.getString("jvmstats.jvm_uptime"),
81                     sm.getString("jvmstats.milli_seconds"),
82                     sm.getString("jvmstats.jvm_uptime_desc") );
83         uptime = new MutableCountStatisticImpl(u);
84         
85         
86         // Initialize a MutableBoundedRangeStatistic
87
long upper = Runtime.getRuntime().maxMemory();
88         BoundedRangeStatistic JavaDoc h = new BoundedRangeStatisticImpl(
89                     sm.getString("jvmstats.jvm_heapsize"),
90                     sm.getString("jvmstats.bytes"),
91                     sm.getString("jvmstats.jvm_heapsize_desc"),
92                     0, upper, 0);
93         
94         heapSize = new MutableBoundedRangeStatisticImpl(h);
95     }
96     
97     /**
98      * Method to query the size of the JVM's heap
99      * @return BoundedRangeStatistic
100      */

101     public BoundedRangeStatistic JavaDoc getHeapSize() {
102         long heap = Runtime.getRuntime().totalMemory();
103         heapSize.setCount(heap);
104         return (BoundedRangeStatistic JavaDoc) heapSize.unmodifiableView();
105     }
106     
107     /**
108      * Method to query the amount of time that the JVM has been running
109      * @return CountStatistic
110      */

111     public CountStatistic JavaDoc getUpTime() {
112         long curTime = System.currentTimeMillis();
113         long upTime = curTime - initTime;
114         uptime.setCount(upTime);
115         return (CountStatistic JavaDoc) uptime.unmodifiableView();
116     }
117     
118     /**
119      * This method can be used to retrieve all the Statistics, exposed
120      * by this implementation of Stats
121      * @return Statistic[]
122      */

123     public Statistic JavaDoc[] getStatistics() {
124         return baseStatsImpl.getStatistics();
125     }
126     
127     /**
128      * queries for a Statistic by name.
129      * @return Statistic
130      */

131     public Statistic JavaDoc getStatistic(String JavaDoc str) {
132         return baseStatsImpl.getStatistic(str);
133     }
134     
135     /**
136      * returns an array of names of all the Statistics, that can be
137      * retrieved from this implementation of Stats
138      * @return String[]
139      */

140     public String JavaDoc[] getStatisticNames() {
141         return baseStatsImpl.getStatisticNames();
142     }
143     
144 }
145
Popular Tags