| Method from org.apache.commons.modeler.BaseModelMBean Detail: |
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);
}
Add an attribute change notification event listener to this MBean. |
public void addNotificationListener(NotificationListener listener,
NotificationFilter filter,
Object handback) throws IllegalArgumentException {
if (listener == null)
throw new IllegalArgumentException("Listener is null");
if( log.isDebugEnabled() ) log.debug("addNotificationListener " + listener);
if (generalBroadcaster == null)
generalBroadcaster = new BaseNotificationBroadcaster();
generalBroadcaster.addNotificationListener
(listener, filter, handback);
// We'll send the attribute change notifications to all listeners ( who care )
// The normal filtering can be used.
// The problem is that there is no other way to add attribute change listeners
// to a model mbean ( AFAIK ). I suppose the spec should be fixed.
if (attributeBroadcaster == null)
attributeBroadcaster = new BaseNotificationBroadcaster();
if( log.isDebugEnabled() )
log.debug("addAttributeNotificationListener " + listener);
attributeBroadcaster.addNotificationListener
(listener, filter, handback);
}
Add a notification event listener to this MBean. |
protected ModelMBeanInfo createDefaultModelMBeanInfo() {
return (new ModelMBeanInfoSupport(this.getClass().getName(),
"Default ModelMBean",
null, null, null, null));
}
Create and return a default ModelMBeanInfo object. |
protected void createResource() {
try {
//Thread.currentThread().setContextClassLoader(BaseModelMBean.class.getClassLoader());
Class c=null;
try {
c=Class.forName( resourceType );
resource = c.newInstance();
} catch( Throwable t ) {
log.error( "Error creating class " + t);
}
} catch( Throwable ex) {
log.error( "TCL: " + Thread.currentThread().getContextClassLoader(),
ex);
}
}
Set the type of the mbean. This is used as a key to locate
the description in the Registry. |
public Object getAttribute(String name) throws ReflectionException, AttributeNotFoundException, MBeanException {
// Validate the input parameters
if (name == null)
throw new RuntimeOperationsException
(new IllegalArgumentException("Attribute name is null"),
"Attribute name is null");
if( (resource instanceof DynamicMBean) &&
! ( resource instanceof BaseModelMBean )) {
return ((DynamicMBean)resource).getAttribute(name);
}
// Extract the method from cache
Method m=(Method)getAttMap.get( name );
if( m==null ) {
// Look up the actual operation to be used
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");
String getMethod = (String) attrDesc.getFieldValue("getMethod");
if (getMethod == null)
throw new AttributeNotFoundException("Cannot find attribute " + name + " get method name");
Object object = null;
NoSuchMethodException exception = null;
try {
object = this;
m = object.getClass().getMethod(getMethod, NO_ARGS_PARAM_SIG);
} catch (NoSuchMethodException e) {
exception = e;;
}
if( m== null && resource != null ) {
try {
object = resource;
m = object.getClass().getMethod(getMethod, NO_ARGS_PARAM_SIG);
exception=null;
} catch (NoSuchMethodException e) {
exception = e;
}
}
if( exception != null )
throw new ReflectionException(exception,
"Cannot find getter method " + getMethod);
getAttMap.put( name, m );
}
Object result = null;
try {
Class declaring=m.getDeclaringClass();
// workaround for catalina weird mbeans - the declaring class is BaseModelMBean.
// but this is the catalina class.
if( declaring.isAssignableFrom(this.getClass()) ) {
result = m.invoke(this, NO_ARGS_PARAM );
} else {
result = m.invoke(resource, NO_ARGS_PARAM );
}
} 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) {
throw new MBeanException
(e, "Exception invoking method " + name);
}
// Return the results of this method invocation
// FIXME - should we validate the return type?
return (result);
}
Obtain and return the value of a specific attribute of this MBean. |
public AttributeList getAttributes(String[] names) {
// Validate the input parameters
if (names == null)
throw new RuntimeOperationsException
(new IllegalArgumentException("Attribute names list is null"),
"Attribute names list is null");
// Prepare our response, eating all exceptions
AttributeList response = new AttributeList();
for (int i = 0; i < names.length; i++) {
try {
response.add(new Attribute(names[i],getAttribute(names[i])));
} catch (Exception e) {
; // Not having a particular attribute in the response
; // is the indication of a getter problem
}
}
return (response);
}
Obtain and return the values of several attributes of this MBean. |
public String getClassName() {
return getModelerType();
}
|
public ObjectName getJmxName() {
return oname;
}
|
public MBeanInfo getMBeanInfo() {
// XXX Why do we have to clone ?
if( info== null ) return null;
return ((MBeanInfo) info.clone());
}
Return the MBeanInfo object for this MBean. |
public Object getManagedResource() throws InstanceNotFoundException, InvalidTargetObjectTypeException, RuntimeOperationsException, MBeanException {
if (resource == null)
throw new RuntimeOperationsException
(new IllegalArgumentException("Managed resource is null"),
"Managed resource is null");
return resource;
}
Get the instance handle of the object against which we execute
all methods in this ModelMBean management interface. |
public String getModelerType() {
return resourceType;
}
|
public MBeanNotificationInfo[] getNotificationInfo() {
// Acquire the set of application notifications
MBeanNotificationInfo current[] = info.getNotifications();
if (current == null)
current = new MBeanNotificationInfo[0];
MBeanNotificationInfo response[] =
new MBeanNotificationInfo[current.length + 2];
Descriptor descriptor = null;
// Fill in entry for general notifications
descriptor = new DescriptorSupport
(new String[] { "name=GENERIC",
"descriptorType=notification",
"log=T",
"severity=5",
"displayName=jmx.modelmbean.generic" });
response[0] = new ModelMBeanNotificationInfo
(new String[] { "jmx.modelmbean.generic" },
"GENERIC",
"Text message notification from the managed resource",
descriptor);
// Fill in entry for attribute change notifications
descriptor = new DescriptorSupport
(new String[] { "name=ATTRIBUTE_CHANGE",
"descriptorType=notification",
"log=T",
"severity=5",
"displayName=jmx.attribute.change" });
response[1] = new ModelMBeanNotificationInfo
(new String[] { "jmx.attribute.change" },
"ATTRIBUTE_CHANGE",
"Observed MBean attribute value has changed",
descriptor);
// Copy remaining notifications as reported by the application
System.arraycopy(current, 0, response, 2, current.length);
return (response);
}
Return an MBeanNotificationInfo object describing the
notifications sent by this MBean. |
public String getObjectName() {
if (oname != null) {
return oname.toString();
} else {
return null;
}
}
|
public Registry getRegistry() {
// XXX Need a better solution - to avoid the static
if( registry == null )
registry=Registry.getRegistry();
return registry;
}
|
protected void initModelInfo(String type) {
try {
if( log.isDebugEnabled())
log.debug("setModeledType " + type);
log.debug( "Set model Info " + type);
if(type==null) {
return;
}
resourceType=type;
//Thread.currentThread().setContextClassLoader(BaseModelMBean.class.getClassLoader());
Class c=null;
try {
c=Class.forName( type);
} catch( Throwable t ) {
log.debug( "Error creating class " + t);
}
// The class c doesn't need to exist
ManagedBean descriptor=getRegistry().findManagedBean(c, type);
if( descriptor==null )
return;
this.setModelMBeanInfo(descriptor.createMBeanInfo());
} catch( Throwable ex) {
log.error( "TCL: " + Thread.currentThread().getContextClassLoader(),
ex);
}
}
Set the type of the mbean. This is used as a key to locate
the description in the Registry. |
public Object invoke(String name,
Object[] params,
String[] signature) throws ReflectionException, MBeanException {
if( (resource instanceof DynamicMBean) &&
! ( resource instanceof BaseModelMBean )) {
return ((DynamicMBean)resource).invoke(name, params, signature);
}
// Validate the input parameters
if (name == null)
throw new RuntimeOperationsException
(new IllegalArgumentException("Method name is null"),
"Method name is null");
if( log.isDebugEnabled()) log.debug("Invoke " + name);
MethodKey mkey = new MethodKey(name, signature);
Method method=(Method)invokeAttMap.get(mkey);
if( method==null ) {
if (params == null)
params = new Object[0];
if (signature == null)
signature = new String[0];
if (params.length != signature.length)
throw new RuntimeOperationsException
(new IllegalArgumentException("Inconsistent arguments and signature"),
"Inconsistent arguments and signature");
// Acquire the ModelMBeanOperationInfo information for
// the requested operation
ModelMBeanOperationInfo opInfo = info.getOperation(name);
if (opInfo == null)
throw new MBeanException
(new ServiceNotFoundException("Cannot find operation " + name),
"Cannot find operation " + name);
// Prepare the signature required by Java reflection APIs
// FIXME - should we use the signature from opInfo?
Class types[] = new Class[signature.length];
for (int i = 0; i < signature.length; i++) {
types[i]=getAttributeClass( signature[i] );
}
// Locate the method to be invoked, either in this MBean itself
// or in the corresponding managed resource
// FIXME - Accessible methods in superinterfaces?
Object object = null;
Exception exception = null;
try {
object = this;
method = object.getClass().getMethod(name, types);
} catch (NoSuchMethodException e) {
exception = e;;
}
try {
if ((method == null) && (resource != null)) {
object = resource;
method = object.getClass().getMethod(name, types);
}
} catch (NoSuchMethodException e) {
exception = e;
}
if (method == null) {
throw new ReflectionException(exception,
"Cannot find method " + name +
" with this signature");
}
invokeAttMap.put( mkey, method );
}
// Invoke the selected method on the appropriate object
Object result = null;
try {
if( method.getDeclaringClass().isAssignableFrom( this.getClass()) ) {
result = method.invoke(this, params );
} else {
result = method.invoke(resource, params);
}
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
log.error("Exception invoking method " + name , t );
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
((Exception)t, "Exception invoking method " + name);
} catch (Exception e) {
log.error("Exception invoking method " + name , e );
throw new MBeanException
(e, "Exception invoking method " + name);
}
// Return the results of this method invocation
// FIXME - should we validate the return type?
return (result);
}
Invoke a particular method on this MBean, and return any returned
value.
IMPLEMENTATION NOTE - This implementation will
attempt to invoke this method on the MBean itself, or (if not
available) on the managed resource object associated with this
MBean. |
protected boolean isModelMBeanInfoValid(ModelMBeanInfo info) {
return (true);
}
Is the specified ModelMBeanInfo instance valid?
IMPLEMENTATION NOTE - This implementation
does not check anything, but this method can be overridden
as required. |
public void load() throws InstanceNotFoundException, RuntimeOperationsException, MBeanException {
// XXX If a context was set, use it to load the data
throw new MBeanException
(new IllegalStateException("Persistence is not supported"),
"Persistence is not supported");
}
|
public void postDeregister() {
if( resource instanceof MBeanRegistration ) {
((MBeanRegistration)resource).postDeregister();
}
}
|
public void postRegister(Boolean registrationDone) {
if( resource instanceof MBeanRegistration ) {
((MBeanRegistration)resource).postRegister(registrationDone);
}
}
|
public void preDeregister() throws Exception {
if( resource instanceof MBeanRegistration ) {
((MBeanRegistration)resource).preDeregister();
}
}
|
public ObjectName preRegister(MBeanServer server,
ObjectName name) throws Exception {
if( log.isDebugEnabled())
log.debug("preRegister " + resource + " " + name );
oname=name;
if( resource instanceof MBeanRegistration ) {
oname = ((MBeanRegistration)resource).preRegister(server, name );
}
return oname;
}
|
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. |
public void removeAttributeChangeNotificationListener(NotificationListener listener,
String attributeName,
Object handback) throws ListenerNotFoundException {
removeAttributeChangeNotificationListener(listener, attributeName);
}
Remove an attribute change notification event listener from
this MBean. |
public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException {
if (listener == null)
throw new IllegalArgumentException("Listener is null");
if (generalBroadcaster == null)
generalBroadcaster = new BaseNotificationBroadcaster();
generalBroadcaster.removeNotificationListener(listener);
}
Remove a notification event listener from this MBean. |
public void removeNotificationListener(NotificationListener listener,
Object handback) throws ListenerNotFoundException {
removeNotificationListener(listener);
}
Remove a notification event listener from this MBean. |
public void removeNotificationListener(NotificationListener listener,
NotificationFilter filter,
Object handback) throws ListenerNotFoundException {
removeNotificationListener(listener);
}
Remove a notification event listener from this MBean. |
public void sendAttributeChangeNotification(AttributeChangeNotification notification) throws RuntimeOperationsException, MBeanException {
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 AttributeChangeNotification to all registered
listeners. |
public void sendAttributeChangeNotification(Attribute oldValue,
Attribute newValue) throws RuntimeOperationsException, MBeanException {
// 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 an AttributeChangeNotification to all registered
listeners. |
public void sendNotification(Notification notification) throws RuntimeOperationsException, MBeanException {
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 Notification to all registered listeners as a
jmx.modelmbean.general notification. |
public void sendNotification(String message) throws RuntimeOperationsException, MBeanException {
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);
}
Send a Notification which contains the specified string
as a jmx.modelmbean.generic notification. |
public void setAttribute(Attribute attribute) throws ReflectionException, AttributeNotFoundException, MBeanException {
if( log.isDebugEnabled() )
log.debug("Setting attribute " + this + " " + attribute );
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);
} 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);
}
}
Set the value of a specific attribute of this MBean. |
public AttributeList setAttributes(AttributeList attributes) {
// Validate the input parameters
if (attributes == null)
throw new RuntimeOperationsException
(new IllegalArgumentException("Attributes list is null"),
"Attributes list is null");
// Prepare and return our response, eating all exceptions
AttributeList response = new AttributeList();
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));
}
Set the values of several attributes of this MBean. |
public void setManagedResource(Object resource,
String type) throws InstanceNotFoundException, InvalidTargetObjectTypeException, RuntimeOperationsException, MBeanException {
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 );
}
}
Set the instance handle of the object against which we will execute
all methods in this ModelMBean management interface.
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. |
public void setModelMBeanInfo(ModelMBeanInfo info) throws RuntimeOperationsException, MBeanException {
if (info == null)
throw new RuntimeOperationsException
(new IllegalArgumentException("ModelMBeanInfo is null"),
"ModelMBeanInfo is null");
if (!isModelMBeanInfoValid(info))
throw new RuntimeOperationsException
(new IllegalArgumentException("ModelMBeanInfo is invalid"),
"ModelMBeanInfo is invalid");
this.info = (ModelMBeanInfo) info.clone();
}
Initialize the ModelMBeanInfo associated with this
ModelMBean. After the information and associated
descriptors have been customized, the ModelMBean should
be registered with the associated MBeanServer.
Currently the model can be set after registration. This behavior is
deprecated and won't be supported in future versions. |
public void setModeledType(String type) {
initModelInfo(type);
createResource();
}
Set the type of the mbean. This is used as a key to locate
the description in the Registry. |
public void setRegistry(Registry registry) {
this.registry = registry;
}
|
public void store() throws InstanceNotFoundException, RuntimeOperationsException, MBeanException {
// XXX if a context was set, use it to store the data
throw new MBeanException
(new IllegalStateException("Persistence is not supported"),
"Persistence is not supported");
}
|
public String toString() {
if( resource==null )
return "BaseModelMbean[" + resourceType + "]";
return resource.toString();
}
|