1 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 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 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 119 public String getJavaVersion() { 120 return JAVA_VERSION; 121 } 122 123 129 public String getJavaVendor() { 130 return JAVA_VENDOR; 131 } 132 133 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 } 197 | Popular Tags |