KickJava   Java API By Example, From Geeks To Geeks.

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


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.MemoryMXBean JavaDoc;
27 import java.lang.management.MemoryUsage JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.MBeanServerFactory JavaDoc;
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.j2ee.statistics.Statistic JavaDoc;
32 import javax.management.j2ee.statistics.CountStatistic JavaDoc;
33 import com.sun.enterprise.admin.monitor.stats.MutableCountStatistic;
34 import com.sun.enterprise.admin.monitor.stats.MutableCountStatisticImpl;
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.StatisticImpl;
38 import com.sun.enterprise.admin.monitor.stats.JVMMemoryStats;
39 import com.sun.enterprise.util.i18n.StringManager;
40
41 public class JVMMemoryStatsImpl implements JVMMemoryStats {
42     
43     private GenericStatsImpl baseStatsImpl;
44     private static final String JavaDoc STATS_INTERFACE_NAME =
45                         "com.sun.enterprise.admin.monitor.stats.JVMMemoryStats";
46     private MutableCountStatistic initHeapSize;
47     private MutableCountStatistic usedHeapSize;
48     private MutableCountStatistic maxHeapSize;
49     private MutableCountStatistic commitHeapSize;
50     private MutableCountStatistic initNonHeapSize;
51     private MutableCountStatistic maxNonHeapSize;
52     private MutableCountStatistic usedNonHeapSize;
53     private MutableCountStatistic commitNonHeapSize;
54     private MutableCountStatistic objPendingCount;
55     private MemoryUsage JavaDoc heapUsage;
56     private MemoryUsage JavaDoc nonheapUsage;
57     private MemoryMXBean JavaDoc bean;
58     private static final String JavaDoc BYTE_UNITS = "monitor.stats.byte_units";
59     private static final StringManager localStrMgr =
60                 StringManager.getManager(JVMMemoryStatsImpl.class);
61
62
63     /** Creates a new instance of JVMMemoryStatsImpl */
64     public JVMMemoryStatsImpl() {
65         
66         try {
67             baseStatsImpl = new GenericStatsImpl(STATS_INTERFACE_NAME, this);
68         } catch(Exception JavaDoc e) {
69             
70         }
71         bean = ManagementFactory.getMemoryMXBean();
72         heapUsage = bean.getHeapMemoryUsage();
73         nonheapUsage = bean.getNonHeapMemoryUsage();
74         
75         // initialize all the MutableStatistic Classes
76
initializeStatistics();
77     }
78     
79     
80     private void initializeStatistics() {
81         String JavaDoc bytes_str = localStrMgr.getString(BYTE_UNITS);
82
83         // Initialize the MutableCountStatistic for InitialHeapSize
84
CountStatistic JavaDoc c = new CountStatisticImpl(
85             localStrMgr.getString("monitor.stats.initial_heap_size"), bytes_str,
86             localStrMgr.getString("monitor.stats.initial_heap_size_desc"));
87         initHeapSize = new MutableCountStatisticImpl(c);
88         
89         // Initialize the MutableCountStatistic for MaxHeapSize
90
c = new CountStatisticImpl(
91             localStrMgr.getString("monitor.stats.max_heap_size"), bytes_str,
92             localStrMgr.getString("monitor.stats.max_heap_size_desc"));
93         maxHeapSize = new MutableCountStatisticImpl(c);
94         
95         // Initialize the MutableCountStatistic for UsedHeapSize
96
c = new CountStatisticImpl(
97             localStrMgr.getString("monitor.stats.used_heap_size"), bytes_str,
98             localStrMgr.getString("monitor.stats.used_heap_size_desc"));
99         usedHeapSize = new MutableCountStatisticImpl(c);
100         
101         // Initialize the MutableCountStatistic for CommittedHeapSize
102
c = new CountStatisticImpl(
103             localStrMgr.getString("monitor.stats.committed_heap_size"), bytes_str,
104             localStrMgr.getString("monitor.stats.committed_heap_size_desc"));
105         commitHeapSize = new MutableCountStatisticImpl(c);
106         
107         // Initialize the MutableCountStatistic for InitialNonHeapSize
108
c = new CountStatisticImpl(
109             localStrMgr.getString("monitor.stats.initial_non_heap_size"), bytes_str,
110             localStrMgr.getString("monitor.stats.initial_non_heap_size_desc"));
111         initNonHeapSize = new MutableCountStatisticImpl(c);
112         
113         // Initialize the MutableCountStatistic for MaxNonHeapSize
114
c = new CountStatisticImpl(
115             localStrMgr.getString("monitor.stats.max_non_heap_size"),bytes_str,
116             localStrMgr.getString("monitor.stats.max_non_heap_size_desc"));
117         maxNonHeapSize = new MutableCountStatisticImpl(c);
118         
119         // Initialize the MutableCountStatistic for UsedNonHeapSize
120
c = new CountStatisticImpl(
121             localStrMgr.getString("monitor.stats.used_non_heap_size") ,bytes_str,
122             localStrMgr.getString("monitor.stats.used_non_heap_size_desc"));
123         usedNonHeapSize = new MutableCountStatisticImpl(c);
124         
125         // Initialize the MutableCountStatistic for CommittedNonHeapSize
126
c = new CountStatisticImpl(
127             localStrMgr.getString("monitor.stats.committed_non_heap_size"),
128             bytes_str,
129             localStrMgr.getString("monitor.stats.committed_non_heap_size_desc"));
130         commitNonHeapSize = new MutableCountStatisticImpl(c);
131         
132         // Initialize the MutableCountStatistic for ObjectsPendingFinalization
133
c = new CountStatisticImpl(
134             localStrMgr.getString("monitor.stats.objs_pending_serialization"),
135             StatisticImpl.DEFAULT_UNIT,
136             localStrMgr.getString("monitor.stats.objs_pending_serialization_desc"));
137         objPendingCount = new MutableCountStatisticImpl(c);
138     }
139     
140
141     public CountStatistic JavaDoc getCommittedHeapSize() {
142         commitHeapSize.setCount(heapUsage.getCommitted());
143         return (CountStatistic JavaDoc)commitHeapSize.unmodifiableView();
144     }
145     
146     public CountStatistic JavaDoc getCommittedNonHeapSize() {
147         commitNonHeapSize.setCount(nonheapUsage.getCommitted());
148         return (CountStatistic JavaDoc)commitNonHeapSize.unmodifiableView();
149     }
150     
151     public CountStatistic JavaDoc getInitHeapSize() {
152         initHeapSize.setCount(heapUsage.getInit());
153         return (CountStatistic JavaDoc)initHeapSize.unmodifiableView();
154     }
155     
156     public CountStatistic JavaDoc getInitNonHeapSize() {
157         initNonHeapSize.setCount(nonheapUsage.getCommitted());
158         return (CountStatistic JavaDoc)initNonHeapSize.unmodifiableView();
159     }
160     
161     public CountStatistic JavaDoc getMaxHeapSize() {
162         maxHeapSize.setCount(heapUsage.getMax());
163         return (CountStatistic JavaDoc)maxHeapSize.unmodifiableView();
164     }
165     
166     public CountStatistic JavaDoc getMaxNonHeapSize() {
167         maxNonHeapSize.setCount(nonheapUsage.getInit());
168         return (CountStatistic JavaDoc)maxNonHeapSize.unmodifiableView();
169     }
170     
171     public CountStatistic JavaDoc getObjectPendingFinalizationCount() {
172         objPendingCount.setCount(bean.getObjectPendingFinalizationCount());
173         return (CountStatistic JavaDoc)objPendingCount.unmodifiableView();
174     }
175     
176     public CountStatistic JavaDoc getUsedHeapSize() {
177         usedHeapSize.setCount(heapUsage.getUsed());
178         return (CountStatistic JavaDoc)usedHeapSize.unmodifiableView();
179     }
180     
181     public CountStatistic JavaDoc getUsedNonHeapSize() {
182         usedNonHeapSize.setCount(nonheapUsage.getUsed());
183         return (CountStatistic JavaDoc)usedNonHeapSize.unmodifiableView();
184     }
185     
186         
187     /**
188      * This method can be used to retrieve all the Statistics, exposed
189      * by this implementation of Stats
190      * @return Statistic[]
191      */

192     public Statistic JavaDoc[] getStatistics() {
193         return baseStatsImpl.getStatistics();
194     }
195     
196     /**
197      * queries for a Statistic by name.
198      * @return Statistic
199      */

200     public Statistic JavaDoc getStatistic(String JavaDoc str) {
201         return baseStatsImpl.getStatistic(str);
202     }
203     
204     /**
205      * returns an array of names of all the Statistics, that can be
206      * retrieved from this implementation of Stats
207      * @return String[]
208      */

209     public String JavaDoc[] getStatisticNames() {
210         return baseStatsImpl.getStatisticNames();
211     }
212 }
213
Popular Tags