Search in sources :

Example 76 with MBeanOperationInfo

use of javax.management.MBeanOperationInfo in project jdk8u_jdk by JetBrains.

the class MXBeanIntrospector method getMBeanOperationInfo.

@Override
MBeanOperationInfo getMBeanOperationInfo(String operationName, ConvertingMethod operation) {
    final Method method = operation.getMethod();
    final String description = operationName;
    /* Ideally this would be an empty string, but
           OMBOperationInfo constructor forbids that.  Also, we
           could consult an annotation to get a useful
           description.  */
    final int impact = MBeanOperationInfo.UNKNOWN;
    final OpenType<?> returnType = operation.getOpenReturnType();
    final Type originalReturnType = operation.getGenericReturnType();
    final OpenType<?>[] paramTypes = operation.getOpenParameterTypes();
    final Type[] originalParamTypes = operation.getGenericParameterTypes();
    final MBeanParameterInfo[] params = new MBeanParameterInfo[paramTypes.length];
    boolean openReturnType = canUseOpenInfo(originalReturnType);
    boolean openParameterTypes = true;
    Annotation[][] annots = method.getParameterAnnotations();
    for (int i = 0; i < paramTypes.length; i++) {
        final String paramName = "p" + i;
        final String paramDescription = paramName;
        final OpenType<?> openType = paramTypes[i];
        final Type originalType = originalParamTypes[i];
        Descriptor descriptor = typeDescriptor(openType, originalType);
        descriptor = ImmutableDescriptor.union(descriptor, Introspector.descriptorForAnnotations(annots[i]));
        final MBeanParameterInfo pi;
        if (canUseOpenInfo(originalType)) {
            pi = new OpenMBeanParameterInfoSupport(paramName, paramDescription, openType, descriptor);
        } else {
            openParameterTypes = false;
            pi = new MBeanParameterInfo(paramName, originalTypeString(originalType), paramDescription, descriptor);
        }
        params[i] = pi;
    }
    Descriptor descriptor = typeDescriptor(returnType, originalReturnType);
    descriptor = ImmutableDescriptor.union(descriptor, Introspector.descriptorForElement(method));
    final MBeanOperationInfo oi;
    if (openReturnType && openParameterTypes) {
        /* If the return value and all the parameters can be faithfully
             * represented as OpenType then we return an OpenMBeanOperationInfo.
             * If any of them is a primitive type, we can't.  Compatibility
             * with JSR 174 means that we must return an MBean*Info where
             * the getType() is the primitive type, not its wrapped type as
             * we would get with an OpenMBean*Info.  The OpenType is available
             * in the Descriptor in either case.
             */
        final OpenMBeanParameterInfo[] oparams = new OpenMBeanParameterInfo[params.length];
        System.arraycopy(params, 0, oparams, 0, params.length);
        oi = new OpenMBeanOperationInfoSupport(operationName, description, oparams, returnType, impact, descriptor);
    } else {
        oi = new MBeanOperationInfo(operationName, description, params, openReturnType ? returnType.getClassName() : originalTypeString(originalReturnType), impact, descriptor);
    }
    return oi;
}
Also used : OpenMBeanParameterInfo(javax.management.openmbean.OpenMBeanParameterInfo) OpenType(javax.management.openmbean.OpenType) MBeanOperationInfo(javax.management.MBeanOperationInfo) OpenMBeanParameterInfoSupport(javax.management.openmbean.OpenMBeanParameterInfoSupport) Method(java.lang.reflect.Method) GenericArrayType(java.lang.reflect.GenericArrayType) OpenType(javax.management.openmbean.OpenType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Descriptor(javax.management.Descriptor) ImmutableDescriptor(javax.management.ImmutableDescriptor) OpenMBeanOperationInfoSupport(javax.management.openmbean.OpenMBeanOperationInfoSupport) MBeanParameterInfo(javax.management.MBeanParameterInfo) OpenMBeanParameterInfo(javax.management.openmbean.OpenMBeanParameterInfo)

Example 77 with MBeanOperationInfo

use of javax.management.MBeanOperationInfo in project felix by apache.

the class MBeanProxyFactory method getSignatures.

/**
 * Internal method for generating the signatures of the mbeans.
 * @param objectName
 * @return
 * @throws IOException
 * @throws ReflectionException
 * @throws IntrospectionException
 * @throws InstanceNotFoundException
 */
private List<Signature> getSignatures(ObjectName objectName) throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
    List<Signature> methods = new ArrayList<Signature>();
    MBeanInfo minfo;
    MBeanAttributeInfo[] attributes = null;
    minfo = mbeanServer.getMBeanInfo(objectName);
    attributes = minfo.getAttributes();
    for (MBeanAttributeInfo info : attributes) {
        String name = info.getName().substring(0, 1).toUpperCase() + info.getName().substring(1);
        if (info.isReadable()) {
            // when it is being used on the remote side)
            if (info.isIs()) {
                methods.add(new Signature("is" + name, Type.BOOLEAN_TYPE, new Type[0]));
            } else {
                try {
                    methods.add(new Signature("get" + name, getType(info.getType()), new Type[0]));
                } catch (ClassNotFoundException cnfe) {
                    System.out.println("JMXINTROSPECTOR WARNING: " + info.getType() + " could not be found. Attribute will not be added to proxy.");
                    continue;
                }
            }
        }
        // Same with each writable att, but with setters.
        if (info.isWritable()) {
            try {
                Type[] params = new Type[] { getType(info.getType()) };
                Signature s = new Signature("set" + name, Type.VOID_TYPE, params);
                methods.add(s);
            } catch (ClassNotFoundException cnfe) {
                System.out.println("JMXINTROSPECTOR WARNING: " + info.getType() + " could not be found. Attribute will not be added to proxy.");
                continue;
            }
        }
    }
    // same for each operation
    for (MBeanOperationInfo info : minfo.getOperations()) {
        try {
            Type[] params = new Type[info.getSignature().length];
            for (int i = 0; i < params.length; i++) {
                params[i] = getType(info.getSignature()[i].getType());
            }
            Signature s = new Signature(info.getName(), getType(info.getReturnType()), params);
            methods.add(s);
        } catch (ClassNotFoundException cnfe) {
            System.out.println("JMXINTROSPECTOR WARNING: " + info.toString() + " could not be created. Operation will not be added to proxy.");
            continue;
        }
    }
    return methods;
}
Also used : Type(org.objectweb.asm.Type) MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) Signature(net.sf.cglib.core.Signature) ArrayList(java.util.ArrayList) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 78 with MBeanOperationInfo

use of javax.management.MBeanOperationInfo in project felix by apache.

the class InvokeOperationCommandProcessor method executeRequest.

public Document executeRequest(HttpInputStream in) throws IOException, JMException {
    Document document = builder.newDocument();
    Element root = document.createElement("MBeanOperation");
    document.appendChild(root);
    Element operationElement = document.createElement("Operation");
    operationElement.setAttribute("operation", "invoke");
    root.appendChild(operationElement);
    String objectVariable = in.getVariable("objectname");
    String operationVariable = in.getVariable("operation");
    if (objectVariable == null || objectVariable.equals("") || operationVariable == null || operationVariable.equals("")) {
        operationElement.setAttribute("result", "error");
        operationElement.setAttribute("errorMsg", "Incorrect parameters in the request");
        return document;
    }
    operationElement.setAttribute("objectname", objectVariable);
    List types = new ArrayList();
    List values = new ArrayList();
    int i = 0;
    boolean unmatchedParameters = false;
    boolean valid = false;
    do {
        String parameterType = in.getVariable("type" + i);
        String parameterValue = in.getVariable("value" + i);
        valid = (parameterType != null && parameterValue != null);
        if (valid) {
            types.add(parameterType);
            Object value = null;
            try {
                value = CommandProcessorUtil.createParameterValue(parameterType, parameterValue);
            } catch (Exception e) {
                operationElement.setAttribute("result", "error");
                operationElement.setAttribute("errorMsg", "Parameter " + i + ": " + parameterValue + " cannot be converted to type " + parameterType);
                return document;
            }
            if (value != null) {
                values.add(value);
            }
        }
        if (parameterType == null ^ parameterValue == null) {
            unmatchedParameters = true;
            break;
        }
        i++;
    } while (valid);
    if (objectVariable == null || objectVariable.equals("") || operationVariable == null || operationVariable.equals("")) {
        operationElement.setAttribute("result", "error");
        operationElement.setAttribute("errorMsg", "Incorrect parameters in the request");
        return document;
    }
    if (unmatchedParameters) {
        operationElement.setAttribute("result", "error");
        operationElement.setAttribute("errorMsg", "count of parameter types doesn't match count of parameter values");
        return document;
    }
    ObjectName name = null;
    try {
        name = new ObjectName(objectVariable);
    } catch (MalformedObjectNameException e) {
        operationElement.setAttribute("result", "error");
        operationElement.setAttribute("errorMsg", "Malformed object name");
        return document;
    }
    if (server.isRegistered(name)) {
        MBeanInfo info = server.getMBeanInfo(name);
        MBeanOperationInfo[] operations = info.getOperations();
        boolean match = false;
        if (operations != null) {
            for (int j = 0; j < operations.length; j++) {
                if (operations[j].getName().equals(operationVariable)) {
                    MBeanParameterInfo[] parameters = operations[j].getSignature();
                    if (parameters.length != types.size()) {
                        continue;
                    }
                    Iterator k = types.iterator();
                    boolean signatureMatch = true;
                    for (int p = 0; p < types.size(); p++) {
                        if (!parameters[p].getType().equals(k.next())) {
                            signatureMatch = false;
                            break;
                        }
                    }
                    match = signatureMatch;
                }
                if (match) {
                    break;
                }
            }
        }
        if (!match) {
            operationElement.setAttribute("result", "error");
            operationElement.setAttribute("errorMsg", "Operation singature has no match in the MBean");
        } else {
            try {
                Object[] params = values.toArray();
                String[] signature = new String[types.size()];
                types.toArray(signature);
                Object returnValue = server.invoke(name, operationVariable, params, signature);
                operationElement.setAttribute("result", "success");
                if (returnValue != null) {
                    operationElement.setAttribute("returnclass", returnValue.getClass().getName());
                    operationElement.setAttribute("return", returnValue.toString());
                } else {
                    operationElement.setAttribute("returnclass", null);
                    operationElement.setAttribute("return", null);
                }
            } catch (Exception e) {
                operationElement.setAttribute("result", "error");
                operationElement.setAttribute("errorMsg", e.getMessage());
            }
        }
    } else {
        if (name != null) {
            operationElement.setAttribute("result", "error");
            operationElement.setAttribute("errorMsg", new StringBuffer("MBean ").append(name).append(" not registered").toString());
        }
    }
    return document;
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) MalformedObjectNameException(javax.management.MalformedObjectNameException) JMException(javax.management.JMException) IOException(java.io.IOException) ObjectName(javax.management.ObjectName) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) MBeanParameterInfo(javax.management.MBeanParameterInfo)

Example 79 with MBeanOperationInfo

use of javax.management.MBeanOperationInfo in project Payara by payara.

the class AMXTest method checkReturnTypes.

/**
 *     Verify:
 *     <ul>
 *     <li>that all return types are suitable for the API</li>
 *     </ul>
 */
public void checkReturnTypes(final ObjectName objectName) throws Exception {
    final AMX proxy = getProxyFactory().getProxy(objectName, AMX.class);
    final MBeanInfo info = Util.getExtra(proxy).getMBeanInfo();
    final MBeanOperationInfo[] operations = info.getOperations();
    boolean emittedName = false;
    for (int i = 0; i < operations.length; ++i) {
        final MBeanOperationInfo opInfo = operations[i];
        final String returnType = opInfo.getReturnType();
        if (!isSuitableReturnTypeForAPI(returnType)) {
            if (!emittedName) {
                emittedName = true;
                trace("\n" + objectName);
            }
            trace("WARNING: unsuitable return type in API: " + returnType + " " + opInfo.getName() + "(...)");
        }
    }
}
Also used : MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) AMX(com.sun.appserv.management.base.AMX)

Example 80 with MBeanOperationInfo

use of javax.management.MBeanOperationInfo in project Payara by payara.

the class AMXTest method checkCreateRemoveGet.

/**
 *     Verify:
 *     <ul>
 *     <li>each create() or createAbc() method ends in "Config" if it returns an AMXConfig subclass</li>
 *     <li>each remove() or removeAbc() method ends in "Config"</li>
 *     </ul>
 */
public void checkCreateRemoveGet(final ObjectName objectName) throws Exception {
    final AMX proxy = getProxyFactory().getProxy(objectName, AMX.class);
    if (proxy instanceof Container) {
        final Method[] methods = getInterfaceClass(proxy).getMethods();
        final MBeanInfo mbeanInfo = Util.getExtra(proxy).getMBeanInfo();
        final MBeanOperationInfo[] operations = mbeanInfo.getOperations();
        for (int methodIdx = 0; methodIdx < methods.length; ++methodIdx) {
            final Method method = methods[methodIdx];
            final String methodName = method.getName();
            if (methodName.startsWith("create") && !methodName.endsWith("Config")) {
                if (AMXConfig.class.isAssignableFrom(method.getReturnType()) && (!(proxy instanceof SecurityMapConfig))) {
                    trace("WARNING: method " + methodName + " does not end in 'Config': " + objectName);
                }
            } else if (methodName.startsWith("remove") && !methodName.endsWith("Config") && proxy instanceof AMXConfig) {
                if (// method.getReturnType() == Void.class &&
                method.getParameterTypes().length == 1 && method.getParameterTypes()[0] == String.class && !method.getName().equals("removeProperty") && !method.getName().equals("removeSystemProperty") && (!(proxy instanceof SecurityMapConfig))) {
                    trace("WARNING: method " + methodName + " does not end in 'Config': " + methodName);
                }
            }
        }
    }
}
Also used : Container(com.sun.appserv.management.base.Container) MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) SecurityMapConfig(com.sun.appserv.management.config.SecurityMapConfig) Method(java.lang.reflect.Method) AMX(com.sun.appserv.management.base.AMX) AMXConfig(com.sun.appserv.management.config.AMXConfig)

Aggregations

MBeanOperationInfo (javax.management.MBeanOperationInfo)117 MBeanInfo (javax.management.MBeanInfo)76 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)59 MBeanParameterInfo (javax.management.MBeanParameterInfo)38 ObjectName (javax.management.ObjectName)33 MBeanNotificationInfo (javax.management.MBeanNotificationInfo)23 ArrayList (java.util.ArrayList)22 MBeanConstructorInfo (javax.management.MBeanConstructorInfo)20 InstanceNotFoundException (javax.management.InstanceNotFoundException)19 ReflectionException (javax.management.ReflectionException)17 Test (org.junit.Test)16 IntrospectionException (javax.management.IntrospectionException)13 MBeanServer (javax.management.MBeanServer)13 MalformedObjectNameException (javax.management.MalformedObjectNameException)9 Test (org.testng.annotations.Test)9 Method (java.lang.reflect.Method)8 Attribute (javax.management.Attribute)8 Descriptor (javax.management.Descriptor)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)5