KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > jmx > MemoryAdmin


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

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 JavaDoc 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     /***************************** DYNAMIC MBEAN IMPLEMENTATION *****************************/
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 JavaDoc internalGetAttribute(String JavaDoc attributeName)
85             throws AttributeNotFoundException, MBeanException, ReflectionException {
86         if (attributeName.equals("FreeMemorySize")) {
87             return new Float JavaDoc(getFreeMemorySize());
88         }
89         if (attributeName.equals("TotalMemorySize")) {
90             return new Float JavaDoc(getTotalMemorySize());
91         }
92         if (attributeName.equals("UsedMemorySize")) {
93             return new Float JavaDoc(getUsedMemorySize());
94         }
95
96         throw(new AttributeNotFoundException(
97                 "Cannot find " + attributeName + " attribute in " + this.getClass().getName()));
98     }
99
100     protected boolean internalSetAttribute(String JavaDoc name, Object JavaDoc 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 JavaDoc internalInvoke(String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature)
116             throws MBeanException, ReflectionException {
117         throw new ReflectionException(new NoSuchMethodException JavaDoc(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