1 16 17 package org.springframework.jmx.export.metadata; 18 19 import java.beans.PropertyDescriptor ; 20 import java.lang.reflect.Method ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 24 import org.springframework.beans.BeanUtils; 25 import org.springframework.beans.factory.InitializingBean; 26 import org.springframework.metadata.Attributes; 27 import org.springframework.util.Assert; 28 29 41 public class AttributesJmxAttributeSource implements JmxAttributeSource, InitializingBean { 42 43 46 private Attributes attributes; 47 48 49 53 public AttributesJmxAttributeSource() { 54 } 55 56 61 public AttributesJmxAttributeSource(Attributes attributes) { 62 if (attributes == null) { 63 throw new IllegalArgumentException ("Attributes is required"); 64 } 65 this.attributes = attributes; 66 } 67 68 72 public void setAttributes(Attributes attributes) { 73 this.attributes = attributes; 74 } 75 76 public void afterPropertiesSet() { 77 if (this.attributes == null) { 78 throw new IllegalArgumentException ("'attributes' is required"); 79 } 80 } 81 82 83 90 public ManagedResource getManagedResource(Class clazz) { 91 Assert.notNull(this.attributes, "'attributes' is required"); 92 Collection attrs = this.attributes.getAttributes(clazz, ManagedResource.class); 93 if (attrs.isEmpty()) { 94 return null; 95 } 96 else if (attrs.size() == 1) { 97 return (ManagedResource) attrs.iterator().next(); 98 } 99 else { 100 throw new InvalidMetadataException("A Class can have only one ManagedResource attribute"); 101 } 102 } 103 104 112 public ManagedAttribute getManagedAttribute(Method method) throws InvalidMetadataException { 113 Assert.notNull(this.attributes, "'attributes' is required"); 114 PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method); 115 if (pd == null) { 116 throw new InvalidMetadataException( 117 "The ManagedAttribute attribute is only valid for JavaBean properties: " + 118 "use ManagedOperation for methods"); 119 } 120 Collection attrs = this.attributes.getAttributes(method, ManagedAttribute.class); 121 if (attrs.isEmpty()) { 122 return null; 123 } 124 else if (attrs.size() == 1) { 125 return (ManagedAttribute) attrs.iterator().next(); 126 } 127 else { 128 throw new InvalidMetadataException("A Method can have only one ManagedAttribute attribute"); 129 } 130 } 131 132 140 public ManagedOperation getManagedOperation(Method method) { 141 Assert.notNull(this.attributes, "'attributes' is required"); 142 PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method); 143 if (pd != null) { 144 throw new InvalidMetadataException( 145 "The ManagedOperation attribute is not valid for JavaBean properties: " + 146 "use ManagedAttribute instead"); 147 } 148 Collection attrs = this.attributes.getAttributes(method, ManagedOperation.class); 149 if (attrs.isEmpty()) { 150 return null; 151 } 152 else if (attrs.size() == 1) { 153 return (ManagedOperation) attrs.iterator().next(); 154 } 155 else { 156 throw new InvalidMetadataException("A Method can have only one ManagedAttribute attribute"); 157 } 158 } 159 160 168 public ManagedOperationParameter[] getManagedOperationParameters(Method method) 169 throws InvalidMetadataException { 170 171 Assert.notNull(this.attributes, "'attributes' is required"); 172 Collection attrs = this.attributes.getAttributes(method, ManagedOperationParameter.class); 173 if (attrs.size() == 0) { 174 return new ManagedOperationParameter[0]; 175 } 176 else if (attrs.size() != method.getParameterTypes().length) { 177 throw new InvalidMetadataException( 178 "Method [" + method + "] has an incorrect number of ManagedOperationParameters specified"); 179 } 180 else { 181 ManagedOperationParameter[] params = new ManagedOperationParameter[attrs.size()]; 182 for (Iterator it = attrs.iterator(); it.hasNext();) { 183 ManagedOperationParameter param = (ManagedOperationParameter) it.next(); 184 if (param.getIndex() < 0 || param.getIndex() >= params.length) { 185 throw new InvalidMetadataException( 186 "ManagedOperationParameter index for [" + param.getName() + "] is out of bounds"); 187 } 188 params[param.getIndex()] = param; 189 } 190 return params; 191 } 192 } 193 194 198 public ManagedNotification[] getManagedNotifications(Class clazz) { 199 Assert.notNull(this.attributes, "'attributes' is required"); 200 Collection attrs = this.attributes.getAttributes(clazz, ManagedNotification.class); 201 return attrs.isEmpty() ? new ManagedNotification[0] : (ManagedNotification[]) attrs.toArray(new ManagedNotification[attrs.size()]); 202 } 203 } 204 | Popular Tags |