KickJava   Java API By Example, From Geeks To Geeks.

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


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.JalistoFactory;
27 import org.objectweb.jalisto.se.api.ClassDescription;
28 import org.objectweb.jalisto.se.api.MetaRepository;
29 import org.objectweb.jalisto.se.api.Session;
30 import org.objectweb.jalisto.se.api.query.FieldDescription;
31 import org.objectweb.jalisto.se.impl.meta.type.MetaTypeFactory;
32 import org.objectweb.jalisto.se.jmx.exception.MBeanAdminException;
33
34 import javax.management.*;
35 import java.util.Iterator JavaDoc;
36
37 public class ClassDescriptionAdmin extends JalistoAbstractDynamicMBean {
38
39     public ClassDescriptionAdmin(String JavaDoc propertiesPath) {
40         this.propertiesPath = propertiesPath;
41         this.provider = SessionProvider.getInstance();
42     }
43
44     public String JavaDoc getClassDescription(String JavaDoc filter) {
45         try {
46             ClassDescription meta = internalGetClassDescription(filter);
47             return meta.toStringFull(0);
48         } catch (MBeanAdminException JalistoAdminExc) {
49             return JalistoAdminExc.getMessage();
50         }
51     }
52
53     public int getNumberOfPersistentClass() throws MBeanException {
54         Session session = null;
55         try {
56             session = provider.getSessionForMbean(propertiesPath);
57             session.openSession();
58             return session.getMetaRepository().getAllClassNames().size();
59         } catch(Exception JavaDoc e) {
60             e.printStackTrace();
61             throw new MBeanException(e, "failed to get NumberOfPersitentClass");
62         } finally {
63             if (session != null ) session.closeSession();
64         }
65     }
66
67     public String JavaDoc getPersistentClassNames() throws MBeanException {
68         Session session = null;
69         try {
70             session = provider.getSessionForMbean(propertiesPath);
71             session.openSession();
72             return session.getMetaRepository().getAllClassNames().toString();
73         } catch(Exception JavaDoc e) {
74             e.printStackTrace();
75             throw new MBeanException(e, "failed to get PersitentClassNames");
76         } finally {
77             if (session != null ) session.closeSession();
78         }
79     }
80
81     public String JavaDoc defineClass(String JavaDoc className) {
82         try {
83             internalGetClassDescription(className);
84         } catch (MBeanAdminException JalistoAdminExc) {
85             if (JalistoAdminExc.getMessage().startsWith("No persistent class available")) {
86                 Session session = provider.getSessionForMbean(propertiesPath);
87                 session.getMetaRepository().defineClass(session, JalistoFactory.createClassDescription(className));
88                 return session.getMetaRepository().getMetaData(className).toStringFull(0);
89             } else {
90                 throw JalistoAdminExc;
91             }
92         }
93         throw new MBeanAdminException("Class "+className+" is already define !");
94     }
95
96     public String JavaDoc removeClass(String JavaDoc classNameFilter) {
97         ClassDescription meta = internalGetClassDescription(classNameFilter);
98         Session session = provider.getSessionForMbean(propertiesPath);
99         session.getMetaRepository().removeClass(session, meta.getClassName());
100         return "Class "+meta.getClassName()+" has been removed";
101     }
102
103     public String JavaDoc addField(String JavaDoc classNameFilter, String JavaDoc fieldName, String JavaDoc fieldType, Short JavaDoc index) {
104         ClassDescription meta = internalGetClassDescription(classNameFilter);
105         FieldDescription fieldDescription = JalistoFactory.createFieldDescription(fieldName, MetaTypeFactory.getMetaType(fieldType));
106         fieldDescription.setIndex(index.shortValue());
107         Session session = provider.getSessionForMbean(propertiesPath);
108         session.getMetaRepository().addField(session, meta.getClassName(), fieldDescription);
109         return session.getMetaRepository().getMetaData(meta.getClassName()).toStringFull(0);
110     }
111
112     public String JavaDoc removeField(String JavaDoc classNameFilter, String JavaDoc fieldName) {
113         ClassDescription meta = internalGetClassDescription(classNameFilter);
114         Session session = provider.getSessionForMbean(propertiesPath);
115         session.getMetaRepository().removeField(session, meta.getClassName(), "");
116         return session.getMetaRepository().getMetaData(meta.getClassName()).toStringFull(0);
117     }
118
119     private ClassDescription internalGetClassDescription(String JavaDoc filter) {
120         MetaRepository repository = provider.getSessionForMbean(propertiesPath).getMetaRepository();
121         Iterator JavaDoc names = repository.getAllClassNames().iterator();
122         ClassDescription meta = null;
123         while (names.hasNext()) {
124             String JavaDoc name = (String JavaDoc) names.next();
125             if (name.indexOf(filter) != -1) {
126                 if (meta == null) {
127                     meta = repository.getMetaData(name);
128                 } else {
129                     throw new MBeanAdminException("More than one persistent class available with filter "+filter);
130                 }
131             }
132         }
133         if (meta == null) {
134             throw new MBeanAdminException("No persistent class available with filter "+filter);
135         }
136         return meta;
137     }
138
139
140     /***************************** DYNAMIC MBEAN IMPLEMENTATION *****************************/
141
142     public MBeanInfo getMBeanInfo() {
143         if (infos == null) {
144             MBeanAttributeInfo[] attributesInfos = new MBeanAttributeInfo[2];
145             attributesInfos[0] = new MBeanAttributeInfo(
146                     "NumberOfPersistentClass",
147                     "java.lang.Integer",
148                     "Number of persistent class defined in Jalisto instance.",
149                     true,
150                     false,
151                     false);
152             attributesInfos[1] = new MBeanAttributeInfo(
153                     "PersistentClassNames",
154                     "java.lang.String",
155                     "Persistent class names defined in Jalisto instance.",
156                     true,
157                     false,
158                     false);
159
160             MBeanOperationInfo[] operationsInfos = new MBeanOperationInfo[5];
161             operationsInfos[0] = new MBeanOperationInfo(
162                     "getClassDescription",
163                     "Get String representation of the persistent class's metadata.",
164                     new MBeanParameterInfo[]{new MBeanParameterInfo("classNameFilter", "java.lang.String", null)},
165                     "java.lang.String",
166                     MBeanOperationInfo.INFO);
167             operationsInfos[1] = new MBeanOperationInfo(
168                     "defineClass",
169                     "Define a new persistent class in base, only with the class name.",
170                     new MBeanParameterInfo[]{new MBeanParameterInfo("fullClassName", "java.lang.String", null)},
171                     "java.lang.String",
172                     MBeanOperationInfo.ACTION);
173             operationsInfos[2] = new MBeanOperationInfo(
174                     "removeClass",
175                     "Remove a persistent class from base.",
176                     new MBeanParameterInfo[]{new MBeanParameterInfo("classNameFilter", "java.lang.String", null)},
177                     "java.lang.String",
178                     MBeanOperationInfo.ACTION);
179             operationsInfos[3] = new MBeanOperationInfo(
180                     "addField",
181                     "Add a field to a persistent class.",
182                     new MBeanParameterInfo[]{
183                         new MBeanParameterInfo("classNameFilter", "java.lang.String", null),
184                         new MBeanParameterInfo("fieldName", "java.lang.String", null),
185                         new MBeanParameterInfo("fieldType", "java.lang.String", null),
186                         new MBeanParameterInfo("index", "java.lang.Short", null)},
187                     "java.lang.String",
188                     MBeanOperationInfo.ACTION);
189             operationsInfos[4] = new MBeanOperationInfo(
190                     "removeField",
191                     "remove a field from a persistent class.",
192                     new MBeanParameterInfo[]{
193                         new MBeanParameterInfo("classNameFilter", "java.lang.String", null),
194                         new MBeanParameterInfo("fieldName", "java.lang.String", null)},
195                     "java.lang.String",
196                     MBeanOperationInfo.ACTION);
197
198             infos = new MBeanInfo(this.getClass().getName(),
199                                   "Administration MBean for the Jalisto persistent class metadatas",
200                                   attributesInfos,
201                                   new MBeanConstructorInfo[0],
202                                   operationsInfos,
203                                   new MBeanNotificationInfo[0]);
204         }
205         return infos;
206     }
207
208     protected Object JavaDoc internalGetAttribute(String JavaDoc attributeName)
209             throws AttributeNotFoundException, MBeanException, ReflectionException {
210         if (attributeName.equals("NumberOfPersistentClass")) {
211             return new Integer JavaDoc(getNumberOfPersistentClass());
212         }
213         if (attributeName.equals("PersistentClassNames")) {
214             return getPersistentClassNames();
215         }
216
217         throw(new AttributeNotFoundException(
218                 "Cannot find " + attributeName + " attribute in " + this.getClass().getName()));
219     }
220
221     protected boolean internalSetAttribute(String JavaDoc name, Object JavaDoc value)
222             throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
223         if (name.equals("NumberOfPersistentClass")) {
224             throw(new AttributeNotFoundException(
225                     "Cannot set attribute " + name + " because it is read-only"));
226         } else if (name.equals("PersistentClassNames")) {
227             throw(new AttributeNotFoundException(
228                     "Cannot set attribute " + name + " because it is read-only"));
229         }
230         return false;
231     }
232
233     protected Object JavaDoc internalInvoke(String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature)
234             throws MBeanException, ReflectionException {
235         if (operationName.equals("getClassDescription")) {
236             return getClassDescription((String JavaDoc) params[0]);
237         } else if (operationName.equals("defineClass")) {
238             return defineClass((String JavaDoc) params[0]);
239         } else if (operationName.equals("removeClass")) {
240             return removeClass((String JavaDoc) params[0]);
241         } else if (operationName.equals("addField")) {
242             return addField((String JavaDoc) params[0],
243                             (String JavaDoc) params[1],
244                             (String JavaDoc) params[2],
245                             (Short JavaDoc) params[3]);
246         } else if (operationName.equals("removeField")) {
247             return removeField((String JavaDoc) params[0],
248                                (String JavaDoc) params[1]);
249         } else {
250             throw new ReflectionException(new NoSuchMethodException JavaDoc(operationName),
251                                           "Cannot find the operation " + operationName +
252                                           " in " + this.getClass().getName());
253         }
254     }
255
256
257     private String JavaDoc propertiesPath;
258     private SessionProvider provider;
259     private MBeanInfo infos;
260 }
261
Popular Tags