KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.objectweb.jalisto.se.impl.server.oid.OidTableImpl;
27 import org.objectweb.jalisto.se.api.internal.SessionInternal;
28 import org.objectweb.jalisto.se.api.cache.JalistoCache;
29
30 import javax.management.*;
31 import java.util.Map JavaDoc;
32
33 public class CacheAdmin extends JalistoAbstractDynamicMBean {
34
35     public CacheAdmin(String JavaDoc propertiesPath) {
36         this.propertiesPath = propertiesPath;
37         this.provider = SessionProvider.getInstance();
38     }
39
40
41     public int getOidTableCurrentSize() throws MBeanException {
42         try {
43             SessionInternal session = provider.getSessionForMbean(propertiesPath);
44             OidTableImpl oidTable = (OidTableImpl) session.getOidTable();
45             return oidTable.getTransactionalSize(session.getSessionId());
46         } catch (Exception JavaDoc e) {
47             throw new MBeanException(e, "failed to get OidTableCurrentSize");
48         }
49     }
50
51     public int getOidTableMaxSize() throws MBeanException {
52         try {
53             SessionInternal session = provider.getSessionForMbean(propertiesPath);
54             OidTableImpl oidTable = (OidTableImpl) session.getOidTable();
55             return oidTable.getOidTableSize();
56         } catch (Exception JavaDoc e) {
57             throw new MBeanException(e, "failed to get OidTableMaxSize");
58         }
59     }
60
61     public int getPageCacheCurrentSize() throws MBeanException {
62         try {
63             SessionInternal session = provider.getSessionForMbean(propertiesPath);
64             return session.getFileAccess().getPhysicalAccess().getBaseImage().getReadObjects().size();
65         } catch (Exception JavaDoc e) {
66             throw new MBeanException(e, "failed to get PageCacheCurrentSize");
67         }
68     }
69
70     public int getPageCacheMaxSize() throws MBeanException {
71         try {
72             SessionInternal session = provider.getSessionForMbean(propertiesPath);
73             Map JavaDoc map = session.getFileAccess().getPhysicalAccess().getBaseImage().getReadObjects();
74             if (map instanceof JalistoCache) {
75                 return ((JalistoCache) map).getMaxSize();
76             } else {
77                 return -1;
78             }
79         } catch (Exception JavaDoc e) {
80             throw new MBeanException(e, "failed to get OidTableMaxSize");
81         }
82     }
83
84     public void setOidTableMaxSize(int size) throws MBeanException {
85         try {
86             SessionInternal session = provider.getSessionForMbean(propertiesPath);
87             OidTableImpl oidTable = (OidTableImpl) session.getOidTable();
88             oidTable.setOidTableSize(size);
89         } catch (Exception JavaDoc e) {
90             throw new MBeanException(e, "failed to set OidTableMaxSize");
91         }
92     }
93
94     public void setPageCacheMaxSize(int size) throws MBeanException {
95         try {
96             SessionInternal session = provider.getSessionForMbean(propertiesPath);
97             Map JavaDoc map = session.getFileAccess().getPhysicalAccess().getBaseImage().getReadObjects();
98             if (map instanceof JalistoCache) {
99                 ((JalistoCache) map).setMaxSize(size);
100             }
101         } catch (Exception JavaDoc e) {
102             throw new MBeanException(e, "failed to set PageCacheMaxSize");
103         }
104     }
105
106     public boolean getKeepInMemory() throws MBeanException {
107         try {
108             SessionInternal session = provider.getSessionForMbean(propertiesPath);
109             return session.getFileAccess().getPhysicalAccess().getBaseImage().isKeepInMemory();
110         } catch (Exception JavaDoc e) {
111             throw new MBeanException(e, "failed to get KeepInMemory");
112         }
113     }
114
115     public void setKeepInMemory(boolean keep) throws MBeanException {
116         try {
117             SessionInternal session = provider.getSessionForMbean(propertiesPath);
118             session.getFileAccess().getPhysicalAccess().getBaseImage().setKeepInMemory(keep);
119         } catch(Exception JavaDoc e) {
120             throw new MBeanException(e, "failed to set KeepInMemory");
121         }
122     }
123
124
125     /***************************** DYNAMIC MBEAN IMPLEMENTATION *****************************/
126
127     public MBeanInfo getMBeanInfo() {
128         if (infos == null) {
129             MBeanAttributeInfo[] attributesInfos = new MBeanAttributeInfo[5];
130             attributesInfos[0] = new MBeanAttributeInfo(
131                     "OidTableCurrentSize",
132                     "java.lang.Integer",
133                     "Number of page used in oid table's cache.",
134                     true,
135                     false,
136                     false);
137             attributesInfos[1] = new MBeanAttributeInfo(
138                     "OidTableMaxSize",
139                     "java.lang.Integer",
140                     "Maximum size of oid table's cache.",
141                     true,
142                     true,
143                     false);
144             attributesInfos[2] = new MBeanAttributeInfo(
145                     "PageCacheCurrentSize",
146                     "java.lang.Integer",
147                     "Number of page used in page cache.",
148                     true,
149                     false,
150                     false);
151             attributesInfos[3] = new MBeanAttributeInfo(
152                     "PageCacheMaxSize",
153                     "java.lang.Integer",
154                     "Maximum size of page cache.",
155                     true,
156                     true,
157                     false);
158             attributesInfos[4] = new MBeanAttributeInfo(
159                     "KeepInMemory",
160                     "java.lang.Boolean",
161                     "Tell if readed page are keep in memory after commit or rollback.",
162                     true,
163                     true,
164                     false);
165
166             infos = new MBeanInfo(this.getClass().getName(),
167                                   "Administration MBean to watch caches used by Jalisto",
168                                   attributesInfos,
169                                   new MBeanConstructorInfo[0],
170                                   new MBeanOperationInfo[0],
171                                   new MBeanNotificationInfo[0]);
172         }
173         return infos;
174     }
175
176     protected Object JavaDoc internalGetAttribute(String JavaDoc attributeName)
177             throws AttributeNotFoundException, MBeanException, ReflectionException {
178         if (attributeName.equals("OidTableCurrentSize")) {
179             return new Integer JavaDoc(getOidTableCurrentSize());
180         }
181         if (attributeName.equals("OidTableMaxSize")) {
182             return new Integer JavaDoc(getOidTableMaxSize());
183         }
184         if (attributeName.equals("PageCacheCurrentSize")) {
185             return new Integer JavaDoc(getPageCacheCurrentSize());
186         }
187         if (attributeName.equals("PageCacheMaxSize")) {
188             return new Integer JavaDoc(getPageCacheMaxSize());
189         }
190         if (attributeName.equals("KeepInMemory")) {
191             return new Boolean JavaDoc(getKeepInMemory());
192         }
193
194         throw(new AttributeNotFoundException(
195                 "Cannot find " + attributeName + " attribute in " + this.getClass().getName()));
196     }
197
198     protected boolean internalSetAttribute(String JavaDoc name, Object JavaDoc value)
199             throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
200         if (name.equals("OidTableCurrentSize")) {
201             throw(new AttributeNotFoundException(
202                     "Cannot set attribute " + name + " because it is read-only"));
203         } else if (name.equals("OidTableMaxSize")) {
204             setOidTableMaxSize(((Integer JavaDoc)value).intValue());
205             return true;
206         } else if (name.equals("PageCacheCurrentSize")) {
207             throw(new AttributeNotFoundException(
208                     "Cannot set attribute " + name + " because it is read-only"));
209         } else if (name.equals("PageCacheMaxSize")) {
210             setPageCacheMaxSize(((Integer JavaDoc)value).intValue());
211             return true;
212         } else if (name.equals("KeepInMemory")) {
213             setKeepInMemory(((Boolean JavaDoc)value).booleanValue());
214             return true;
215         }
216         return false;
217     }
218
219     protected Object JavaDoc internalInvoke(String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature)
220             throws MBeanException, ReflectionException {
221         throw new ReflectionException(new NoSuchMethodException JavaDoc(operationName),
222                                       "Cannot find the operation " + operationName +
223                                       " in " + this.getClass().getName());
224     }
225
226
227     private MBeanInfo infos;
228     private String JavaDoc propertiesPath;
229     private SessionProvider provider;
230 }
231
Popular Tags