1 16 17 package org.springframework.jmx.export.assembler; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import javax.management.modelmbean.ModelMBeanNotificationInfo ; 27 28 import org.springframework.jmx.export.metadata.JmxMetadataUtils; 29 import org.springframework.jmx.export.metadata.ManagedNotification; 30 import org.springframework.util.StringUtils; 31 32 40 public abstract class AbstractConfigurableMBeanInfoAssembler extends AbstractReflectiveMBeanInfoAssembler { 41 42 private ModelMBeanNotificationInfo [] notificationInfos; 43 44 private final Map notificationInfoMappings = new HashMap (); 45 46 47 public void setNotificationInfos(ManagedNotification[] notificationInfos) { 48 ModelMBeanNotificationInfo [] infos = new ModelMBeanNotificationInfo [notificationInfos.length]; 49 for (int i = 0; i < notificationInfos.length; i++) { 50 ManagedNotification notificationInfo = notificationInfos[i]; 51 infos[i] = JmxMetadataUtils.convertToModelMBeanNotificationInfo(notificationInfo); 52 } 53 this.notificationInfos = infos; 54 } 55 56 public void setNotificationInfoMappings(Map notificationInfoMappings) { 57 Iterator entries = notificationInfoMappings.entrySet().iterator(); 58 while (entries.hasNext()) { 59 Map.Entry entry = (Map.Entry ) entries.next(); 60 if (!(entry.getKey() instanceof String )) { 61 throw new IllegalArgumentException ("Property [notificationInfoMappings] only accepts Strings for Map keys"); 62 } 63 this.notificationInfoMappings.put(entry.getKey(), extractNotificationMetadata(entry.getValue())); 64 } 65 } 66 67 68 protected ModelMBeanNotificationInfo [] getNotificationInfo(Object managedBean, String beanKey) { 69 ModelMBeanNotificationInfo [] result = null; 70 71 if (StringUtils.hasText(beanKey)) { 72 result = (ModelMBeanNotificationInfo []) this.notificationInfoMappings.get(beanKey); 73 } 74 75 if (result == null) { 76 result = this.notificationInfos; 77 } 78 79 return (result == null) ? new ModelMBeanNotificationInfo [0] : result; 80 } 81 82 private ModelMBeanNotificationInfo [] extractNotificationMetadata(Object mapValue) { 83 if (mapValue instanceof ManagedNotification) { 84 ManagedNotification mn = (ManagedNotification) mapValue; 85 return new ModelMBeanNotificationInfo [] {JmxMetadataUtils.convertToModelMBeanNotificationInfo(mn)}; 86 } 87 else if (mapValue instanceof Collection ) { 88 Collection col = (Collection ) mapValue; 89 List result = new ArrayList (); 90 for (Iterator iterator = col.iterator(); iterator.hasNext();) { 91 Object colValue = iterator.next(); 92 if (!(colValue instanceof ManagedNotification)) { 93 throw new IllegalArgumentException ( 94 "Property 'notificationInfoMappings' only accepts ManagedNotifications for Map values"); 95 } 96 ManagedNotification mn = (ManagedNotification) colValue; 97 result.add(JmxMetadataUtils.convertToModelMBeanNotificationInfo(mn)); 98 } 99 return (ModelMBeanNotificationInfo []) result.toArray(new ModelMBeanNotificationInfo [result.size()]); 100 } 101 else { 102 throw new IllegalArgumentException ( 103 "Property 'notificationInfoMappings' only accepts ManagedNotifications for Map values"); 104 } 105 } 106 107 } 108 | Popular Tags |