1 24 package org.objectweb.jalisto.se.jmx; 25 26 import javax.management.*; 27 28 29 public class MemoryAdmin extends JalistoAbstractDynamicMBean { 30 31 public MemoryAdmin(String propertiesPath) { 32 } 33 34 public float getFreeMemorySize() { 35 return ((float) Runtime.getRuntime().freeMemory()) / mo; 36 } 37 38 public float getTotalMemorySize() { 39 return ((float) Runtime.getRuntime().totalMemory()) / mo; 40 } 41 42 public float getUsedMemorySize() { 43 return ((float) (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())) / mo; 44 } 45 46 47 48 49 public MBeanInfo getMBeanInfo() { 50 if (infos == null) { 51 MBeanAttributeInfo[] attributesInfos = new MBeanAttributeInfo[3]; 52 attributesInfos[0] = new MBeanAttributeInfo( 53 "FreeMemorySize", 54 "java.lang.Float", 55 "Size in Mo of free memory allocated to the JVM.", 56 true, 57 false, 58 false); 59 attributesInfos[1] = new MBeanAttributeInfo( 60 "TotalMemorySize", 61 "java.lang.Float", 62 "Size in Mo of memory allocated to the JVM.", 63 true, 64 false, 65 false); 66 attributesInfos[2] = new MBeanAttributeInfo( 67 "UsedMemorySize", 68 "java.lang.Float", 69 "Size in Mo of used memory allocated to the JVM.", 70 true, 71 false, 72 false); 73 74 infos = new MBeanInfo(this.getClass().getName(), 75 "Administration MBean to watch memory used by Jalisto", 76 attributesInfos, 77 new MBeanConstructorInfo[0], 78 new MBeanOperationInfo[0], 79 new MBeanNotificationInfo[0]); 80 } 81 return infos; 82 } 83 84 protected Object internalGetAttribute(String attributeName) 85 throws AttributeNotFoundException, MBeanException, ReflectionException { 86 if (attributeName.equals("FreeMemorySize")) { 87 return new Float (getFreeMemorySize()); 88 } 89 if (attributeName.equals("TotalMemorySize")) { 90 return new Float (getTotalMemorySize()); 91 } 92 if (attributeName.equals("UsedMemorySize")) { 93 return new Float (getUsedMemorySize()); 94 } 95 96 throw(new AttributeNotFoundException( 97 "Cannot find " + attributeName + " attribute in " + this.getClass().getName())); 98 } 99 100 protected boolean internalSetAttribute(String name, Object value) 101 throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { 102 if (name.equals("FreeMemorySize")) { 103 throw(new AttributeNotFoundException( 104 "Cannot set attribute " + name + " because it is read-only")); 105 } else if (name.equals("TotalMemorySize")) { 106 throw(new AttributeNotFoundException( 107 "Cannot set attribute " + name + " because it is read-only")); 108 } else if (name.equals("UsedMemorySize")) { 109 throw(new AttributeNotFoundException( 110 "Cannot set attribute " + name + " because it is read-only")); 111 } 112 return false; 113 } 114 115 protected Object internalInvoke(String operationName, Object [] params, String [] signature) 116 throws MBeanException, ReflectionException { 117 throw new ReflectionException(new NoSuchMethodException (operationName), 118 "Cannot find the operation " + operationName + 119 " in " + this.getClass().getName()); 120 } 121 122 123 private MBeanInfo infos; 124 125 private static final int mo = 1024 * 1024; 126 } 127 | Popular Tags |