Methods Summary |
---|
public synchronized javax.management.MBeanInfo | buildMBeanInfo(java.lang.Class c)Builds the MBeanInfo from the given concrete MBean class.
return Introspector.testCompliance(c);
|
public synchronized javax.management.MBeanInfo | buildMBeanInfo(java.lang.Class c, java.lang.Class mbeanInterface)Builds the MBeanInfo from the given concrete MBean class,
using the given mbeanInterface as Management Interface.
return Introspector.testCompliance(c,mbeanInterface);
|
void | cacheMBeanInfo(java.lang.Class c, java.lang.Class mbeanInterface, javax.management.MBeanInfo info)Cache the MBeanInfo and MBean interface obtained for class
c.
This method is called by testCompliance(...)
after compliance is successfully verified. It uses two
{@link java.util.WeakHashMap WeakHashMaps} - one for the
MBeanInfo, one for the MBeanInterface, with calss c
as the key.
if (info != null) {
synchronized (mbeanInfoCache) {
if (mbeanInfoCache.get(c) == null) {
mbeanInfoCache.put(c, info);
}
}
}
if (mbeanInterface != null) {
synchronized (mbeanInterfaceCache) {
if ((mbeanInterfaceCache.get(c) == null) || (((WeakReference)mbeanInterfaceCache.get(c)).get() == null)) {
mbeanInterfaceCache.put(c, new WeakReference(mbeanInterface));
}
}
}
|
private static void | debug(java.lang.String clz, java.lang.String func, java.lang.String info)
Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info);
|
private static void | debug(java.lang.String func, java.lang.String info)
debug(dbgTag, func, info);
|
private static void | debugX(java.lang.String func, java.lang.Throwable e)
if (isDebugOn()) {
final StringWriter s = new StringWriter();
e.printStackTrace(new PrintWriter(s));
final String stack = s.toString();
debug(dbgTag,func,"Exception caught in "+ func+"(): "+e);
debug(dbgTag,func,stack);
// java.lang.System.err.println("**** Exception caught in "+
// func+"(): "+e);
// java.lang.System.err.println(stack);
}
|
java.lang.Class | findClass(java.lang.String className, java.lang.ClassLoader loader)Find a class using the specified ClassLoader.
return MBeanInstantiatorImpl.loadClass(className,
loader);
|
public static java.lang.Class | findClassForPrim(java.lang.String primName)Get the class of the constructed type
corresponding to the given primitive type
return (Class) primitiveClasses.get(primName);
|
public static java.lang.reflect.Constructor | findConstructor(java.lang.Class theClass, java.lang.Class[] parameterTypes)Finds a specific constructor of a class
Returns the requested constructor or null if not found
// Get the list of methods
Constructor mth = null;
try {
mth = theClass.getConstructor(parameterTypes);
} catch(Exception e) {
return null;
}
return mth;
|
public static java.lang.reflect.Method | findGetter(java.lang.Class classObj, java.lang.String attribute)Finds the getter of a specific attribute in an object.
Returns the method for accessing the attributes, null otherwise
// Methods called "is" or "get" tout court are not getters
if (attribute.length() == 0)
return null;
// Look for a method T getX(), where T is not void
Method m = findMethod(classObj, "get" + attribute, null);
if (m != null && m.getReturnType() != void.class)
return m;
// Look for a method boolean isX()
// must not be any other type than "boolean", including not "Boolean"
m = findMethod(classObj, "is" + attribute, null);
if (m != null && m.getReturnType() == boolean.class)
return m;
return null;
|
public static java.lang.reflect.Method | findMethod(java.lang.Class classObj, java.lang.String name, java.lang.Class[] parameterTypes)Finds a specific method of an object.
Returns the method or null if not found
Method method=null;
try {
method= classObj.getMethod(name, parameterTypes);
} catch(Exception e) {
// OK: will return null.
}
return method;
|
public static java.lang.reflect.Method | findMethod(java.lang.Class classObj, java.lang.String name)Finds a specific method of an object without knowing the parameter
types.
Returns the method or null if not found
Method method = null ;
try {
Method[] methods=classObj.getMethods();
int i = 0;
while ((i < methods.length) &&
!methods[i].getName().equals(name)) {
i++;
}
if (i < methods.length) {
method = methods[i];
}
} catch(Exception e) {
// OK: will return null.
}
return method;
|
public static java.lang.reflect.Method | findMethod(java.lang.Class classObj, java.lang.String name, int paramCount)Finds a specific method of an object given the number of parameters.
Returns the method or null if not found
Method method = null;
try {
Method[] methods=classObj.getMethods();
int i = 0;
boolean found = false;
while ((i < methods.length) && !found) {
found = methods[i].getName().equals(name);
if (found) { // Now check if the number of parameters
found = (methods[i].getParameterTypes().length ==
paramCount);
}
i++;
}
if (found) {
method = methods[i-1] ; // Note i-1 !
}
} catch(Exception e) {
// OK: will return null;
}
return method;
|
javax.management.MBeanNotificationInfo[] | findNotifications(java.lang.Object moi)Returns the MBeanNotificationInfo of the MBeans that implement
the NotificationBroadcaster interface.
if (moi instanceof javax.management.NotificationBroadcaster) {
MBeanNotificationInfo[] mbn =
((NotificationBroadcaster)moi).getNotificationInfo();
if (mbn == null) {
return new MBeanNotificationInfo[0];
}
MBeanNotificationInfo[] result =
new MBeanNotificationInfo[mbn.length];
for (int i = 0; i < mbn.length; i++) {
result[i] = (MBeanNotificationInfo) mbn[i].clone();
}
return result;
}
return new MBeanNotificationInfo[0];
|
public static java.lang.Class | findPrimForClass(java.lang.Object value)Get the class of the primitive type
corresponding to the given constructed object.
if (value instanceof Boolean)
return Boolean.TYPE;
else if (value instanceof Character)
return Character.TYPE;
else if (value instanceof Byte)
return Byte.TYPE;
else if (value instanceof Short)
return Short.TYPE;
else if (value instanceof Integer)
return Integer.TYPE;
else if (value instanceof Long)
return Long.TYPE;
else if (value instanceof Float)
return Float.TYPE;
else if (value instanceof Double)
return Double.TYPE;
return null;
|
public static java.lang.reflect.Method | findSetter(java.lang.Class classObj, java.lang.String attribute, java.lang.Class type)Finds the setter of a specific attribute in an object.
Returns the method for accessing the attribute, null otherwise
Method mth= findMethod(classObj, "set" + attribute, 1);
if (mth != null) {
Class[] pars = mth.getParameterTypes();
if (pars[0].isAssignableFrom(type)) {
return mth;
}
}
return null;
|
public static java.lang.reflect.Method | findSetter(java.lang.Class classObj, java.lang.String attribute)Finds the setter of a specific attribute without knowing its type.
Returns the method for accessing the attribute, null otherwise
return findMethod(classObj, "set" + attribute, 1) ;
|
static java.lang.String | findSignature(java.lang.Class clz)Converts the class to a class signature.
return clz.getName();
|
java.lang.Class[] | findSignatureClasses(java.lang.String[] signature, java.lang.ClassLoader loader)Find the classes from a signature using the specified ClassLoader.
return ((signature == null)?null:
MBeanInstantiatorImpl.loadSignatureClasses(signature,loader));
|
static java.lang.String[] | findSignatures(java.lang.Class[] clz)Converts the array of classes to an array of class signatures.
String signers[] = new String[clz.length];
for (int i = 0; i < clz.length; i++) {
signers[i] = findSignature(clz[i]);
}
return signers;
|
private static void | forbidInvokeGetterSetter(java.lang.reflect.Method mth, java.lang.String operationName)
final Class argTypes[] = mth.getParameterTypes();
final Class resultType = mth.getReturnType();
final int argCount = argTypes.length;
boolean isInvokeGetterSetter = false;
switch (argCount) {
case 0: // might be a getter
if ((startsWithAndHasMore(operationName, "get") &&
resultType != Void.TYPE) ||
(startsWithAndHasMore(operationName, "is") &&
resultType == Boolean.TYPE)) {
// Operation is a getter
isInvokeGetterSetter = true;
}
break;
case 1: // might be a setter
if (startsWithAndHasMore(operationName, "set") &&
resultType == Void.TYPE) {
// Operation is a setter
isInvokeGetterSetter = true;
}
break;
}
if (isInvokeGetterSetter) {
boolean allow;
try {
GetPropertyAction getProp =
new GetPropertyAction("jmx.invoke.getters");
allow = (AccessController.doPrivileged(getProp) != null);
} catch (SecurityException e) {
// too bad, don't allow it
allow = false;
}
if (!allow) {
final String msg =
"Cannot invoke getter or setter (" + operationName +
") as operation unless jmx.invoke.getters property is set";
final Exception nested =
new NoSuchMethodException(operationName);
throw new ReflectionException(nested, msg);
}
}
|
public java.lang.Object | getAttribute(java.lang.Object instance, java.lang.String attribute)
Class mbeanClass = getMBeanInterfaceFromInstance(instance);
if (isDebugOn()) {
debug("getAttribute","MBean Class is " + instance.getClass());
debug("getAttribute","MBean Interface is " + mbeanClass);
}
return getAttribute(instance, attribute, mbeanClass);
|
java.lang.Object | getAttribute(java.lang.Object instance, java.lang.String attribute, java.lang.Class mbeanClass)Invoke getAttribute through reflection on a standard MBean instance.
if (attribute == null) {
final RuntimeException r =
new IllegalArgumentException("Attribute name cannot be null");
throw new RuntimeOperationsException(r,
"Exception occured trying to invoke the getter on the MBean");
}
// Standard MBean: need to reflect...
Method meth = null;
meth = findGetter(mbeanClass, attribute);
if (meth == null) {
if (isTraceOn()) {
trace("getAttribute", "Cannot find getter for "+attribute+
" in class " + mbeanClass.getName());
}
throw new AttributeNotFoundException(attribute +
" not accessible");
}
// Invoke the getter
if (isTraceOn()) {
trace("getAttribute", "Invoke callback");
}
Object result= null;
try {
result = meth.invoke(instance, (Object[]) null);
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t instanceof RuntimeException) {
debugX("getAttribute",t);
final String msg =
"RuntimeException thrown in the getter for the attribute "
+ attribute;
throw wrapRuntimeException((RuntimeException) t, msg);
} else if (t instanceof Error) {
debugX("getAttribute",t);
throw new RuntimeErrorException((Error) t ,
"Error thrown in the getter for the attribute " +
attribute);
} else {
debugX("getAttribute",t);
throw new MBeanException((Exception) t,
"Exception thrown in the getter for the attribute " +
attribute);
}
} catch (RuntimeException e) {
debugX("getAttribute",e);
throw new RuntimeOperationsException(e,
"RuntimeException thrown trying to invoke the getter" +
" for the attribute " + attribute);
} catch (IllegalAccessException e) {
debugX("getAttribute",e);
throw new ReflectionException(e, "Exception thrown trying to" +
" invoke the getter for the attribute " + attribute);
} catch (Error e) {
debugX("getAttribute",e);
throw new RuntimeErrorException((Error)e,
"Error thrown trying to invoke the getter " +
" for the attribute " + attribute);
}
if (isTraceOn()) {
trace("getAttribute", attribute + "= " + result + "\n");
}
return result;
|
public javax.management.AttributeList | getAttributes(java.lang.Object instance, java.lang.String[] attributes)
final Class mbeanClass =
getMBeanInterfaceFromInstance(instance);
if (isDebugOn()) {
debug("getAttributes","MBean Class is " + instance.getClass());
debug("getAttributes","MBean Interface is " + mbeanClass);
}
if (attributes == null) {
throw new RuntimeOperationsException(new
IllegalArgumentException("Attributes cannot be null"),
"Exception occured trying to invoke the getter on the MBean");
}
// Go through the list of attributes
//
final int maxLimit = attributes.length;
final AttributeList result = new AttributeList(maxLimit);
for (int i=0;i<maxLimit;i++) {
final String elmt = (String)attributes[i];
try {
final Object value =
getAttribute(instance, elmt, mbeanClass);
result.add(new Attribute(elmt, value));
} catch (Exception excep) {
if (isDebugOn()) {
debug("getAttributes", "Object= " + instance +
", Attribute=" + elmt + " failed: " + excep);
}
}
}
return result;
|
javax.management.MBeanInfo | getCachedMBeanInfo(java.lang.Class c)Returns the MBeanInfo that was cached for class c.
synchronized (mbeanInfoCache) {
return (MBeanInfo)mbeanInfoCache.get(c);
}
|
java.lang.Class | getCachedMBeanInterface(java.lang.Class c)Returns the MBean interface that was cached for class c.
synchronized (mbeanInterfaceCache) {
return (Class)(((WeakReference)mbeanInterfaceCache.get(c)).get());
}
|
public java.lang.String | getMBeanClassName(java.lang.Object moi)
return moi.getClass().getName();
|
public javax.management.MBeanInfo | getMBeanInfo(java.lang.Object moi)
try {
final MBeanInfo mbi = getMBeanInfoFromClass(moi.getClass());
return new MBeanInfo(mbi.getClassName(), mbi.getDescription(),
mbi.getAttributes(),
mbi.getConstructors(),
mbi.getOperations(),
findNotifications(moi));
} catch (NotCompliantMBeanException x) {
debugX("getMBeanInfo",x);
throw new IntrospectionException("Can't build MBeanInfo for "+
moi.getClass().getName());
}
|
public javax.management.MBeanInfo | getMBeanInfoFromClass(java.lang.Class beanClass)This method discovers the attributes and operations that an MBean
exposes for management.
// Check the mbean information cache.
MBeanInfo bi = getCachedMBeanInfo(beanClass);
// Make an independent copy of the MBeanInfo.
if (bi != null) return (MBeanInfo) bi.clone() ;
// We don't have have any MBeanInfo for that class yet.
// => test compliance.
testCompliance(beanClass);
bi = getCachedMBeanInfo(beanClass);;
// Make an independent copy of the MBeanInfo.
if (bi != null) return (MBeanInfo) bi.clone() ;
return bi;
|
public java.lang.Class | getMBeanInterfaceFromClass(java.lang.Class c)This methods returns the MBean interface of an MBean
final Class itf = getCachedMBeanInterface(c);
if (itf != null) return itf;
synchronized (this) {
return Introspector.getMBeanInterface(c);
}
|
java.lang.Class | getMBeanInterfaceFromInstance(java.lang.Object instance)This methods returns the MBean interface of the given MBean
instance.
It does so by calling
getMBeanInterfaceFromClass(instance.getClass());
if (instance == null) return null;
return getMBeanInterfaceFromClass(instance.getClass());
|
public java.lang.Class | getStandardMBeanInterface(java.lang.Class c)This methods analizes the passed MBean class and
returns the default MBean interface according to JMX patterns.
synchronized (this) {
return Introspector.getStandardMBeanInterface(c);
}
|
public java.lang.Object | invoke(java.lang.Object instance, java.lang.String operationName, java.lang.Object[] params, java.lang.String[] signature)
if (operationName == null) {
final RuntimeException r =
new IllegalArgumentException("Operation name cannot be null");
throw new RuntimeOperationsException(r,
"Exception occured trying to invoke the operation on the MBean");
}
final Class objClass = instance.getClass();
final Class mbeanClass = getMBeanInterfaceFromInstance(instance);
final ClassLoader aLoader = objClass.getClassLoader();
if (isDebugOn()) {
debug("invoke","MBean Class is " + instance.getClass());
debug("invoke","MBean Interface is " + mbeanClass);
}
// Build the signature of the method
//
final Class[] tab =
((signature == null)?null:
findSignatureClasses(signature,aLoader));
// Query the metadata service to get the right method
//
Method mth= findMethod(mbeanClass, operationName, tab);
if (mth == null) {
if (isTraceOn()) {
trace("invoke", operationName + " not found in class " +
mbeanClass.getName());
}
throw new ReflectionException(
new NoSuchMethodException(operationName),
"The operation with name " + operationName +
" could not be found");
}
// Make it impossible to call getters and setters through invoke()
//
forbidInvokeGetterSetter(mth, operationName);
// invoke the method
if (isTraceOn()) {
trace("invoke", "Invoking " + operationName);
}
Object result=null;
try {
result= mth.invoke(instance, params);
} catch (IllegalAccessException e) {
debugX("invoke",e);
throw new ReflectionException(e, "IllegalAccessException" +
" occured trying to invoke operation " + operationName);
} catch (RuntimeException e) {
debugX("invoke",e);
throw new RuntimeOperationsException(e, "RuntimeException" +
" occured trying to invoke operation " + operationName);
} catch (InvocationTargetException e) {
// Wrap the exception.
Throwable t = e.getTargetException();
debugX("invoke",t);
if (t instanceof RuntimeException) {
final String msg = "RuntimeException thrown in operation " +
operationName;
throw wrapRuntimeException((RuntimeException) t, msg);
} else if (t instanceof Error) {
throw new RuntimeErrorException((Error) t,
"Error thrown in operation " + operationName);
} else {
throw new MBeanException((Exception) t,
"Exception thrown in operation " + operationName);
}
}
if (isTraceOn()) {
trace("invoke", "Send the result");
}
return (result);
|
private static boolean | isDebugOn()
return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER);
|
public boolean | isInstanceOf(java.lang.Object instance, java.lang.String className)
final Class c =
findClass(className, instance.getClass().getClassLoader());
return c.isInstance(instance);
|
private static boolean | isTraceOn()
return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER);
|
public java.lang.Object | setAttribute(java.lang.Object instance, javax.management.Attribute attribute)
final Class mbeanClass =
getMBeanInterfaceFromInstance(instance);
if (isDebugOn()) {
debug("setAttribute","MBean Class is " + instance.getClass());
debug("setAttribute","MBean Interface is " + mbeanClass);
}
return setAttribute(instance,attribute,mbeanClass);
|
java.lang.Object | setAttribute(java.lang.Object instance, javax.management.Attribute attribute, java.lang.Class mbeanClass)Invoke setAttribute through reflection on a standard MBean instance.
if (attribute == null) {
final RuntimeException r =
new IllegalArgumentException("Attribute name cannot be null");
throw new RuntimeOperationsException(r,
"Exception occured trying to invoke the setter on the MBean");
}
final Class objClass = instance.getClass();
final ClassLoader aLoader = objClass.getClassLoader();
Object result = null;
final Object value = attribute.getValue();
final String attname = attribute.getName();
// Query the metadata service to get the appropriate setter
// of the object.
Method meth = null;
if (value == null) {
meth = findSetter(mbeanClass, attname);
} else {
meth = findSetter(mbeanClass, attname, value.getClass());
}
if (meth == null) {
// Check whether the type is a primitive one
Class primClass = findPrimForClass(value);
if (primClass != null) {
meth = findSetter(mbeanClass, attname, primClass);
}
}
if (meth == null) {
// Try to check if the attribute name does correspond to a
// valid property
meth= findSetter(mbeanClass, attname);
if (meth == null) {
if (isTraceOn()) {
trace("setAttribute", "Cannot find setter for "+attribute+
" in class " + mbeanClass.getName());
}
throw new AttributeNotFoundException( attname +
" not accessible");
} else {
final Object v = attribute.getValue();
if (v == null) {
throw new InvalidAttributeValueException("attribute= " +
attname + " value = null");
} else {
throw new InvalidAttributeValueException("attribute= " +
attname + " value = " + v);
}
}
}
// Invoke the setter
if (isTraceOn()) {
trace("setAttribute", "Invoking the set method for " +
attname);
}
final Object[] values = new Object[1];
values[0] = value;
try {
result = meth.invoke(instance,values);
} catch (IllegalAccessException e) {
debugX("setAttribute",e);
// Wrap the exception.
throw new ReflectionException(e, "IllegalAccessException" +
" occured trying to invoke the setter on the MBean");
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
debugX("setAttribute",t);
if (t instanceof RuntimeException) {
final String msg =
"RuntimeException thrown in the setter for the attribute "
+ attribute;
throw wrapRuntimeException((RuntimeException) t, msg);
} else if (t instanceof Error) {
throw new RuntimeErrorException((Error) t,
"Error thrown in the MBean's setter");
} else {
throw new MBeanException((Exception) t,
"Exception thrown in the MBean's setter");
}
}
if (isTraceOn()) {
trace("setAttribute", attname + "= " + value);
}
return value;
|
public javax.management.AttributeList | setAttributes(java.lang.Object instance, javax.management.AttributeList attributes)
final Class objClass = instance.getClass();
final Class mbeanClass = getMBeanInterfaceFromInstance(instance);
final ClassLoader aLoader = objClass.getClassLoader();
if (isDebugOn()) {
debug("setAttributes","MBean Class is " + instance.getClass());
debug("setAttributes","MBean Interface is " + mbeanClass);
}
if (attributes == null) return new AttributeList();
final AttributeList result = new AttributeList(attributes.size());
// Go through the list of attributes
for (final Iterator i = attributes.iterator(); i.hasNext();) {
final Attribute attr = (Attribute) i.next();
final String id = attr.getName();
final Object value = attr.getValue();
try {
final Object newValue =
setAttribute(instance, attr, mbeanClass);
if (isTraceOn()) {
trace("setAttributes", "Updating the list\n");
}
result.add(new Attribute(id, newValue));
} catch (Exception excep) {
if (isDebugOn()) {
debug("setAttributes", "Unexpected exception occured: " +
excep.getClass().getName());
}
}
}
return result;
|
private static boolean | startsWithAndHasMore(java.lang.String s, java.lang.String prefix)
return (s.startsWith(prefix) && s.length() > prefix.length());
|
public synchronized void | testCompliance(java.lang.Class c)This methods tests if the MBean is JMX compliant
// ------------------------------
// ------------------------------
final MBeanInfo mbeanInfo = buildMBeanInfo(c);
final Class mbeanInterface = Introspector.getMBeanInterface(c);
cacheMBeanInfo(c,mbeanInterface,mbeanInfo);
|
public synchronized void | testCompliance(java.lang.Class c, java.lang.Class mbeanInterface)This methods tests if the MBean is JMX compliant.
It does not enforce that if c="X", mbeanInterface="XMBean".
It does not check whether c is a DynamicMBean
// ------------------------------
// ------------------------------
final MBeanInfo mbeanInfo =
buildMBeanInfo(c,mbeanInterface);
if (mbeanInterface == null)
mbeanInterface = Introspector.getStandardMBeanInterface(c);
cacheMBeanInfo(c,mbeanInterface,mbeanInfo);
|
private static void | trace(java.lang.String clz, java.lang.String func, java.lang.String info)
Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info);
|
private static void | trace(java.lang.String func, java.lang.String info)
trace(dbgTag, func, info);
|
private java.lang.RuntimeException | wrapRuntimeException(java.lang.RuntimeException re, java.lang.String msg)
if (wrapRuntimeExceptions)
return new RuntimeMBeanException(re, msg);
else
return re;
|