1 16 package org.jmanage.core.modules; 17 18 import org.jmanage.core.management.*; 19 import org.jmanage.core.management.ObjectName; 20 import org.jmanage.core.management.MalformedObjectNameException; 21 import org.jmanage.core.util.Loggers; 22 23 import javax.management.*; 24 import javax.management.openmbean.CompositeData ; 25 import javax.management.openmbean.TabularData ; 26 import java.util.*; 27 import java.util.logging.Logger ; 28 import java.util.logging.Level ; 29 import java.lang.reflect.Method ; 30 import java.lang.reflect.InvocationTargetException ; 31 import java.io.IOException ; 32 33 38 public abstract class JMXServerConnection implements ServerConnection{ 39 40 private static final Logger logger = 41 Loggers.getLogger(JMXServerConnection.class); 42 43 private Object mbeanServer; 44 private Class mbeanServerClass; 47 48 public JMXServerConnection(){} 49 50 public JMXServerConnection(Object mbeanServer, Class mbeanServerClass){ 51 this.mbeanServer = mbeanServer; 52 this.mbeanServerClass = mbeanServerClass; 53 } 54 55 61 public Set queryNames(ObjectName objectName){ 62 63 Class [] methodSignature = new Class []{javax.management.ObjectName .class, 64 javax.management.QueryExp .class}; 65 Object [] methodArgs = new Object []{toJMXObjectName(objectName), 66 null}; 67 Set mbeans = (Set)callMBeanServer("queryNames", methodSignature, 68 methodArgs); 69 return toJmanageObjectNameInstance(mbeans); 70 } 71 72 73 83 public Object invoke(ObjectName objectName, 84 String operationName, 85 Object [] params, 86 String [] signature) { 87 88 Class [] methodSignature = new Class []{javax.management.ObjectName .class, 89 String .class, 90 new Object [0].getClass(), 91 new String [0].getClass()}; 92 Object [] methodArgs = new Object []{toJMXObjectName(objectName), 93 operationName, 94 params, 95 signature}; 96 return callMBeanServer("invoke", methodSignature, methodArgs); 97 } 98 99 105 public ObjectInfo getObjectInfo(ObjectName objectName) { 106 107 Class [] methodSignature = new Class []{javax.management.ObjectName .class}; 108 Object [] methodArgs = new Object []{toJMXObjectName(objectName)}; 109 MBeanInfo mbeanInfo = (MBeanInfo)callMBeanServer("getMBeanInfo", 110 methodSignature, methodArgs); 111 return toObjectInfo(objectName, mbeanInfo); 112 } 113 114 121 public Object getAttribute(ObjectName objectName, String attributeName){ 122 123 Class [] methodSignature = new Class []{javax.management.ObjectName .class, 124 String .class}; 125 Object [] methodArgs = new Object []{toJMXObjectName(objectName), 126 attributeName}; 127 return callMBeanServer("getAttribute", methodSignature, methodArgs); 128 } 129 130 138 public List getAttributes(ObjectName objectName, String [] attributeNames) { 139 140 Class [] methodSignature = new Class []{javax.management.ObjectName .class, 141 new String [0].getClass()}; 142 Object [] methodArgs = new Object []{toJMXObjectName(objectName), 143 attributeNames}; 144 AttributeList attrList = (AttributeList)callMBeanServer("getAttributes", 145 methodSignature, methodArgs); 146 return toObjectAttributeList(attrList); 147 } 148 149 155 public List setAttributes(ObjectName objectName, List attributeList) { 156 157 Class [] methodSignature = new Class []{javax.management.ObjectName .class, 158 javax.management.AttributeList .class}; 159 Object [] methodArgs = new Object []{toJMXObjectName(objectName), 160 toJMXAttributeList(attributeList)}; 161 AttributeList output = (AttributeList)callMBeanServer("setAttributes", 162 methodSignature, methodArgs); 163 return toObjectAttributeList(output); 164 } 165 166 protected Map notifications = new HashMap(); 169 protected Map notifFilters = new HashMap(); 170 171 public void addNotificationListener(ObjectName objectName, 172 ObjectNotificationListener listener, 173 ObjectNotificationFilter filter, 174 Object handback){ 175 176 NotificationListener notifListener = 177 toJMXNotificationListener(listener); 178 notifications.put(listener, notifListener); 179 NotificationFilter notifFilter = 180 toJMXNotificationFilter(filter); 181 notifFilters.put(filter, notifFilter); 182 183 Class [] methodSignature = new Class []{javax.management.ObjectName .class, 184 NotificationListener.class, 185 NotificationFilter.class, 186 Object .class}; 187 Object [] methodArgs = new Object []{toJMXObjectName(objectName), 188 notifListener, 189 notifFilter, 190 handback}; 191 callMBeanServer("addNotificationListener", methodSignature, methodArgs); 192 } 193 194 public void removeNotificationListener(ObjectName objectName, 195 ObjectNotificationListener listener, 196 ObjectNotificationFilter filter, 197 Object handback){ 198 199 NotificationListener notifListener = 200 (NotificationListener)notifications.remove(listener); 201 NotificationFilter notifFilter = 202 (NotificationFilter)notifFilters.remove(filter); 203 assert notifListener != null; 204 assert notifFilter != null; 205 206 Class [] methodSignature = new Class []{javax.management.ObjectName .class, 207 NotificationListener.class, 208 NotificationFilter.class, 209 Object .class}; 210 Object [] methodArgs = new Object []{toJMXObjectName(objectName), 211 notifListener, 212 notifFilter, 213 handback}; 214 callMBeanServer("removeNotificationListener", methodSignature, methodArgs); 215 } 216 217 public void createMBean(String className, 219 ObjectName name, 220 Object [] params, 221 String [] signature){ 222 Class [] methodSignature = new Class []{String .class, 223 javax.management.ObjectName .class, 224 new Object [0].getClass(), 225 new String [0].getClass()}; 226 Object [] methodArgs = new Object []{className, toJMXObjectName(name), 227 params, signature}; 228 callMBeanServer("createMBean", methodSignature, methodArgs); 229 } 230 231 public void unregisterMBean(ObjectName objectName){ 232 233 Class [] methodSignature = new Class []{javax.management.ObjectName .class}; 234 Object [] methodArgs = new Object []{toJMXObjectName(objectName)}; 235 callMBeanServer("unregisterMBean", methodSignature, methodArgs); 236 } 237 238 public Object buildObjectName(String objectName){ 239 try { 240 return new javax.management.ObjectName (objectName); 241 } catch (javax.management.MalformedObjectNameException e) { 242 throw new RuntimeException (e); 243 } 244 } 245 246 253 public boolean isOpen(){ 254 try { 255 Class [] methodSignature = new Class [0]; 256 Object [] methodArgs = new Object [0]; 257 Integer count = (Integer )callMBeanServer("getMBeanCount", 258 methodSignature, methodArgs); 259 return true; 260 } catch (Exception e) { 261 logger.log(Level.INFO, "Connection to the server is lost. " + 262 "Error message: " + e.getMessage()); 263 return false; 264 } 265 } 266 267 270 public void close() throws IOException { 271 logger.fine("Noop close operation."); 272 } 273 274 277 private Object callMBeanServer(String methodName, 278 Class [] params, 279 Object [] args){ 280 281 try { 282 Method method = mbeanServerClass.getMethod(methodName, params); 283 return method.invoke(mbeanServer, args); 284 } catch (InvocationTargetException e) { 285 if(e.getCause() != null && 286 e.getCause() instanceof RuntimeException ){ 287 throw (RuntimeException )e.getCause(); 288 } 289 throw new RuntimeException (e.getCause()); 290 } catch (Throwable e){ 291 if(e instanceof RuntimeException ){ 292 throw (RuntimeException )e; 293 }else{ 294 throw new RuntimeException (e); 295 } 296 } 297 } 298 299 protected static javax.management.ObjectName 300 toJMXObjectName(ObjectName objectName){ 301 try { 302 return new javax.management.ObjectName (objectName.toString()); 303 } catch (javax.management.MalformedObjectNameException e) { 304 throw new MalformedObjectNameException(e); 305 } 306 } 307 308 protected static ObjectName toJmanageObjectName( 309 javax.management.ObjectName objectName){ 310 return new ObjectName(objectName.toString(), 311 objectName.getCanonicalName()); 312 } 313 314 318 protected static Set toJmanageObjectNameInstance(Set mbeans){ 319 final Set output = new HashSet(mbeans.size()); 320 for(Iterator it=mbeans.iterator(); it.hasNext();){ 321 javax.management.ObjectName objName = 322 (javax.management.ObjectName )it.next(); 323 output.add(toJmanageObjectName(objName)); 324 } 325 return output; 326 } 327 328 protected static ObjectInfo toObjectInfo(ObjectName objectName, 329 MBeanInfo mbeanInfo){ 330 331 ObjectAttributeInfo[] attributes = 332 toObjectAttributes(mbeanInfo.getAttributes()); 333 ObjectConstructorInfo[] constructors = 334 toObjectConstructors(mbeanInfo.getConstructors()); 335 ObjectOperationInfo[] operations = 336 toObjectOperations(mbeanInfo.getOperations()); 337 ObjectNotificationInfo[] notifications = 338 toObjectNotifications(mbeanInfo.getNotifications()); 339 return new ObjectInfo(objectName, mbeanInfo.getClassName(), 340 mbeanInfo.getDescription(), attributes, 341 constructors, operations, notifications); 342 } 343 344 protected static ObjectAttributeInfo[] 345 toObjectAttributes(MBeanAttributeInfo[] attributes){ 346 ObjectAttributeInfo[] objAttributes = 347 new ObjectAttributeInfo[attributes.length]; 348 for(int i=0; i < attributes.length; i++) { 349 objAttributes[i] = toObjectAttributeInfo(attributes[i]); 350 } 351 return objAttributes; 352 } 353 354 protected static ObjectAttributeInfo 355 toObjectAttributeInfo(MBeanAttributeInfo attribute){ 356 357 return new ObjectAttributeInfo(attribute.getName(), 358 attribute.getDescription(), 359 attribute.getType(), 360 attribute.isWritable(), 361 attribute.isReadable(), 362 attribute.isIs()); 363 } 364 365 protected static ObjectConstructorInfo[] 366 toObjectConstructors(MBeanConstructorInfo[] constructors){ 367 ObjectConstructorInfo[] objCtors = 368 new ObjectConstructorInfo[constructors.length]; 369 for(int i=0; i < constructors.length; i++) { 370 objCtors[i] = toObjectConstructorInfo(constructors[i]); 371 } 372 return objCtors; 373 } 374 375 protected static ObjectConstructorInfo 376 toObjectConstructorInfo(MBeanConstructorInfo constructor){ 377 return new ObjectConstructorInfo(constructor.getName(), 378 constructor.getDescription(), 379 toObjectParameters(constructor.getSignature())); 380 } 381 382 protected static ObjectOperationInfo[] 383 toObjectOperations(MBeanOperationInfo[] operations){ 384 ObjectOperationInfo[] objOperations = 385 new ObjectOperationInfo[operations.length]; 386 for(int i=0; i < operations.length; i++) { 387 objOperations[i] = toObjectOperationInfo(operations[i]); 388 } 389 return objOperations; 390 } 391 392 protected static ObjectOperationInfo 393 toObjectOperationInfo(MBeanOperationInfo operation){ 394 return new ObjectOperationInfo(operation.getName(), 395 operation.getDescription(), 396 toObjectParameters(operation.getSignature()), 397 operation.getReturnType(), 398 operation.getImpact()); 399 } 400 401 protected static ObjectNotificationInfo[] 402 toObjectNotifications(MBeanNotificationInfo[] notifications){ 403 ObjectNotificationInfo[] objNotifications = 404 new ObjectNotificationInfo[notifications.length]; 405 for(int i=0; i < notifications.length; i++) { 406 objNotifications[i] = toObjectNotificationInfo(notifications[i]); 407 } 408 return objNotifications; 409 } 410 411 protected static ObjectNotificationInfo 412 toObjectNotificationInfo(MBeanNotificationInfo notification){ 413 return new ObjectNotificationInfo(notification.getNotifTypes(), 414 notification.getName(), 415 notification.getDescription()); 416 } 417 418 protected static ObjectParameterInfo[] 419 toObjectParameters(MBeanParameterInfo[] parameters){ 420 ObjectParameterInfo[] objParameters = 421 new ObjectParameterInfo[parameters.length]; 422 for(int i=0; i < parameters.length; i++) { 423 objParameters[i] = toObjectParameterInfo(parameters[i]); 424 } 425 return objParameters; 426 } 427 428 protected static ObjectParameterInfo 429 toObjectParameterInfo(MBeanParameterInfo parameter){ 430 431 return new ObjectParameterInfo(parameter.getName(), 432 parameter.getDescription(), parameter.getType()); 433 } 434 435 protected static List toObjectAttributeList(AttributeList attrList){ 436 final List objAttrList = new ArrayList(attrList.size()); 437 for(Iterator it=attrList.iterator(); it.hasNext(); ){ 438 Attribute attr = (Attribute)it.next(); 439 objAttrList.add(toObjectAttribute(attr)); 440 } 441 return objAttrList; 442 } 443 444 protected static ObjectAttribute toObjectAttribute(Attribute attr){ 445 Object value = attr.getValue(); 446 if(value != null){ 447 if(value instanceof CompositeData ){ 450 value = JMXInterfaceProxy.newProxyInstance( 451 CompositeData .class, value); 452 }else if(value instanceof TabularData ){ 453 value = JMXInterfaceProxy.newProxyInstance( 454 TabularData .class, value); 455 } 456 } 457 return new ObjectAttribute(attr.getName(), value); 458 } 459 460 protected static AttributeList toJMXAttributeList(List objAttrs){ 461 AttributeList attrList = new AttributeList(objAttrs.size()); 462 for(Iterator it=objAttrs.iterator(); it.hasNext(); ){ 463 attrList.add(toJMXAttribute((ObjectAttribute)it.next())); 464 } 465 return attrList; 466 } 467 468 protected static Attribute toJMXAttribute(ObjectAttribute objAttr){ 469 return new Attribute(objAttr.getName(), objAttr.getValue()); 470 } 471 472 protected static ObjectNotification toObjectNotification( 473 Notification n){ 474 475 return new ObjectNotification(n.getType(), n.getSource(), 476 n.getSequenceNumber(), n.getTimeStamp(), 477 n.getMessage(), n.getUserData()); 478 } 479 480 protected static NotificationListener toJMXNotificationListener( 481 final ObjectNotificationListener listener){ 482 483 return new NotificationListener(){ 484 public void handleNotification(Notification notification, 485 Object handback) { 486 listener.handleNotification(toObjectNotification(notification), 487 handback); 488 } 489 }; 490 } 491 492 protected static NotificationFilter toJMXNotificationFilter( 493 final ObjectNotificationFilter filter){ 494 NotificationFilterSupport notificationFilter = 495 new NotificationFilterSupport(); 496 for(Iterator it=filter.getEnabledTypes().iterator(); it.hasNext();){ 497 notificationFilter.enableType((String )it.next()); 498 } 499 return notificationFilter; 500 } 501 } 502 | Popular Tags |