if( (resource instanceof DynamicMBean) &&
! ( resource instanceof BaseModelMBean )) {
try {
((DynamicMBean)resource).setAttribute(attribute);
} catch (InvalidAttributeValueException e) {
throw new MBeanException(e);
}
return;
}
// Validate the input parameters
if (attribute == null)
throw new RuntimeOperationsException
(new IllegalArgumentException("Attribute is null"),
"Attribute is null");
String name = attribute.getName();
Object value = attribute.getValue();
if (name == null)
throw new RuntimeOperationsException
(new IllegalArgumentException("Attribute name is null"),
"Attribute name is null");
ModelMBeanAttributeInfo attrInfo=info.getAttribute(name);
if (attrInfo == null)
throw new AttributeNotFoundException("Cannot find attribute " + name);
Descriptor attrDesc=attrInfo.getDescriptor();
if (attrDesc == null)
throw new AttributeNotFoundException("Cannot find attribute " + name + " descriptor");
Object oldValue=null;
if( getAttMap.get(name) != null )
oldValue=getAttribute( name );
// Extract the method from cache
Method m=(Method)setAttMap.get( name );
if( m==null ) {
// Look up the actual operation to be used
String setMethod = (String) attrDesc.getFieldValue("setMethod");
if (setMethod == null)
throw new AttributeNotFoundException("Cannot find attribute " + name + " set method name");
String argType=attrInfo.getType();
Class signature[] = new Class[] { getAttributeClass( argType ) };
Object object = null;
NoSuchMethodException exception = null;
try {
object = this;
m = object.getClass().getMethod(setMethod, signature);
} catch (NoSuchMethodException e) {
exception = e;;
}
if( m== null && resource != null ) {
try {
object = resource;
m = object.getClass().getMethod(setMethod, signature);
exception=null;
} catch (NoSuchMethodException e) {
if( log.isDebugEnabled())
log.debug("Method not found in resource " +resource);
exception = e;
}
}
if( exception != null )
throw new ReflectionException(exception,
"Cannot find setter method " + setMethod +
" " + resource);
setAttMap.put( name, m );
}
Object result = null;
try {
if( m.getDeclaringClass().isAssignableFrom( this.getClass()) ) {
result = m.invoke(this, new Object[] { value });
} else {
result = m.invoke(resource, new Object[] { value });
}
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t == null)
t = e;
if (t instanceof RuntimeException)
throw new RuntimeOperationsException
((RuntimeException) t, "Exception invoking method " + name);
else if (t instanceof Error)
throw new RuntimeErrorException
((Error) t, "Error invoking method " + name);
else
throw new MBeanException
(e, "Exception invoking method " + name);
} catch (Exception e) {
log.error("Exception invoking method " + name , e );
throw new MBeanException
(e, "Exception invoking method " + name);
}
try {
sendAttributeChangeNotification(new Attribute( name, oldValue),
attribute);