KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > j2ee > management > impl > JVMImpl


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.j2ee.management.impl;
19
20 import java.net.InetAddress JavaDoc;
21 import java.net.UnknownHostException JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Properties JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26 import javax.management.j2ee.statistics.Stats JavaDoc;
27
28 import org.apache.geronimo.gbean.GBeanInfo;
29 import org.apache.geronimo.gbean.GBeanInfoBuilder;
30 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
31 import org.apache.geronimo.kernel.Kernel;
32 import org.apache.geronimo.kernel.ObjectNameUtil;
33 import org.apache.geronimo.management.StatisticsProvider;
34 import org.apache.geronimo.management.geronimo.JVM;
35 import org.apache.geronimo.management.stats.BoundedRangeImpl;
36 import org.apache.geronimo.management.stats.JVMStatsImpl;
37 import org.apache.geronimo.system.logging.SystemLog;
38
39 /**
40  *
41  *
42  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
43  */

44 public class JVMImpl implements JVM, StatisticsProvider {
45     public static final String JavaDoc JAVA_VERSION = System.getProperty("java.version");
46     public static final String JavaDoc JAVA_VENDOR = System.getProperty("java.vendor");
47     public static final String JavaDoc NODE;
48     private static final Runtime JavaDoc runtime = Runtime.getRuntime();
49
50     static {
51         String JavaDoc node;
52         try {
53             node = InetAddress.getLocalHost().toString();
54         } catch (UnknownHostException JavaDoc e) {
55             node = null;
56         }
57         NODE = node;
58     }
59
60     private final String JavaDoc objectName;
61     private final Kernel kernel;
62     private final SystemLog systemLog;
63     private JVMStatsImpl stats;
64
65     public JVMImpl(String JavaDoc objectName, Kernel kernel, SystemLog systemLog) {
66         this.objectName = objectName;
67         this.kernel = kernel;
68         this.systemLog = systemLog;
69         ObjectName JavaDoc myObjectName = ObjectNameUtil.getObjectName(this.objectName);
70         verifyObjectName(myObjectName);
71     }
72
73     /**
74      * ObjectName must match this pattern:
75      * <p/>
76      * domain:j2eeType=JVM,name=MyName
77      */

78     private void verifyObjectName(ObjectName JavaDoc objectName) {
79         if (objectName.isPattern()) {
80             throw new InvalidObjectNameException("ObjectName can not be a pattern", objectName);
81         }
82         Hashtable JavaDoc keyPropertyList = objectName.getKeyPropertyList();
83         if (!"JVM".equals(keyPropertyList.get("j2eeType"))) {
84             throw new InvalidObjectNameException("JVM object name j2eeType property must be 'JVM'", objectName);
85         }
86         if (!keyPropertyList.containsKey("name")) {
87             throw new InvalidObjectNameException("JVM object must contain a name property", objectName);
88         }
89         if (!keyPropertyList.containsKey("J2EEServer")) {
90             throw new InvalidObjectNameException("JVM object must contain a J2EEServer property", objectName);
91         }
92         if (keyPropertyList.size() != 3) {
93             throw new InvalidObjectNameException("JVM object name can only have J2EEServer, j2eeType, and name", objectName);
94         }
95     }
96
97     public String JavaDoc getObjectName() {
98         return objectName;
99     }
100
101     public boolean isStateManageable() {
102         return true;
103     }
104
105     public boolean isStatisticsProvider() {
106         return true;
107     }
108
109     public boolean isEventProvider() {
110         return true;
111     }
112
113     /**
114      * The version of the JVMImpl we are running on.
115      * This is the value of java.version system property
116      * @see "JSR77.3.4.1.1"
117      * @return the JVMImpl version
118      */

119     public String JavaDoc getJavaVersion() {
120         return JAVA_VERSION;
121     }
122
123     /**
124      * The vendor of the JVMImpl we are running on.
125      * This is the value of java.vendor system property
126      * @see "JSR77.3.4.1.2"
127      * @return the JVMImpl version
128      */

129     public String JavaDoc getJavaVendor() {
130         return JAVA_VENDOR;
131     }
132
133     /**
134      * The node we are running on.
135      * This is the fully qualified host name returned for InetAddress.getLocalHost.toString();
136      * we return null if there is no network
137      * @see "JSR77.3.4.1.3"
138      * @return the node we are running on
139      */

140     public String JavaDoc getNode() {
141         return NODE;
142     }
143
144     public int getAvailableProcessors() {
145         return runtime.availableProcessors();
146     }
147
148     public Date JavaDoc getKernelBootTime() {
149         return kernel.getBootTime();
150     }
151
152     public Stats JavaDoc getStats() {
153         BoundedRangeImpl heap;
154         if(stats == null) {
155             stats = new JVMStatsImpl();
156             long start = kernel.getBootTime().getTime();
157             stats.getUpTimeImpl().setCount(start);
158             stats.getUpTimeImpl().setStartTime(start);
159             heap = stats.getHeapSizeImpl();
160             heap.setStartTime(start);
161             heap.setBounds(0, runtime.totalMemory());
162             heap.setCurrent(heap.getUpperBound() - runtime.freeMemory());
163             heap.setLowWaterMark(heap.getCurrent());
164             heap.setHighWaterMark(heap.getCurrent());
165         } else {
166             heap = stats.getHeapSizeImpl();
167             heap.setBounds(0, runtime.totalMemory());
168             heap.setCurrent(heap.getUpperBound() - runtime.freeMemory());
169         }
170         long now = System.currentTimeMillis();
171         stats.getUpTimeImpl().setLastSampleTime(now);
172         heap.setLastSampleTime(now);
173         return stats;
174     }
175
176     public Properties JavaDoc getSystemProperties() {
177         return System.getProperties();
178     }
179
180     public SystemLog getSystemLog() {
181         return systemLog;
182     }
183
184     public static final GBeanInfo GBEAN_INFO;
185
186     static {
187         GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(JVMImpl.class, NameFactory.JVM);
188         infoFactory.addReference("SystemLog", SystemLog.class);
189         infoFactory.setConstructor(new String JavaDoc[] {"objectName", "kernel", "SystemLog"});
190         GBEAN_INFO = infoFactory.getBeanInfo();
191     }
192
193     public static GBeanInfo getGBeanInfo() {
194         return GBEAN_INFO;
195     }
196 }
197
Popular Tags