1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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;
21 import java.net.UnknownHostException;
22 import java.util.Date;
23 import java.util.Hashtable;
24 import java.util.Properties;
25 import javax.management.ObjectName;
26 import javax.management.j2ee.statistics.Stats;
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: 396206 $ $Date: 2006-04-22 19:55:45 -0700 (Sat, 22 Apr 2006) $
43 */
44 public class JVMImpl implements JVM, StatisticsProvider {
45 public static final String JAVA_VERSION = System.getProperty("java.version");
46 public static final String JAVA_VENDOR = System.getProperty("java.vendor");
47 public static final String NODE;
48 private static final Runtime runtime = Runtime.getRuntime();
49
50 static {
51 String node;
52 try {
53 node = InetAddress.getLocalHost().toString();
54 } catch (UnknownHostException e) {
55 node = null;
56 }
57 NODE = node;
58 }
59
60 private final String objectName;
61 private final Kernel kernel;
62 private final SystemLog systemLog;
63 private JVMStatsImpl stats;
64
65 public JVMImpl(String objectName, Kernel kernel, SystemLog systemLog) {
66 this.objectName = objectName;
67 this.kernel = kernel;
68 this.systemLog = systemLog;
69 ObjectName 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 objectName) {
79 if (objectName.isPattern()) {
80 throw new InvalidObjectNameException("ObjectName can not be a pattern", objectName);
81 }
82 Hashtable 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 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 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 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 getNode() {
141 return NODE;
142 }
143
144 public int getAvailableProcessors() {
145 return runtime.availableProcessors();
146 }
147
148 public Date getKernelBootTime() {
149 return kernel.getBootTime();
150 }
151
152 public Stats 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 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[] {"objectName", "kernel", "SystemLog"});
190 GBEAN_INFO = infoFactory.getBeanInfo();
191 }
192
193 public static GBeanInfo getGBeanInfo() {
194 return GBEAN_INFO;
195 }
196 }