📄 basemodelmbean.java
字号:
(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");
Object oldValue=null;
//if( getAttMap.get(name) != null )
// oldValue=getAttribute( name );
Method m=managedBean.getSetter(name,this,resource);
try {
if( m.getDeclaringClass().isAssignableFrom( this.getClass()) ) {
m.invoke(this, new Object[] { value });
} else {
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);
} catch(Exception ex) {
log.error("Error sending notification " + name, ex);
}
//attributes.put( name, value );
// if( source != null ) {
// // this mbean is asscoiated with a source - maybe we want to persist
// source.updateField(oname, name, value);
// }
}
public String toString() {
if( resource==null )
return "BaseModelMbean[" + resourceType + "]";
return resource.toString();
}
/**
* Set the values of several attributes of this MBean.
*
* @param attributes THe names and values to be set
*
* @return The list of attributes that were set and their new values
*/
public AttributeList setAttributes(AttributeList attributes) {
AttributeList response = new AttributeList();
// Validate the input parameters
if (attributes == null)
return response;
// Prepare and return our response, eating all exceptions
String names[] = new String[attributes.size()];
int n = 0;
Iterator items = attributes.iterator();
while (items.hasNext()) {
Attribute item = (Attribute) items.next();
names[n++] = item.getName();
try {
setAttribute(item);
} catch (Exception e) {
; // Ignore all exceptions
}
}
return (getAttributes(names));
}
// ----------------------------------------------------- ModelMBean Methods
/**
* Get the instance handle of the object against which we execute
* all methods in this ModelMBean management interface.
*
* @exception InstanceNotFoundException if the managed resource object
* cannot be found
* @exception MBeanException if the initializer of the object throws
* an exception
* @exception RuntimeOperationsException if the managed resource or the
* resource type is <code>null</code> or invalid
*/
public Object getManagedResource()
throws InstanceNotFoundException, InvalidTargetObjectTypeException,
MBeanException, RuntimeOperationsException {
if (resource == null)
throw new RuntimeOperationsException
(new IllegalArgumentException("Managed resource is null"),
"Managed resource is null");
return resource;
}
/**
* Set the instance handle of the object against which we will execute
* all methods in this ModelMBean management interface.
*
* <strike>This method will detect and call "setModelMbean" method. A resource
* can implement this method to get a reference to the model mbean.
* The reference can be used to send notification and access the
* registry.
* </strike> The caller can provide the mbean instance or the object name to
* the resource, if needed.
*
* @param resource The resource object to be managed
* @param type The type of reference for the managed resource
* ("ObjectReference", "Handle", "IOR", "EJBHandle", or
* "RMIReference")
*
* @exception InstanceNotFoundException if the managed resource object
* cannot be found
* @exception InvalidTargetObjectTypeException if this ModelMBean is
* asked to handle a reference type it cannot deal with
* @exception MBeanException if the initializer of the object throws
* an exception
* @exception RuntimeOperationsException if the managed resource or the
* resource type is <code>null</code> or invalid
*/
public void setManagedResource(Object resource, String type)
throws InstanceNotFoundException,
MBeanException, RuntimeOperationsException
{
if (resource == null)
throw new RuntimeOperationsException
(new IllegalArgumentException("Managed resource is null"),
"Managed resource is null");
// if (!"objectreference".equalsIgnoreCase(type))
// throw new InvalidTargetObjectTypeException(type);
this.resource = resource;
this.resourceType = resource.getClass().getName();
// // Make the resource aware of the model mbean.
// try {
// Method m=resource.getClass().getMethod("setModelMBean",
// new Class[] {ModelMBean.class});
// if( m!= null ) {
// m.invoke(resource, new Object[] {this});
// }
// } catch( NoSuchMethodException t ) {
// // ignore
// } catch( Throwable t ) {
// log.error( "Can't set model mbean ", t );
// }
}
// ------------------------------ ModelMBeanNotificationBroadcaster Methods
/**
* Add an attribute change notification event listener to this MBean.
*
* @param listener Listener that will receive event notifications
* @param name Name of the attribute of interest, or <code>null</code>
* to indicate interest in all attributes
* @param handback Handback object to be sent along with event
* notifications
*
* @exception IllegalArgumentException if the listener parameter is null
*/
public void addAttributeChangeNotificationListener
(NotificationListener listener, String name, Object handback)
throws IllegalArgumentException {
if (listener == null)
throw new IllegalArgumentException("Listener is null");
if (attributeBroadcaster == null)
attributeBroadcaster = new BaseNotificationBroadcaster();
if( log.isDebugEnabled() )
log.debug("addAttributeNotificationListener " + listener);
BaseAttributeFilter filter = new BaseAttributeFilter(name);
attributeBroadcaster.addNotificationListener
(listener, filter, handback);
}
/**
* Remove an attribute change notification event listener from
* this MBean.
*
* @param listener The listener to be removed
* @param name The attribute name for which no more events are required
*
*
* @exception ListenerNotFoundException if this listener is not
* registered in the MBean
*/
public void removeAttributeChangeNotificationListener
(NotificationListener listener, String name)
throws ListenerNotFoundException {
if (listener == null)
throw new IllegalArgumentException("Listener is null");
if (attributeBroadcaster == null)
attributeBroadcaster = new BaseNotificationBroadcaster();
// FIXME - currently this removes *all* notifications for this listener
attributeBroadcaster.removeNotificationListener(listener);
}
/**
* Remove an attribute change notification event listener from
* this MBean.
*
* @param listener The listener to be removed
* @param attributeName The attribute name for which no more events are required
* @param handback Handback object to be sent along with event
* notifications
*
*
* @exception ListenerNotFoundException if this listener is not
* registered in the MBean
*/
public void removeAttributeChangeNotificationListener
(NotificationListener listener, String attributeName, Object handback)
throws ListenerNotFoundException {
removeAttributeChangeNotificationListener(listener, attributeName);
}
/**
* Send an <code>AttributeChangeNotification</code> to all registered
* listeners.
*
* @param notification The <code>AttributeChangeNotification</code>
* that will be passed
*
* @exception MBeanException if an object initializer throws an
* exception
* @exception RuntimeOperationsException wraps IllegalArgumentException
* when the specified notification is <code>null</code> or invalid
*/
public void sendAttributeChangeNotification
(AttributeChangeNotification notification)
throws MBeanException, RuntimeOperationsException {
if (notification == null)
throw new RuntimeOperationsException
(new IllegalArgumentException("Notification is null"),
"Notification is null");
if (attributeBroadcaster == null)
return; // This means there are no registered listeners
if( log.isDebugEnabled() )
log.debug( "AttributeChangeNotification " + notification );
attributeBroadcaster.sendNotification(notification);
}
/**
* Send an <code>AttributeChangeNotification</code> to all registered
* listeners.
*
* @param oldValue The original value of the <code>Attribute</code>
* @param newValue The new value of the <code>Attribute</code>
*
* @exception MBeanException if an object initializer throws an
* exception
* @exception RuntimeOperationsException wraps IllegalArgumentException
* when the specified notification is <code>null</code> or invalid
*/
public void sendAttributeChangeNotification
(Attribute oldValue, Attribute newValue)
throws MBeanException, RuntimeOperationsException {
// Calculate the class name for the change notification
String type = null;
if (newValue.getValue() != null)
type = newValue.getValue().getClass().getName();
else if (oldValue.getValue() != null)
type = oldValue.getValue().getClass().getName();
else
return; // Old and new are both null == no change
AttributeChangeNotification notification =
new AttributeChangeNotification
(this, 1, System.currentTimeMillis(),
"Attribute value has changed",
oldValue.getName(), type,
oldValue.getValue(), newValue.getValue());
sendAttributeChangeNotification(notification);
}
/**
* Send a <code>Notification</code> to all registered listeners as a
* <code>jmx.modelmbean.general</code> notification.
*
* @param notification The <code>Notification</code> that will be passed
*
* @exception MBeanException if an object initializer throws an
* exception
* @exception RuntimeOperationsException wraps IllegalArgumentException
* when the specified notification is <code>null</code> or invalid
*/
public void sendNotification(Notification notification)
throws MBeanException, RuntimeOperationsException {
if (notification == null)
throw new RuntimeOperationsException
(new IllegalArgumentException("Notification is null"),
"Notification is null");
if (generalBroadcaster == null)
return; // This means there are no registered listeners
generalBroadcaster.sendNotification(notification);
}
/**
* Send a <code>Notification</code> which contains the specified string
* as a <code>jmx.modelmbean.generic</code> notification.
*
* @param message The message string to be passed
*
* @exception MBeanException if an object initializer throws an
* exception
* @exception RuntimeOperationsException wraps IllegalArgumentException
* when the specified notification is <code>null</code> or invalid
*/
public void sendNotification(String message)
throws MBeanException, RuntimeOperationsException {
if (message == null)
throw new RuntimeOperationsException
(new IllegalArgumentException("Message is null"),
"Message is null");
Notification notification = new Notification
("jmx.modelmbean.generic", this, 1, message);
sendNotification(notification);
}
// ---------------------------------------- NotificationBroadcaster Methods
/**
* Add a notification event listener to this MBean.
*
* @param listener Listener that will receive event notifications
* @param filter Filter object used to filter event notifications
* actually delivered, or <code>null</code> for no filtering
* @param handback Handback object to be sent along with event
* notifications
*
* @exception IllegalArgumentException if the listener parameter is null
*/
public void addNotificationListener(NotificationListener listener,
NotificationFilter filter,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -