KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > registry > spi > JVMMonitoringManager


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.registry.spi;
25 import java.util.List JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30 import java.lang.management.ManagementFactory JavaDoc;
31 import java.lang.management.GarbageCollectorMXBean JavaDoc;
32 import java.lang.management.ThreadMXBean JavaDoc;
33 import java.lang.management.ThreadInfo JavaDoc;
34 import com.sun.enterprise.admin.common.constant.AdminConstants;
35 import javax.management.j2ee.statistics.Stats JavaDoc;
36 import com.sun.enterprise.admin.monitor.stats.*;
37 import com.sun.enterprise.admin.monitor.registry.MonitoredObjectType;
38 import com.sun.enterprise.admin.monitor.registry.MonitoringLevel;
39 import com.sun.enterprise.admin.monitor.registry.MonitoringLevelListener;
40 import com.sun.enterprise.admin.monitor.registry.spi.MonitoringRegistrationHelper;
41 import com.sun.enterprise.admin.monitor.registry.MonitoringRegistrationException;
42 import com.sun.enterprise.admin.monitor.registry.StatsHolder;
43 import com.sun.enterprise.admin.monitor.registry.spi.StatsHolderImpl;
44 import com.sun.enterprise.admin.monitor.registry.spi.reconfig.MonitoringConfigurationHandler;
45 import com.sun.enterprise.admin.monitor.stats.spi.JVMCompilationStatsImpl;
46 import com.sun.enterprise.admin.monitor.stats.spi.JVMClassLoadingStatsImpl;
47 import com.sun.enterprise.admin.monitor.stats.spi.JVMRuntimeStatsImpl;
48 import com.sun.enterprise.admin.monitor.stats.spi.JVMOperatingSystemStatsImpl;
49 import com.sun.enterprise.admin.monitor.stats.spi.JVMGarbageCollectorStatsImpl;
50 import com.sun.enterprise.admin.monitor.stats.spi.JVMMemoryStatsImpl;
51 import com.sun.enterprise.admin.monitor.stats.spi.JVMThreadStatsImpl;
52 import com.sun.enterprise.admin.monitor.stats.spi.JVMThreadInfoStatsImpl;
53
54
55 /**
56  * Manager class responsible for registration/unregistration of all the
57  * JVM1.5 Stats. In addition, this class is responsible for handling the
58  * monitoring level change events pertaining to the JVM
59  */

60
61 public class JVMMonitoringManager implements MonitoringLevelListener {
62     
63     private static final Logger JavaDoc logger = Logger.getLogger(AdminConstants.kLoggerName);
64     private static final JVMMonitoringManager jmm = new JVMMonitoringManager();
65     private final int STACK_DEPTH = 5;
66     private final String JavaDoc THREAD_NODE_NAME = "thread";
67     
68     /** Creates a new instance of JVMMonitoringManager */
69     private JVMMonitoringManager() {
70     }
71     
72     public static JVMMonitoringManager getInstance() {
73         return jmm;
74     }
75     
76     /**
77      * Method that handles the registration of all the
78      * stats for various subsystems of the JVM
79      */

80     public void registerStats(StatsHolder rootNode) {
81         MonitoringLevel level = MonitoringConfigurationHandler.getLevel(MonitoredObjectType.JVM);
82         // check monitoring level of JVM before registering
83
if(level != MonitoringLevel.OFF) {
84             try {
85                 registerJVMCompilationStats(rootNode);
86                 registerJVMClassLoadingStats(rootNode);
87                 registerJVMRuntimeStats(rootNode);
88                 registerJVMOperatingSystemStats(rootNode);
89                 registerJVMGarbageCollectorStats(rootNode);
90                 registerJVMMemoryStats(rootNode);
91                 registerJVMThreadStats(rootNode);
92                 if(level == MonitoringLevel.HIGH)
93                     registerJVMThreadInfoStats(rootNode);
94             } catch(MonitoringRegistrationException mre) {
95                 logger.finest("MonitoringRegistrationException in the registration of JVM1.5 Stats: "+ mre.getLocalizedMessage());
96             } catch(Exception JavaDoc e) {
97                 logger.finest("Exception in the registration of JVM 1.5 Stats: "+ e.getLocalizedMessage());
98             }
99         }
100     }
101
102     //All the register/unregister methods for various subsystems of
103
//the JVM
104

105     /*
106      * Register the JVMCompilationStats with the monitoring registry
107      * @param rootNode the root node of the monitoring hierarchy
108      * @throws MonitoringRegistrationException
109      */

110     public void registerJVMCompilationStats(StatsHolder rootNode) throws
111            MonitoringRegistrationException {
112
113         final StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName());
114         assert(jvmNode != null): "jvm node is null";
115         StatsHolder childNode = jvmNode.addChild(MonitoredObjectType.JVM_COMPILATION.getTypeName(),
116                                                  MonitoredObjectType.JVM_COMPILATION);
117         childNode.setStats(new JVMCompilationStatsImpl());
118         childNode.setStatsClass(com.sun.enterprise.admin.monitor.stats.JVMCompilationStats.class);
119         childNode.setDottedName(DottedNameFactory.getJVMCompilationDottedName());
120         childNode.setObjectName(MonitoringObjectNames.getJVMCompilationObjectName());
121         childNode.registerMBean();
122     }
123            
124     /*
125      * Register the JVMCClassLoadingStats with the monitoring registry
126      * @param rootNode the root node of the monitoring hierarchy
127      * @throws MonitoringRegistrationException
128      */

129     public void registerJVMClassLoadingStats(StatsHolder rootNode)
130            throws MonitoringRegistrationException {
131                
132         final StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName());
133         assert(jvmNode != null): "jvm node is null";
134         StatsHolder childNode = jvmNode.addChild(MonitoredObjectType.JVM_CLASSLOADING.getTypeName(),
135                                                  MonitoredObjectType.JVM_CLASSLOADING);
136         childNode.setStats(new JVMClassLoadingStatsImpl());
137         childNode.setStatsClass(com.sun.enterprise.admin.monitor.stats.JVMClassLoadingStats.class);
138         childNode.setDottedName(DottedNameFactory.getJVMClassLoadingDottedName());
139         childNode.setObjectName(MonitoringObjectNames.getJVMClassLoadingObjectName());
140         childNode.registerMBean();
141     }
142             
143     /*
144      * Register the JVMRuntimeStats with the monitoring registry
145      * @param rootNode the root node of the monitoring hierarchy
146      * @throws MonitoringRegistrationException
147      */

148     public void registerJVMRuntimeStats(StatsHolder rootNode) throws
149            MonitoringRegistrationException {
150      
151         final StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName());
152         assert(jvmNode != null): "jvm node is null";
153         StatsHolder childNode = jvmNode.addChild(MonitoredObjectType.JVM_RUNTIME.getTypeName(),
154                                                  MonitoredObjectType.JVM_RUNTIME);
155         childNode.setStats(new JVMRuntimeStatsImpl());
156         childNode.setStatsClass(com.sun.enterprise.admin.monitor.stats.JVMRuntimeStats.class);
157         childNode.setDottedName(DottedNameFactory.getJVMRuntimeDottedName());
158         childNode.setObjectName(MonitoringObjectNames.getJVMRuntimeObjectName());
159         childNode.registerMBean();
160     }
161     
162     
163     /*
164      * Register the JVMOperatingSystemStats with the monitoring registry
165      * @param rootNode the root node of the monitoring hierarchy
166      * @throws MonitoringRegistrationException
167      */

168     public void registerJVMOperatingSystemStats(StatsHolder rootNode)
169            throws MonitoringRegistrationException {
170     
171         final StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName());
172         assert(jvmNode != null): "jvm node is null";
173         StatsHolder childNode = jvmNode.addChild(MonitoredObjectType.JVM_OS.getTypeName(),
174                                                  MonitoredObjectType.JVM_OS);
175         childNode.setStats(new JVMOperatingSystemStatsImpl());
176         childNode.setStatsClass(com.sun.enterprise.admin.monitor.stats.JVMOperatingSystemStats.class);
177         childNode.setDottedName(DottedNameFactory.getJVMOSDottedName());
178         childNode.setObjectName(MonitoringObjectNames.getJVMOSObjectName());
179         childNode.registerMBean();
180     }
181     
182     /*
183      * Unregister the from the monitoring registry
184      * @param rootNode root node of the monitoring hierarchy
185      * @param includeThreadInfo boolean to indicate if the threadinfo stats
186      * have to unregistered
187      * @throws MonitoringRegistrationException
188      */

189     public void unregisterStats(StatsHolder rootNode, boolean includeThreadInfo) throws
190            MonitoringRegistrationException {
191         
192         StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName());
193         assert(jvmNode != null):"jvm node is null";
194         Collection JavaDoc c = new ArrayList JavaDoc();
195         c.addAll(jvmNode.getAllChildren());
196         Iterator JavaDoc iter = c.iterator();
197         while(iter.hasNext()) {
198             StatsHolder childNode = (StatsHolder)iter.next();
199             
200             if(childNode.getType() == MonitoredObjectType.JVM_GCS) {
201                 Collection JavaDoc c1 = new ArrayList JavaDoc();
202                 c1.addAll(childNode.getAllChildren());
203                 Iterator JavaDoc it = c1.iterator();
204                 while(it.hasNext()) {
205                     StatsHolder s = (StatsHolder)it.next();
206                     s.unregisterMBean();
207                     childNode.removeChild(s.getName());
208                 }
209             }
210             
211             if((childNode.getType() == MonitoredObjectType.JVM_THREAD) && includeThreadInfo) {
212                 Collection JavaDoc c1 = new ArrayList JavaDoc();
213                 c1.addAll(childNode.getAllChildren());
214                 Iterator JavaDoc it = c1.iterator();
215                 while(it.hasNext()) {
216                     StatsHolder s = (StatsHolder)it.next();
217                     s.unregisterMBean();
218                     childNode.removeChild(s.getName());
219                 }
220             }
221             
222             childNode.unregisterMBean();
223             jvmNode.removeChild(childNode.getName());
224         }
225     }
226     
227     /**
228      * Register the JVMGarbageCollectorStats with the monitoring registry
229      * @param rootNode the root node of the monitoring hierarchy
230      * @throws MonitoringRegistrationException
231      */

232     public void registerJVMGarbageCollectorStats(StatsHolder rootNode) throws
233            MonitoringRegistrationException {
234                
235         StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName());
236         assert(jvmNode != null):"jvm node is null";
237         // setup the parent node for all garbage collectors
238
StatsHolder gcsNode = jvmNode.addChild(MonitoredObjectType.JVM_GCS.getTypeName(),
239                                                MonitoredObjectType.JVM_GCS);
240         gcsNode.setDottedName(DottedNameFactory.getJVMGCSDottedName());
241         gcsNode.setObjectName(MonitoringObjectNames.getJVMGCSSObjectName());
242         gcsNode.registerMBean();
243         // now register stats for each garbage colelctor
244
List JavaDoc beanList = ManagementFactory.getGarbageCollectorMXBeans();
245         Iterator JavaDoc iter = beanList.iterator();
246         while(iter.hasNext()) {
247             GarbageCollectorMXBean JavaDoc bean = (GarbageCollectorMXBean JavaDoc) iter.next();
248             StatsHolder gcNode = gcsNode.addChild(bean.getName(), MonitoredObjectType.JVM_GC);
249             gcNode.setStats(new JVMGarbageCollectorStatsImpl(bean));
250             gcNode.setStatsClass(com.sun.enterprise.admin.monitor.stats.JVMGarbageCollectorStats.class);
251             gcNode.setDottedName(DottedNameFactory.getJVMGCDottedName(bean.getName()));
252             gcNode.setObjectName(MonitoringObjectNames.getJVMGCObjectName(bean.getName()));
253             gcNode.registerMBean();
254         }
255     }
256     
257     /**
258      * Register the JVMMemoryStats with the monitoring registry
259      * @param rootNode the root node of the monitoring hierarchy
260      * @throws MonitoringRegistrationException
261      */

262     public void registerJVMMemoryStats(StatsHolder rootNode)
263                                         throws MonitoringRegistrationException {
264         
265         final StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName());
266         assert(jvmNode != null): "jvm node is null";
267         StatsHolder childNode = jvmNode.addChild(MonitoredObjectType.JVM_MEMORY.getTypeName(),
268                                                  MonitoredObjectType.JVM_MEMORY);
269         childNode.setStats(new JVMMemoryStatsImpl());
270         childNode.setStatsClass(com.sun.enterprise.admin.monitor.stats.JVMMemoryStats.class);
271         childNode.setDottedName(DottedNameFactory.getJVMMemoryDottedName());
272         childNode.setObjectName(MonitoringObjectNames.getJVMMemoryObjectName());
273         childNode.registerMBean();
274     }
275     
276     public void registerJVMThreadStats(StatsHolder rootNode)
277                                         throws MonitoringRegistrationException {
278         
279         final StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName());
280         assert(jvmNode != null): "jvm node is null";
281         StatsHolder childNode = jvmNode.addChild(MonitoredObjectType.JVM_THREAD.getTypeName(),
282                                                  MonitoredObjectType.JVM_THREAD);
283         childNode.setStats(new JVMThreadStatsImpl());
284         childNode.setStatsClass(com.sun.enterprise.admin.monitor.stats.JVMThreadStats.class);
285         childNode.setDottedName(DottedNameFactory.getJVMThreadDottedName());
286         childNode.setObjectName(MonitoringObjectNames.getJVMThreadObjectName());
287         childNode.registerMBean();
288     }
289     
290     public void registerJVMThreadInfoStats(StatsHolder rootNode) throws
291                                                MonitoringRegistrationException {
292         
293         final StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName());
294         assert(jvmNode != null): "jvm node is null";
295         final StatsHolder threadSystemNode = jvmNode.getChild(MonitoredObjectType.JVM_THREAD.getTypeName());
296         assert(threadSystemNode != null): "thread-system node is null";
297         ThreadMXBean JavaDoc bean = ManagementFactory.getThreadMXBean();
298         long ids[] = bean.getAllThreadIds();
299         for(int i=0; i < ids.length; i++) {
300             ThreadInfo JavaDoc info = bean.getThreadInfo(ids[i], STACK_DEPTH);
301             String JavaDoc threadName = THREAD_NODE_NAME + "-" + ids[i];
302             StatsHolder childNode = threadSystemNode.addChild(threadName, MonitoredObjectType.JVM_THREAD_INFO);
303             childNode.setStats(new JVMThreadInfoStatsImpl(info));
304             childNode.setStatsClass(com.sun.enterprise.admin.monitor.stats.JVMThreadInfoStats.class);
305             childNode.setDottedName(DottedNameFactory.getJVMThreadInfoDottedName(threadName));
306             childNode.setObjectName(MonitoringObjectNames.getJVMThreadInfoObjectName(threadName));
307             childNode.registerMBean();
308         }
309     }
310     
311     public void unregisterJVMThreadInfoStats(StatsHolder rootNode) throws
312                                                MonitoringRegistrationException {
313         
314         final StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName());
315         assert(jvmNode != null): "jvm node is null";
316         final StatsHolder threadSystemNode = jvmNode.getChild(MonitoredObjectType.JVM_THREAD.getTypeName());
317         assert(threadSystemNode != null): "thread-system node is null";
318         Collection JavaDoc c = new ArrayList JavaDoc();
319         c.addAll(threadSystemNode.getAllChildren());
320         Iterator JavaDoc iter = c.iterator();
321         while(iter.hasNext()) {
322             StatsHolder s = (StatsHolder)iter.next();
323             s.unregisterMBean();
324             threadSystemNode.removeChild(s.getName());
325         }
326     }
327     
328     private void registerAllStats(StatsHolder rootNode, boolean includeThreadInfo) {
329         try {
330             registerJVMCompilationStats(rootNode);
331             registerJVMClassLoadingStats(rootNode);
332             registerJVMRuntimeStats(rootNode);
333             registerJVMOperatingSystemStats(rootNode);
334             registerJVMGarbageCollectorStats(rootNode);
335             registerJVMMemoryStats(rootNode);
336             registerJVMThreadStats(rootNode);
337             if(includeThreadInfo)
338                 registerJVMThreadInfoStats(rootNode);
339         } catch(MonitoringRegistrationException mre) {
340             logger.finest("MonitoringRegistrationException in the registration of JVM1.5 Stats: "+ mre.getLocalizedMessage());
341         } catch(Exception JavaDoc e) {
342             logger.finest("Exception in the registration of JVM 1.5 Stats: "+ e.getLocalizedMessage());
343         }
344     }
345        
346     
347     // Methods to handle the monitoringlevel changes
348
public void changeLevel (MonitoringLevel from,
349                              MonitoringLevel to,
350                              MonitoredObjectType type) {
351         
352         logger.finest("changeLevel being invoked on the JVM");
353         final MonitoringRegistrationHelper registryImpl =
354             (MonitoringRegistrationHelper) MonitoringRegistrationHelper.getInstance();
355         
356         StatsHolder root = registryImpl.getRootStatsHolder();
357         boolean includeThreadInfo = false;
358         
359         if(to == MonitoringLevel.OFF) {
360             if(from == MonitoringLevel.HIGH)
361                 includeThreadInfo = true;
362             try {
363                 unregisterStats(root, includeThreadInfo);
364             } catch(MonitoringRegistrationException mre) {
365                 logger.finest("MonitoringRegistrationException in the unregistration of JVM 1.5 Stats: "+ mre.getLocalizedMessage());
366             } catch(Exception JavaDoc e) {
367                 logger.finest("Exception in the unregistration of JVM 1.5 Stats: "+ e.getLocalizedMessage());
368             }
369         }
370         
371         if(from == MonitoringLevel.OFF) {
372             if(to == MonitoringLevel.HIGH)
373                 includeThreadInfo = true;
374             registerAllStats(root, includeThreadInfo);
375         }
376         
377         if(from == MonitoringLevel.LOW && to == MonitoringLevel.HIGH) {
378             try {
379                 registerJVMThreadInfoStats(root);
380             } catch(MonitoringRegistrationException mre) {
381                 logger.finest("MonitoringRegistrationException in the registration of JVM ThreadInfoStats: "+ mre.getLocalizedMessage());
382             } catch(Exception JavaDoc e) {
383                 logger.finest("Exception in the registration of JVM ThreadInfo Stats: "+ e.getLocalizedMessage());
384             }
385         }
386         
387         if(from == MonitoringLevel.HIGH && to == MonitoringLevel.LOW) {
388             try{
389                 unregisterJVMThreadInfoStats(root);
390             } catch(MonitoringRegistrationException mre) {
391                 logger.finest("MonitoringRegistrationException in the unregistration of JVM ThreadInfoStats: "+ mre.getLocalizedMessage());
392             } catch(Exception JavaDoc e) {
393                 logger.finest("Exception in the unregistration of JVM ThreadInfo Stats: "+ e.getLocalizedMessage());
394             }
395         }
396             
397     }
398     
399     public void changeLevel (MonitoringLevel from,
400                              MonitoringLevel to,
401                              Stats JavaDoc handback) {
402     }
403     
404     public void setLevel (MonitoringLevel level) {
405     }
406 }
407
Popular Tags