📄 managedbean.java
字号:
}
/**
* Create and return a <code>ModelMBean</code> that has been
* preconfigured with the <code>ModelMBeanInfo</code> information
* for this managed bean, and is associated with the specified
* managed object instance. The returned <code>ModelMBean</code>
* will <strong>NOT</strong> have been registered with our
* <code>MBeanServer</code>.
*
* @param instance Instanced of the managed object, or <code>null</code>
* for no associated instance
*
* @exception InstanceNotFoundException if the managed resource
* object cannot be found
* @exception InvalidTargetObjectTypeException if our MBean cannot
* handle object references (should never happen)
* @exception MBeanException if a problem occurs instantiating the
* <code>ModelMBean</code> instance
* @exception RuntimeOperationsException if a JMX runtime error occurs
*/
public DynamicMBean createMBean(Object instance)
throws InstanceNotFoundException,
MBeanException, RuntimeOperationsException {
BaseModelMBean mbean = null;
// Load the ModelMBean implementation class
if(getClassName().equals(BASE_MBEAN)) {
// Skip introspection
mbean = new BaseModelMBean();
} else {
Class clazz = null;
Exception ex = null;
try {
clazz = Class.forName(getClassName());
} catch (Exception e) {
}
if( clazz==null ) {
try {
ClassLoader cl= Thread.currentThread().getContextClassLoader();
if ( cl != null)
clazz= cl.loadClass(getClassName());
} catch (Exception e) {
ex=e;
}
}
if( clazz==null) {
throw new MBeanException
(ex, "Cannot load ModelMBean class " + getClassName());
}
try {
// Stupid - this will set the default minfo first....
mbean = (BaseModelMBean) clazz.newInstance();
} catch (RuntimeOperationsException e) {
throw e;
} catch (Exception e) {
throw new MBeanException
(e, "Cannot instantiate ModelMBean of class " +
getClassName());
}
}
mbean.setManagedBean(this);
// Set the managed resource (if any)
try {
if (instance != null)
mbean.setManagedResource(instance, "ObjectReference");
} catch (InstanceNotFoundException e) {
throw e;
}
return (mbean);
}
/**
* Create and return a <code>ModelMBeanInfo</code> object that
* describes this entire managed bean.
*/
MBeanInfo getMBeanInfo() {
// Return our cached information (if any)
if (info != null)
return (info);
// Create subordinate information descriptors as required
AttributeInfo attrs[] = getAttributes();
MBeanAttributeInfo attributes[] =
new MBeanAttributeInfo[attrs.length];
for (int i = 0; i < attrs.length; i++)
attributes[i] = attrs[i].createAttributeInfo();
OperationInfo opers[] = getOperations();
MBeanOperationInfo operations[] =
new MBeanOperationInfo[opers.length];
for (int i = 0; i < opers.length; i++)
operations[i] = opers[i].createOperationInfo();
// ConstructorInfo consts[] = getConstructors();
// ModelMBeanConstructorInfo constructors[] =
// new ModelMBeanConstructorInfo[consts.length];
// for (int i = 0; i < consts.length; i++)
// constructors[i] = consts[i].createConstructorInfo();
NotificationInfo notifs[] = getNotifications();
MBeanNotificationInfo notifications[] =
new MBeanNotificationInfo[notifs.length];
for (int i = 0; i < notifs.length; i++)
notifications[i] = notifs[i].createNotificationInfo();
// Construct and return a new ModelMBeanInfo object
info = new MBeanInfo(getClassName(),
getDescription(),
attributes,
new MBeanConstructorInfo[] {},
operations,
notifications);
// try {
// Descriptor descriptor = info.getMBeanDescriptor();
// Iterator fields = getFields().iterator();
// while (fields.hasNext()) {
// FieldInfo field = (FieldInfo) fields.next();
// descriptor.setField(field.getName(), field.getValue());
// }
// info.setMBeanDescriptor(descriptor);
// } catch (MBeanException e) {
// ;
// }
return (info);
}
/**
* Return a string representation of this managed bean.
*/
public String toString() {
StringBuffer sb = new StringBuffer("ManagedBean[");
sb.append("name=");
sb.append(name);
sb.append(", className=");
sb.append(className);
sb.append(", description=");
sb.append(description);
if (group != null) {
sb.append(", group=");
sb.append(group);
}
sb.append(", type=");
sb.append(type);
sb.append("]");
return (sb.toString());
}
Method getGetter(String aname, BaseModelMBean mbean, Object resource)
throws AttributeNotFoundException, MBeanException, ReflectionException {
// TODO: do we need caching ? JMX is for management, it's not supposed to require lots of performance.
Method m=null; // (Method)getAttMap.get( name );
if( m==null ) {
AttributeInfo attrInfo = (AttributeInfo)attributes.get(aname);
// Look up the actual operation to be used
if (attrInfo == null)
throw new AttributeNotFoundException(" Cannot find attribute " + aname + " for " + resource);
String getMethod = attrInfo.getGetMethod();
if (getMethod == null)
throw new AttributeNotFoundException("Cannot find attribute " + aname + " get method name");
Object object = null;
NoSuchMethodException exception = null;
try {
object = mbean;
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 );
}
return m;
}
public Method getSetter(String aname, BaseModelMBean bean, Object resource)
throws AttributeNotFoundException, MBeanException, ReflectionException {
// Cache may be needed for getters, but it is a really bad idea for setters, this is far
// less frequent.
Method m=null;//(Method)setAttMap.get( name );
if( m==null ) {
AttributeInfo attrInfo = (AttributeInfo)attributes.get(aname);
if (attrInfo == null)
throw new AttributeNotFoundException(" Cannot find attribute " + aname);
// Look up the actual operation to be used
String setMethod = attrInfo.getSetMethod();
if (setMethod == null)
throw new AttributeNotFoundException("Cannot find attribute " + aname + " set method name");
String argType=attrInfo.getType();
Class signature[] = new Class[] { BaseModelMBean.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) {
exception = e;
}
}
if( exception != null )
throw new ReflectionException(exception,
"Cannot find setter method " + setMethod +
" " + resource);
//setAttMap.put( name, m );
}
return m;
}
public Method getInvoke(String aname, Object[] params, String[] signature, BaseModelMBean bean, Object resource)
throws MBeanException, ReflectionException {
Method method = null;
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
OperationInfo opInfo = (OperationInfo)operations.get(aname);
if (opInfo == null)
throw new MBeanException(new ServiceNotFoundException(
"Cannot find operation " + aname),
"Cannot find operation " + aname);
// 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] = BaseModelMBean.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(aname, types);
} catch (NoSuchMethodException e) {
exception = e;
;
}
try {
if ((method == null) && (resource != null)) {
object = resource;
method = object.getClass().getMethod(aname, types);
}
} catch (NoSuchMethodException e) {
exception = e;
}
if (method == null) {
throw new ReflectionException(exception, "Cannot find method "
+ aname + " with this signature");
}
// invokeAttMap.put(mkey, method);
}
return method;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -