下面列出了怎么用javax.management.MBeanParameterInfo的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Extract {@link MBeanParameterInfo} signature for given method.
*/
private MBeanParameterInfo[] signature(final Method method) {
// NOTE: when NX is based on Java8 we can replace Paranamer usage with JDK api
Paranamer paranamer = new BytecodeReadingParanamer();
String[] names = paranamer.lookupParameterNames(method);
Class[] types = method.getParameterTypes();
Annotation[][] annotations = method.getParameterAnnotations();
MBeanParameterInfo[] result = new MBeanParameterInfo[names.length];
for (int i=0; i< names.length; i++) {
Descriptor descriptor = DescriptorHelper.build(annotations[i]);
String description = DescriptorHelper.stringValue(descriptor, "description");
result[i] = new MBeanParameterInfo(
names[i],
types[i].getName(),
description,
descriptor
);
}
return result;
}
/**
* Returns a string containing the entire contents of the ModelMBeanConstructorInfo in human readable form.
*/
@Override
public String toString()
{
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanConstructorInfo.class.getName(),
"toString()", "Entry");
}
String retStr =
"ModelMBeanConstructorInfo: " + this.getName() +
" ; Description: " + this.getDescription() +
" ; Descriptor: " + this.getDescriptor() +
" ; Signature: ";
MBeanParameterInfo[] pTypes = this.getSignature();
for (int i=0; i < pTypes.length; i++)
{
retStr = retStr.concat((pTypes[i]).getType() + ", ");
}
return retStr;
}
/**
* Create parameter info for the given method.
* <p>The default implementation returns an empty array of {@code MBeanParameterInfo}.
* @param method the {@code Method} to get the parameter information for
* @param beanKey the key associated with the MBean in the beans map
* of the {@code MBeanExporter}
* @return the {@code MBeanParameterInfo} array
*/
protected MBeanParameterInfo[] getOperationParameters(Method method, String beanKey) {
ParameterNameDiscoverer paramNameDiscoverer = getParameterNameDiscoverer();
String[] paramNames = (paramNameDiscoverer != null ? paramNameDiscoverer.getParameterNames(method) : null);
if (paramNames == null) {
return new MBeanParameterInfo[0];
}
MBeanParameterInfo[] info = new MBeanParameterInfo[paramNames.length];
Class<?>[] typeParameters = method.getParameterTypes();
for (int i = 0; i < info.length; i++) {
info[i] = new MBeanParameterInfo(paramNames[i], typeParameters[i].getName(), paramNames[i]);
}
return info;
}
/**
* Constructs a ModelMBeanOperationInfo object with a default descriptor.
*
* @param name The name of the method.
* @param description A human readable description of the operation.
* @param signature MBeanParameterInfo objects describing the
* parameters(arguments) of the method.
* @param type The type of the method's return value.
* @param impact The impact of the method, one of INFO, ACTION,
* ACTION_INFO, UNKNOWN.
*/
public ModelMBeanOperationInfo(String name,
String description,
MBeanParameterInfo[] signature,
String type,
int impact)
{
super(name, description, signature, type, impact);
// create default descriptor
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanOperationInfo.class.getName(),
"ModelMBeanOperationInfo(" +
"String,String,MBeanParameterInfo[],String,int)",
"Entry");
}
operationDescriptor = validDescriptor(null);
}
/**
* Constructs a ModelMBeanConstructorInfo object with a default descriptor.
*
* @param name The name of the constructor.
* @param description A human readable description of the constructor.
* @param signature MBeanParameterInfo object array describing the parameters(arguments) of the constructor.
*/
public ModelMBeanConstructorInfo(String name,
String description,
MBeanParameterInfo[] signature)
{
super(name, description, signature);
// create default descriptor
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanConstructorInfo.class.getName(),
"ModelMBeanConstructorInfo(" +
"String,String,MBeanParameterInfo[])", "Entry");
}
consDescriptor = validDescriptor(null);
}
/**
* Constructs a ModelMBeanConstructorInfo object with a default descriptor.
*
* @param name The name of the constructor.
* @param description A human readable description of the constructor.
* @param signature MBeanParameterInfo object array describing the parameters(arguments) of the constructor.
*/
public ModelMBeanConstructorInfo(String name,
String description,
MBeanParameterInfo[] signature)
{
super(name, description, signature);
// create default descriptor
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanConstructorInfo.class.getName(),
"ModelMBeanConstructorInfo(" +
"String,String,MBeanParameterInfo[])", "Entry");
}
consDescriptor = validDescriptor(null);
}
/**
* Returns a string containing the entire contents of the ModelMBeanConstructorInfo in human readable form.
*/
@Override
public String toString()
{
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanConstructorInfo.class.getName(),
"toString()", "Entry");
}
String retStr =
"ModelMBeanConstructorInfo: " + this.getName() +
" ; Description: " + this.getDescription() +
" ; Descriptor: " + this.getDescriptor() +
" ; Signature: ";
MBeanParameterInfo[] pTypes = this.getSignature();
for (int i=0; i < pTypes.length; i++)
{
retStr = retStr.concat((pTypes[i]).getType() + ", ");
}
return retStr;
}
/**
* Constructs a ModelMBeanOperationInfo object with a default descriptor.
*
* @param name The name of the method.
* @param description A human readable description of the operation.
* @param signature MBeanParameterInfo objects describing the
* parameters(arguments) of the method.
* @param type The type of the method's return value.
* @param impact The impact of the method, one of INFO, ACTION,
* ACTION_INFO, UNKNOWN.
*/
public ModelMBeanOperationInfo(String name,
String description,
MBeanParameterInfo[] signature,
String type,
int impact)
{
super(name, description, signature, type, impact);
// create default descriptor
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanOperationInfo.class.getName(),
"ModelMBeanOperationInfo(" +
"String,String,MBeanParameterInfo[],String,int)",
"Entry");
}
operationDescriptor = validDescriptor(null);
}
/**
* Constructs a ModelMBeanConstructorInfo object with a default descriptor.
*
* @param name The name of the constructor.
* @param description A human readable description of the constructor.
* @param signature MBeanParameterInfo object array describing the parameters(arguments) of the constructor.
*/
public ModelMBeanConstructorInfo(String name,
String description,
MBeanParameterInfo[] signature)
{
super(name, description, signature);
// create default descriptor
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanConstructorInfo.class.getName(),
"ModelMBeanConstructorInfo(" +
"String,String,MBeanParameterInfo[])", "Entry");
}
consDescriptor = validDescriptor(null);
}
/**
* Returns a string containing the entire contents of the ModelMBeanConstructorInfo in human readable form.
*/
@Override
public String toString()
{
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanConstructorInfo.class.getName(),
"toString()", "Entry");
}
String retStr =
"ModelMBeanConstructorInfo: " + this.getName() +
" ; Description: " + this.getDescription() +
" ; Descriptor: " + this.getDescriptor() +
" ; Signature: ";
MBeanParameterInfo[] pTypes = this.getSignature();
for (int i=0; i < pTypes.length; i++)
{
retStr = retStr.concat((pTypes[i]).getType() + ", ");
}
return retStr;
}
/**
* Returns a string containing the entire contents of the ModelMBeanConstructorInfo in human readable form.
*/
@Override
public String toString()
{
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanConstructorInfo.class.getName(),
"toString()", "Entry");
}
String retStr =
"ModelMBeanConstructorInfo: " + this.getName() +
" ; Description: " + this.getDescription() +
" ; Descriptor: " + this.getDescriptor() +
" ; Signature: ";
MBeanParameterInfo[] pTypes = this.getSignature();
for (int i=0; i < pTypes.length; i++)
{
retStr = retStr.concat((pTypes[i]).getType() + ", ");
}
return retStr;
}
/**
* Constructs a ModelMBeanOperationInfo object with a default descriptor.
*
* @param name The name of the method.
* @param description A human readable description of the operation.
* @param signature MBeanParameterInfo objects describing the
* parameters(arguments) of the method.
* @param type The type of the method's return value.
* @param impact The impact of the method, one of INFO, ACTION,
* ACTION_INFO, UNKNOWN.
*/
public ModelMBeanOperationInfo(String name,
String description,
MBeanParameterInfo[] signature,
String type,
int impact)
{
super(name, description, signature, type, impact);
// create default descriptor
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanOperationInfo.class.getName(),
"ModelMBeanOperationInfo(" +
"String,String,MBeanParameterInfo[],String,int)",
"Entry");
}
operationDescriptor = validDescriptor(null);
}
/**
* Constructs a ModelMBeanOperationInfo object with a default descriptor.
*
* @param name The name of the method.
* @param description A human readable description of the operation.
* @param signature MBeanParameterInfo objects describing the
* parameters(arguments) of the method.
* @param type The type of the method's return value.
* @param impact The impact of the method, one of INFO, ACTION,
* ACTION_INFO, UNKNOWN.
*/
public ModelMBeanOperationInfo(String name,
String description,
MBeanParameterInfo[] signature,
String type,
int impact)
{
super(name, description, signature, type, impact);
// create default descriptor
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanOperationInfo.class.getName(),
"ModelMBeanOperationInfo(" +
"String,String,MBeanParameterInfo[],String,int)",
"Entry");
}
operationDescriptor = validDescriptor(null);
}
/**
* Returns a string containing the entire contents of the
* ModelMBeanOperationInfo in human readable form.
*/
public String toString()
{
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanOperationInfo.class.getName(),
"toString()", "Entry");
}
String retStr =
"ModelMBeanOperationInfo: " + this.getName() +
" ; Description: " + this.getDescription() +
" ; Descriptor: " + this.getDescriptor() +
" ; ReturnType: " + this.getReturnType() +
" ; Signature: ";
MBeanParameterInfo[] pTypes = this.getSignature();
for (int i=0; i < pTypes.length; i++)
{
retStr = retStr.concat((pTypes[i]).getType() + ", ");
}
return retStr;
}
/**
* Returns a string containing the entire contents of the
* ModelMBeanOperationInfo in human readable form.
*/
public String toString()
{
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanOperationInfo.class.getName(),
"toString()", "Entry");
}
String retStr =
"ModelMBeanOperationInfo: " + this.getName() +
" ; Description: " + this.getDescription() +
" ; Descriptor: " + this.getDescriptor() +
" ; ReturnType: " + this.getReturnType() +
" ; Signature: ";
MBeanParameterInfo[] pTypes = this.getSignature();
for (int i=0; i < pTypes.length; i++)
{
retStr = retStr.concat((pTypes[i]).getType() + ", ");
}
return retStr;
}
public void addModelMBeanMethod(String name,
String[] paramTypes,
String[] paramNames,
String[] paramDescs,
String description,
String rtype,
Descriptor desc) {
MBeanParameterInfo[] params = null;
if (paramTypes != null) {
params = new MBeanParameterInfo[ paramTypes.length ];
for (int i = 0; i < paramTypes.length; i++) {
params[i] = new MBeanParameterInfo(paramNames[i],
paramTypes[i], paramDescs[i]);
}
}
operations.put(name,
new ModelMBeanOperationInfo(name,
description,
params,
rtype,
MBeanOperationInfo.ACTION,
desc));
}
/**
* Build our parameter information for an operation.
*/
private MBeanParameterInfo[] buildOperationParameterInfo(Method method, JmxOperationInfo operationInfo) {
Class<?>[] types = method.getParameterTypes();
MBeanParameterInfo[] parameterInfos = new MBeanParameterInfo[types.length];
String[] parameterNames = operationInfo.getParameterNames();
String[] parameterDescriptions = operationInfo.getParameterDescriptions();
for (int i = 0; i < types.length; i++) {
String parameterName;
if (parameterNames == null || i >= parameterNames.length) {
parameterName = "p" + (i + 1);
} else {
parameterName = parameterNames[i];
}
String typeName = types[i].getName();
String description;
if (parameterDescriptions == null || i >= parameterDescriptions.length) {
description = "parameter #" + (i + 1) + " of type: " + typeName;
} else {
description = parameterDescriptions[i];
}
parameterInfos[i] = new MBeanParameterInfo(parameterName, typeName, description);
}
return parameterInfos;
}
/**
* Constructs a ModelMBeanOperationInfo object with a default descriptor.
*
* @param name The name of the method.
* @param description A human readable description of the operation.
* @param signature MBeanParameterInfo objects describing the
* parameters(arguments) of the method.
* @param type The type of the method's return value.
* @param impact The impact of the method, one of INFO, ACTION,
* ACTION_INFO, UNKNOWN.
*/
public ModelMBeanOperationInfo(String name,
String description,
MBeanParameterInfo[] signature,
String type,
int impact)
{
super(name, description, signature, type, impact);
// create default descriptor
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanOperationInfo.class.getName(),
"ModelMBeanOperationInfo(" +
"String,String,MBeanParameterInfo[],String,int)",
"Entry");
}
operationDescriptor = validDescriptor(null);
}
static MBeanParameterInfo[] parameters(final MBeanOperationInfo jvmInfo,
final Class<?>[] classes,
final Annotation[][] annots) {
final MBeanParameterInfo[] params =
new MBeanParameterInfo[classes.length];
assert classes.length == annots.length;
String desc = "";
for (int i = 0; i < classes.length; i++) {
final Descriptor d = jvmInfo.getSignature()[i].getDescriptor();
final String pn = "arg" + i;
for (final Annotation a : annots[i]) {
final Class<? extends Annotation> type = a.annotationType();
if (type.equals(Description.class) || type.equals(OPENEJB_API_TO_JAVAX.get(Description.class))) {
desc = getDescription(annotationProxy(a, Description.class), desc);
break;
}
}
params[i] = new MBeanParameterInfo(pn, classes[i].getName(), desc, d);
}
return params;
}
/**
* Constructs a ModelMBeanConstructorInfo object with a default descriptor.
*
* @param name The name of the constructor.
* @param description A human readable description of the constructor.
* @param signature MBeanParameterInfo object array describing the parameters(arguments) of the constructor.
*/
public ModelMBeanConstructorInfo(String name,
String description,
MBeanParameterInfo[] signature)
{
super(name, description, signature);
// create default descriptor
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanConstructorInfo.class.getName(),
"ModelMBeanConstructorInfo(" +
"String,String,MBeanParameterInfo[])", "Entry");
}
consDescriptor = validDescriptor(null);
}
/**
* Constructs a ModelMBeanOperationInfo object with a default descriptor.
*
* @param name The name of the method.
* @param description A human readable description of the operation.
* @param signature MBeanParameterInfo objects describing the
* parameters(arguments) of the method.
* @param type The type of the method's return value.
* @param impact The impact of the method, one of INFO, ACTION,
* ACTION_INFO, UNKNOWN.
*/
public ModelMBeanOperationInfo(String name,
String description,
MBeanParameterInfo[] signature,
String type,
int impact)
{
super(name, description, signature, type, impact);
// create default descriptor
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanOperationInfo.class.getName(),
"ModelMBeanOperationInfo(" +
"String,String,MBeanParameterInfo[],String,int)",
"Entry");
}
operationDescriptor = validDescriptor(null);
}
/**
* Returns a string containing the entire contents of the ModelMBeanConstructorInfo in human readable form.
*/
@Override
public String toString()
{
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
ModelMBeanConstructorInfo.class.getName(),
"toString()", "Entry");
}
String retStr =
"ModelMBeanConstructorInfo: " + this.getName() +
" ; Description: " + this.getDescription() +
" ; Descriptor: " + this.getDescriptor() +
" ; Signature: ";
MBeanParameterInfo[] pTypes = this.getSignature();
for (int i=0; i < pTypes.length; i++)
{
retStr = retStr.concat((pTypes[i]).getType() + ", ");
}
return retStr;
}
private static OpenMBeanParameterInfo[]
arrayCopyCast(MBeanParameterInfo[] src) {
if (src == null)
return null;
OpenMBeanParameterInfo[] dst = new OpenMBeanParameterInfo[src.length];
System.arraycopy(src, 0, dst, 0, src.length);
// may throw an ArrayStoreException
return dst;
}
/**
* Creates an instance of {@code ModelMBeanOperationInfo} for the
* given method. Populates the parameter info for the operation.
* @param method the {@code Method} to create a {@code ModelMBeanOperationInfo} for
* @param name the logical name for the operation (method name or property name);
* not used by the default implementation but possibly by subclasses
* @param beanKey the key associated with the MBean in the beans map
* of the {@code MBeanExporter}
* @return the {@code ModelMBeanOperationInfo}
*/
protected ModelMBeanOperationInfo createModelMBeanOperationInfo(Method method, String name, String beanKey) {
MBeanParameterInfo[] params = getOperationParameters(method, beanKey);
if (params.length == 0) {
return new ModelMBeanOperationInfo(getOperationDescription(method, beanKey), method);
}
else {
return new ModelMBeanOperationInfo(method.getName(),
getOperationDescription(method, beanKey),
getOperationParameters(method, beanKey),
method.getReturnType().getName(),
MBeanOperationInfo.UNKNOWN);
}
}
protected static String listOptions(MBeanServerConnection mbsc, ObjectInstance instance)
throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
StringBuffer result = new StringBuffer();
MBeanInfo info = mbsc.getMBeanInfo(instance.getObjectName());
MBeanAttributeInfo[] attributes = info.getAttributes();
if (attributes.length > 0) {
result.append("Attributes:");
result.append("\n");
for (int i = 0; i < attributes.length; i++) {
result.append(' ' + attributes[i].getName() + ": " + attributes[i].getDescription() + " (type="
+ attributes[i].getType() + ")");
result.append("\n");
}
}
MBeanOperationInfo[] operations = info.getOperations();
if (operations.length > 0) {
result.append("Operations:");
result.append("\n");
for (int i = 0; i < operations.length; i++) {
MBeanParameterInfo[] params = operations[i].getSignature();
StringBuffer paramsStrBuffer = new StringBuffer();
if (params != null) {
for (int j = 0; j < params.length; j++) {
paramsStrBuffer.append("\n name=");
paramsStrBuffer.append(params[j].getName());
paramsStrBuffer.append(" type=");
paramsStrBuffer.append(params[j].getType());
paramsStrBuffer.append(" ");
paramsStrBuffer.append(params[j].getDescription());
}
}
result.append(' ' + operations[i].getName() + ": " + operations[i].getDescription() + "\n Parameters "
+ params != null ? params.length
: 0 + ", return type=" + operations[i].getReturnType() + paramsStrBuffer.toString());
result.append("\n");
}
}
return result.toString();
}
private static MBeanParameterInfo[]
arrayCopyCast(OpenMBeanParameterInfo[] src) {
if (src == null)
return null;
MBeanParameterInfo[] dst = new MBeanParameterInfo[src.length];
System.arraycopy(src, 0, dst, 0, src.length);
// may throw an ArrayStoreException
return dst;
}
private boolean isOperationReadOnly(ObjectName name, String operationName, String[] signature) {
MBeanInfo info;
try {
info = getMBeanInfo(name, false, true);
} catch (Exception e) {
//This should not happen, just in case say it is not RO
return false;
}
if (info == null) {
//Default to not RO
return false;
}
for (MBeanOperationInfo op : info.getOperations()) {
if (op.getName().equals(operationName)) {
MBeanParameterInfo[] params = op.getSignature();
if (params.length != signature.length) {
continue;
}
boolean same = true;
for (int i = 0 ; i < params.length ; i++) {
if (!params[i].getType().equals(signature[i])) {
same = false;
break;
}
}
if (same) {
return op.getImpact() == MBeanOperationInfo.INFO;
}
}
}
//Default to not RO
return false;
}
static private MBeanParameterInfo[] makeParInfos(String[][] spec) {
final MBeanParameterInfo[] result =
new MBeanParameterInfo[spec.length];
for (int i=0;i<result.length;i++) {
System.out.println("\tCreate an MBeanParameterInfo: " +
spec[i][0]);
final MBeanParameterInfo item =
new MBeanParameterInfo(spec[i][2],spec[i][1],spec[i][0]);
result[i]=item;
}
return result;
}
/**
* Convert an array of {@code MBeanParameterInfo} into an array of
* {@code Class} instances corresponding to the parameters.
* @param paramInfo the JMX parameter info
* @param classLoader the ClassLoader to use for loading parameter types
* @return the parameter types as classes
* @throws ClassNotFoundException if a parameter type could not be resolved
*/
public static Class<?>[] parameterInfoToTypes(MBeanParameterInfo[] paramInfo, ClassLoader classLoader)
throws ClassNotFoundException {
Class<?>[] types = null;
if (paramInfo != null && paramInfo.length > 0) {
types = new Class<?>[paramInfo.length];
for (int x = 0; x < paramInfo.length; x++) {
types[x] = ClassUtils.forName(paramInfo[x].getType(), classLoader);
}
}
return types;
}
protected String[] createSignature(MBeanOperationInfo info) {
MBeanParameterInfo[] params = info.getSignature();
String[] answer = new String[params.length];
for (int i = 0; i < params.length; i++) {
answer[i] = params[i].getType();
}
return answer;
}