1 package org.oddjob.jmx.server; 2 3 import javax.management.MBeanAttributeInfo ; 4 import javax.management.MBeanException ; 5 import javax.management.MBeanNotificationInfo ; 6 import javax.management.MBeanOperationInfo ; 7 import javax.management.MBeanParameterInfo ; 8 import javax.management.Notification ; 9 import javax.management.ReflectionException ; 10 11 import org.apache.commons.beanutils.DynaBean; 12 import org.apache.commons.beanutils.DynaClass; 13 import org.apache.commons.beanutils.PropertyUtils; 14 import org.oddjob.framework.WrapDynaClass; 15 16 18 public class DynaBeanInfo implements InterfaceInfo { 19 20 public Class interfaceClass() { 21 return DynaBean.class; 22 } 23 24 public MBeanAttributeInfo [] getMBeanAttributeInfo() { 25 return new MBeanAttributeInfo [0]; 26 } 27 28 public MBeanOperationInfo [] getMBeanOperationInfo() { 29 return new MBeanOperationInfo [] { 30 new MBeanOperationInfo ("contains", "Contains the mapped property.", 31 new MBeanParameterInfo [] { 32 new MBeanParameterInfo ("property", String .class.getName(), "The property name."), 33 new MBeanParameterInfo ("key", String .class.getName(), "The key.") 34 }, 35 Boolean.TYPE.getName(), 36 MBeanOperationInfo.INFO), 37 new MBeanOperationInfo ("get", "Get a simple property.", 38 new MBeanParameterInfo [] { 39 new MBeanParameterInfo ("property", String .class.getName(), "The property name.") 40 }, 41 Object .class.getName(), 42 MBeanOperationInfo.INFO), 43 new MBeanOperationInfo ("get", "Get an indexed property.", 44 new MBeanParameterInfo [] { 45 new MBeanParameterInfo ("property", String .class.getName(), "The property name."), 46 new MBeanParameterInfo ("index", Integer.TYPE.getName(), "The index.") 47 }, 48 Object .class.getName(), 49 MBeanOperationInfo.INFO), 50 new MBeanOperationInfo ("get", "Get a mapped property.", 51 new MBeanParameterInfo [] { 52 new MBeanParameterInfo ("property", String .class.getName(), "The property name."), 53 new MBeanParameterInfo ("key", String .class.getName(), "The key.") 54 }, 55 Object .class.getName(), 56 MBeanOperationInfo.INFO), 57 new MBeanOperationInfo ("getDynaClass", "Get the DynaClass.", 58 new MBeanParameterInfo [] {}, 59 DynaClass.class.getName(), 60 MBeanOperationInfo.INFO), 61 new MBeanOperationInfo ("remove", "Remove a mapped property.", 62 new MBeanParameterInfo [] { 63 new MBeanParameterInfo ("property", String .class.getName(), "The property name."), 64 new MBeanParameterInfo ("key", String .class.getName(), "The key.") 65 }, 66 Void.TYPE.getName(), 67 MBeanOperationInfo.ACTION), 68 new MBeanOperationInfo ("set", "Set a simple property.", 69 new MBeanParameterInfo [] { 70 new MBeanParameterInfo ("property", String .class.getName(), "The property name."), 71 new MBeanParameterInfo ("value ", Object .class.getName(), "The value.") 72 }, 73 Void.TYPE.getName(), 74 MBeanOperationInfo.ACTION), 75 new MBeanOperationInfo ("set", "Set an indexed property.", 76 new MBeanParameterInfo [] { 77 new MBeanParameterInfo ("property", String .class.getName(), "The property name."), 78 new MBeanParameterInfo ("index", Integer.TYPE.getName(), "The index."), 79 new MBeanParameterInfo ("value ", Object .class.getName(), "The value.") 80 }, 81 Void.TYPE.getName(), 82 MBeanOperationInfo.ACTION), 83 new MBeanOperationInfo ("set", "Set a mapped property.", 84 new MBeanParameterInfo [] { 85 new MBeanParameterInfo ("property", String .class.getName(), "The property name."), 86 new MBeanParameterInfo ("key", String .class.getName(), "The key."), 87 new MBeanParameterInfo ("value ", Object .class.getName(), "The value.") 88 }, 89 Void.TYPE.getName(), 90 MBeanOperationInfo.ACTION), 91 }; 92 } 93 94 public MBeanNotificationInfo [] getMBeanNotificationInfo() { 95 return new MBeanNotificationInfo [0]; 96 } 97 98 99 public InterfaceHandler attach(Object target, OddjobMBean ojmb) { 100 return new DynaBeanHandler(target); 101 } 102 103 class DynaBeanHandler implements InterfaceHandler { 104 105 private final Object bean; 106 107 DynaBeanHandler(Object bean) { 108 this.bean = bean; 109 } 110 111 public Object invoke(String actionName, Object [] params, String [] signature) throws MBeanException , ReflectionException { 112 if ("contains".equals(actionName) && signature.length == 2 113 && String .class.getName().equals(signature[0]) 114 && String .class.getName().equals(signature[1])) { 115 try { 116 return Boolean.valueOf(! 117 (PropertyUtils.getMappedProperty(bean, (String ) params[0], 118 (String ) params[1]) == null)); 119 } catch (Exception e) { 120 throw new MBeanException (e); 121 } 122 } 123 else if ("get".equals(actionName) && signature.length == 1 124 && String .class.getName().equals(signature[0])) { 125 String property = (String ) params[0]; 126 try { 127 return PropertyUtils.getProperty(bean, property); 128 } catch (Exception e) { 129 throw new MBeanException (e); 130 } 131 } 132 else if ("get".equals(actionName) && signature.length == 2 133 && String .class.getName().equals(signature[0]) 134 && Integer .class.getName().equals(signature[1])) { 135 try { 136 return PropertyUtils.getIndexedProperty(bean, (String ) params[0], 137 ((Integer ) params[1]).intValue()); 138 } catch (Exception e) { 139 throw new MBeanException (e); 140 } 141 } 142 else if ("get".equals(actionName) && signature.length == 2 143 && String .class.getName().equals(signature[0]) 144 && String .class.getName().equals(signature[1])) { 145 try { 146 return PropertyUtils.getMappedProperty(bean, (String ) params[0], 147 (String ) params[1]); 148 } catch (Exception e) { 149 throw new MBeanException (e); 150 } 151 } 152 else if ("getDynaClass".equals(actionName) && signature.length == 0) { 153 if (bean instanceof DynaBean) { 154 return ((DynaBean) bean).getDynaClass(); 155 } 156 else { 157 return WrapDynaClass.createDynaClass(bean.getClass()); 158 } 159 } 160 else if ("remove".equals(actionName) && signature.length == 2 161 && String .class.getName().equals(signature[0]) 162 && String .class.getName().equals(signature[1])) { 163 try { 164 PropertyUtils.setMappedProperty(bean, (String ) params[0], 165 (String ) params[1], null); 166 } catch (Exception e) { 167 throw new MBeanException (e); 168 } 169 return Void.TYPE; 170 } 171 else if ("set".equals(actionName) && signature.length == 3 172 && String .class.getName().equals(signature[0]) 173 && Integer .class.getName().equals(signature[1])) { 174 try { 175 PropertyUtils.setIndexedProperty(bean, (String ) params[0], 176 ((Integer ) params[1]).intValue(), params[2]); 177 } catch (Exception e) { 178 throw new MBeanException (e); 179 } 180 return Void.TYPE; 181 } 182 else if ("set".equals(actionName) && signature.length == 2 183 && String .class.getName().equals(signature[0])) { 184 try { 185 PropertyUtils.setProperty(bean, (String ) params[0], 186 params[1]); 187 } catch (Exception e) { 188 throw new MBeanException (e); 189 } 190 return Void.TYPE; 191 } 192 else if ("set".equals(actionName) && signature.length == 3 193 && String .class.getName().equals(signature[0]) 194 && String .class.getName().equals(signature[1])) { 195 try { 196 PropertyUtils.setMappedProperty(bean, (String ) params[0], 197 (String ) params[1], params[2]); 198 } catch (Exception e) { 199 throw new MBeanException (e); 200 } 201 return Void.TYPE; 202 } 203 else { 204 throw new ReflectionException ( 205 new IllegalStateException ("invoked for an unknown method."), 206 actionName); 207 } 208 } 209 210 public Notification [] getLastNotifications() { 211 return null; 212 } 213 214 public void destroy() { 215 } 216 } 217 } | Popular Tags |