KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > jvm > JVMInformationCollector


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  * JVMInformationCollectorMBean.java
26  *
27  * Created on July 20, 2005, 8:19 PM
28  */

29
30
31 package com.sun.enterprise.admin.mbeans.jvm;
32
33 import com.sun.enterprise.admin.server.core.AdminService;
34 import com.sun.enterprise.admin.target.Target;
35 import com.sun.enterprise.admin.target.TargetBuilder;
36 import com.sun.enterprise.admin.target.TargetType;
37 import com.sun.enterprise.config.ConfigContext;
38 import com.sun.enterprise.util.SystemPropertyConstants;
39 import com.sun.enterprise.util.i18n.StringManager;
40 import java.text.NumberFormat JavaDoc;
41 import java.util.Hashtable JavaDoc;
42 import javax.management.MBeanRegistration JavaDoc;
43 import javax.management.MBeanServer JavaDoc;
44 import javax.management.MBeanServerConnection JavaDoc;
45 import javax.management.NotCompliantMBeanException JavaDoc;
46 import javax.management.ObjectName JavaDoc;
47 import javax.management.StandardMBean JavaDoc;
48
49 /**
50  */

51 public class JVMInformationCollector extends StandardMBean JavaDoc implements JVMInformationMBean, MBeanRegistration JavaDoc {
52     
53     private MBeanServerConnection JavaDoc mbsc;
54     private static final StringManager sm = StringManager.getManager(JVMInformationCollector.class);
55     public JVMInformationCollector() throws NotCompliantMBeanException JavaDoc {
56         super(JVMInformationMBean.class);
57     }
58     public String JavaDoc getThreadDump(final String JavaDoc processName) {
59         final ObjectName JavaDoc on = processTarget(processName);
60         final String JavaDoc td = invokeMBean(on, "getThreadDump");
61         return ( td );
62     }
63
64     public String JavaDoc getSummary(final String JavaDoc processName) {
65         final ObjectName JavaDoc on = processTarget(processName);
66         final String JavaDoc s = invokeMBean(on, "getSummary");
67         return ( s );
68     }
69
70     public String JavaDoc getMemoryInformation(final String JavaDoc processName) {
71         final ObjectName JavaDoc on = processTarget(processName);
72         final String JavaDoc mi = invokeMBean(on, "getMemoryInformation");
73         return ( mi );
74     }
75
76     public String JavaDoc getClassInformation(final String JavaDoc processName) {
77         final ObjectName JavaDoc on = processTarget(processName);
78         final String JavaDoc ci = invokeMBean(on, "getClassInformation");
79         return ( ci );
80     }
81     
82     private ObjectName JavaDoc processTarget(final String JavaDoc processName) throws RuntimeException JavaDoc {
83         try {
84             final TargetType[] vts = {TargetType.DAS, TargetType.SERVER};
85             final ConfigContext acc = AdminService.getAdminService().getAdminContext().getAdminConfigContext();
86             final Target t = TargetBuilder.INSTANCE.createTarget(null, vts, processName, acc); //DAS is the default target
87
//get the object-name of the "other" real implementation of JVMInformationMBean interface :)
88
final String JavaDoc sn = t.getName();
89             final String JavaDoc cn = JVMInformation.class.getSimpleName();
90             final ObjectName JavaDoc on = formObjectName(sn, cn);
91             if (! this.mbsc.isRegistered(on)) {
92                 final String JavaDoc msg = sm.getString("server.unreachable", sn);
93                 throw new RuntimeException JavaDoc(msg);
94             }
95             return (on);
96         } catch (final RuntimeException JavaDoc re) {
97             throw(re);
98         } catch (final Exception JavaDoc e) {
99             throw new RuntimeException JavaDoc(e);
100         }
101     }
102
103     private String JavaDoc invokeMBean(final ObjectName JavaDoc jvm, final String JavaDoc method) throws RuntimeException JavaDoc {
104         try {
105             //though proxies work fine, for now (jul 2005), I am not going to use them because I am not sure how they work with cascading
106
//it is okay to assume that the methods in this mbean take String as parameter
107
final Object JavaDoc[] params = {null};
108             final String JavaDoc[] sign = {"java.lang.String"};
109             final Object JavaDoc ret = this.mbsc.invoke(jvm, method, params, sign);
110             
111             return ( (String JavaDoc) ret );
112             
113         } catch (final Exception JavaDoc e) {
114             throw new RuntimeException JavaDoc(e);
115         }
116     }
117     public void postRegister(Boolean JavaDoc registrationDone) {
118     }
119
120     public ObjectName JavaDoc preRegister(final MBeanServer JavaDoc server, final ObjectName JavaDoc name) throws Exception JavaDoc {
121         this.mbsc = server;
122         final String JavaDoc sn = System.getProperty(SystemPropertyConstants.SERVER_NAME);
123         final ObjectName JavaDoc on = formObjectName(sn, JVMInformationCollector.class.getSimpleName());
124         return ( on );
125     }
126
127     public void preDeregister() throws Exception JavaDoc {
128     }
129
130     public void postDeregister() {
131     }
132     
133     /* package private */ static ObjectName JavaDoc formObjectName(final String JavaDoc sn, final String JavaDoc cName) throws Exception JavaDoc {
134         /* domain:type=impl-class,server=target-server*/
135         final String JavaDoc domain = AdminService.PRIVATE_MBEAN_DOMAIN_NAME;
136         final Hashtable JavaDoc<String JavaDoc, String JavaDoc> props = new Hashtable JavaDoc<String JavaDoc, String JavaDoc> ();
137         props.put("type", cName);
138         props.put("category", "monitor");
139         final String JavaDoc snk = "server";
140         props.put(snk, sn);
141         return ( new ObjectName JavaDoc(domain, props) );
142     }
143     
144     static String JavaDoc millis2HoursMinutesSeconds(final long millis) {
145         final long secmin = millis / (long) 1000;
146         final long sec = secmin % 60;
147         final long minhr = secmin / 60;
148         final long min = minhr % 60;
149         final long hr = minhr / 60;
150         final String JavaDoc msg = sm.getString("m2hms", hr, min, sec);
151         
152         return ( msg );
153     }
154     static String JavaDoc millis2SecondsMillis(final long millis) {
155         final long sec = millis / (long) 1000;
156         final long ms = millis % 1000;
157         final String JavaDoc msg = sm.getString("m2sms", sec, ms);
158         return ( msg );
159     }
160     static String JavaDoc formatLong(final long sayBytes) {
161         final NumberFormat JavaDoc n = NumberFormat.getInstance();
162         return ( n.format(sayBytes) );
163     }
164 }
165
Popular Tags