1 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 ; 36 37 public class ClassDescriptionAdmin extends JalistoAbstractDynamicMBean { 38 39 public ClassDescriptionAdmin(String propertiesPath) { 40 this.propertiesPath = propertiesPath; 41 this.provider = SessionProvider.getInstance(); 42 } 43 44 public String getClassDescription(String 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 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 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 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 defineClass(String 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 removeClass(String 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 addField(String classNameFilter, String fieldName, String fieldType, Short 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 removeField(String classNameFilter, String 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 filter) { 120 MetaRepository repository = provider.getSessionForMbean(propertiesPath).getMetaRepository(); 121 Iterator names = repository.getAllClassNames().iterator(); 122 ClassDescription meta = null; 123 while (names.hasNext()) { 124 String name = (String ) 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 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 internalGetAttribute(String attributeName) 209 throws AttributeNotFoundException, MBeanException, ReflectionException { 210 if (attributeName.equals("NumberOfPersistentClass")) { 211 return new Integer (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 name, Object 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 internalInvoke(String operationName, Object [] params, String [] signature) 234 throws MBeanException, ReflectionException { 235 if (operationName.equals("getClassDescription")) { 236 return getClassDescription((String ) params[0]); 237 } else if (operationName.equals("defineClass")) { 238 return defineClass((String ) params[0]); 239 } else if (operationName.equals("removeClass")) { 240 return removeClass((String ) params[0]); 241 } else if (operationName.equals("addField")) { 242 return addField((String ) params[0], 243 (String ) params[1], 244 (String ) params[2], 245 (Short ) params[3]); 246 } else if (operationName.equals("removeField")) { 247 return removeField((String ) params[0], 248 (String ) params[1]); 249 } else { 250 throw new ReflectionException(new NoSuchMethodException (operationName), 251 "Cannot find the operation " + operationName + 252 " in " + this.getClass().getName()); 253 } 254 } 255 256 257 private String propertiesPath; 258 private SessionProvider provider; 259 private MBeanInfo infos; 260 } 261 | Popular Tags |