001 /**
002 *
003 * Copyright 2003-2004 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.j2ee.management.impl;
019
020 import java.net.InetAddress;
021 import java.net.UnknownHostException;
022 import java.util.Date;
023 import java.util.Hashtable;
024 import java.util.Properties;
025 import javax.management.ObjectName;
026 import javax.management.j2ee.statistics.Stats;
027
028 import org.apache.geronimo.gbean.GBeanInfo;
029 import org.apache.geronimo.gbean.GBeanInfoBuilder;
030 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
031 import org.apache.geronimo.kernel.Kernel;
032 import org.apache.geronimo.kernel.ObjectNameUtil;
033 import org.apache.geronimo.management.StatisticsProvider;
034 import org.apache.geronimo.management.geronimo.JVM;
035 import org.apache.geronimo.management.stats.BoundedRangeImpl;
036 import org.apache.geronimo.management.stats.JVMStatsImpl;
037 import org.apache.geronimo.system.logging.SystemLog;
038
039 /**
040 *
041 *
042 * @version $Rev: 396206 $ $Date: 2006-04-22 19:55:45 -0700 (Sat, 22 Apr 2006) $
043 */
044 public class JVMImpl implements JVM, StatisticsProvider {
045 public static final String JAVA_VERSION = System.getProperty("java.version");
046 public static final String JAVA_VENDOR = System.getProperty("java.vendor");
047 public static final String NODE;
048 private static final Runtime runtime = Runtime.getRuntime();
049
050 static {
051 String node;
052 try {
053 node = InetAddress.getLocalHost().toString();
054 } catch (UnknownHostException e) {
055 node = null;
056 }
057 NODE = node;
058 }
059
060 private final String objectName;
061 private final Kernel kernel;
062 private final SystemLog systemLog;
063 private JVMStatsImpl stats;
064
065 public JVMImpl(String objectName, Kernel kernel, SystemLog systemLog) {
066 this.objectName = objectName;
067 this.kernel = kernel;
068 this.systemLog = systemLog;
069 ObjectName myObjectName = ObjectNameUtil.getObjectName(this.objectName);
070 verifyObjectName(myObjectName);
071 }
072
073 /**
074 * ObjectName must match this pattern:
075 * <p/>
076 * domain:j2eeType=JVM,name=MyName
077 */
078 private void verifyObjectName(ObjectName objectName) {
079 if (objectName.isPattern()) {
080 throw new InvalidObjectNameException("ObjectName can not be a pattern", objectName);
081 }
082 Hashtable keyPropertyList = objectName.getKeyPropertyList();
083 if (!"JVM".equals(keyPropertyList.get("j2eeType"))) {
084 throw new InvalidObjectNameException("JVM object name j2eeType property must be 'JVM'", objectName);
085 }
086 if (!keyPropertyList.containsKey("name")) {
087 throw new InvalidObjectNameException("JVM object must contain a name property", objectName);
088 }
089 if (!keyPropertyList.containsKey("J2EEServer")) {
090 throw new InvalidObjectNameException("JVM object must contain a J2EEServer property", objectName);
091 }
092 if (keyPropertyList.size() != 3) {
093 throw new InvalidObjectNameException("JVM object name can only have J2EEServer, j2eeType, and name", objectName);
094 }
095 }
096
097 public String getObjectName() {
098 return objectName;
099 }
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 }