Search in sources :

Example 26 with MBeanOperationInfo

use of javax.management.MBeanOperationInfo in project jetty.project by eclipse.

the class ObjectMBean method defineOperation.

/* ------------------------------------------------------------ */
/**
     *  TODO update to new behavior
     *
     * Define an operation on the managed object. Defines an operation with parameters. Refection is
     * used to determine find the method and it's return type. The description of the method is
     * found with a call to findDescription on "name(signature)". The name and description of each
     * parameter is found with a call to findDescription with "name(signature)[n]", the returned
     * description is for the last parameter of the partial signature and is assumed to start with
     * the parameter name, followed by a colon.
     *
     * @param metaData "description" or "impact:description" or "type:impact:description", type is
     * the "Object","MBean", "MMBean" or "MObject" to indicate the method is on the object, the MBean or on the
     * object but converted to an MBean reference, and impact is either "ACTION","INFO","ACTION_INFO" or "UNKNOWN".
     */
private MBeanOperationInfo defineOperation(Method method, ManagedOperation methodAnnotation) {
    String description = methodAnnotation.value();
    boolean onMBean = methodAnnotation.proxied();
    boolean convert = false;
    // determine if we should convert
    Class<?> returnType = method.getReturnType();
    if (returnType.isArray()) {
        if (LOG.isDebugEnabled())
            LOG.debug("returnType is array, get component type");
        returnType = returnType.getComponentType();
    }
    if (returnType.isAnnotationPresent(ManagedObject.class)) {
        convert = true;
    }
    String impactName = methodAnnotation.impact();
    if (LOG.isDebugEnabled())
        LOG.debug("defineOperation {} {}:{}:{}", method.getName(), onMBean, impactName, description);
    String signature = method.getName();
    try {
        // Resolve the impact
        int impact = MBeanOperationInfo.UNKNOWN;
        if (impactName == null || impactName.equals("UNKNOWN"))
            impact = MBeanOperationInfo.UNKNOWN;
        else if (impactName.equals("ACTION"))
            impact = MBeanOperationInfo.ACTION;
        else if (impactName.equals("INFO"))
            impact = MBeanOperationInfo.INFO;
        else if (impactName.equals("ACTION_INFO"))
            impact = MBeanOperationInfo.ACTION_INFO;
        else
            LOG.warn("Unknown impact '" + impactName + "' for " + signature);
        Annotation[][] allParameterAnnotations = method.getParameterAnnotations();
        Class<?>[] methodTypes = method.getParameterTypes();
        MBeanParameterInfo[] pInfo = new MBeanParameterInfo[allParameterAnnotations.length];
        for (int i = 0; i < allParameterAnnotations.length; ++i) {
            Annotation[] parameterAnnotations = allParameterAnnotations[i];
            for (Annotation anno : parameterAnnotations) {
                if (anno instanceof Name) {
                    Name nameAnnotation = (Name) anno;
                    pInfo[i] = new MBeanParameterInfo(nameAnnotation.value(), methodTypes[i].getName(), nameAnnotation.description());
                }
            }
        }
        signature += "(";
        for (int i = 0; i < methodTypes.length; ++i) {
            signature += methodTypes[i].getName();
            if (i != methodTypes.length - 1) {
                signature += ",";
            }
        }
        signature += ")";
        Class<?> returnClass = method.getReturnType();
        if (LOG.isDebugEnabled())
            LOG.debug("Method Cache: " + signature);
        if (_methods.containsKey(signature)) {
            // we have an operation for this already
            return null;
        }
        _methods.put(signature, method);
        if (convert)
            _convert.add(signature);
        return new MBeanOperationInfo(method.getName(), description, pInfo, returnClass.isPrimitive() ? TypeUtil.toName(returnClass) : (returnClass.getName()), impact);
    } catch (Exception e) {
        LOG.warn("Operation '" + signature + "'", e);
        throw new IllegalArgumentException(e.toString());
    }
}
Also used : MBeanOperationInfo(javax.management.MBeanOperationInfo) Annotation(java.lang.annotation.Annotation) AttributeNotFoundException(javax.management.AttributeNotFoundException) ReflectionException(javax.management.ReflectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ObjectName(javax.management.ObjectName) Name(org.eclipse.jetty.util.annotation.Name) MBeanParameterInfo(javax.management.MBeanParameterInfo)

Example 27 with MBeanOperationInfo

use of javax.management.MBeanOperationInfo in project jetty.project by eclipse.

the class ObjectMBean method getMBeanInfo.

public MBeanInfo getMBeanInfo() {
    try {
        if (_info == null) {
            // Start with blank lazy lists attributes etc.
            String desc = null;
            List<MBeanAttributeInfo> attributes = new ArrayList<MBeanAttributeInfo>();
            List<MBeanConstructorInfo> constructors = new ArrayList<MBeanConstructorInfo>();
            List<MBeanOperationInfo> operations = new ArrayList<MBeanOperationInfo>();
            List<MBeanNotificationInfo> notifications = new ArrayList<MBeanNotificationInfo>();
            // Find list of classes that can influence the mbean
            Class<?> o_class = _managed.getClass();
            List<Class<?>> influences = new ArrayList<Class<?>>();
            // always add MBean itself
            influences.add(this.getClass());
            influences = findInfluences(influences, _managed.getClass());
            if (LOG.isDebugEnabled())
                LOG.debug("Influence Count: {}", influences.size());
            // Process Type Annotations
            ManagedObject primary = o_class.getAnnotation(ManagedObject.class);
            if (primary != null) {
                desc = primary.value();
            } else {
                if (LOG.isDebugEnabled())
                    LOG.debug("No @ManagedObject declared on {}", _managed.getClass());
            }
            // For each influence
            for (int i = 0; i < influences.size(); i++) {
                Class<?> oClass = influences.get(i);
                ManagedObject typeAnnotation = oClass.getAnnotation(ManagedObject.class);
                if (LOG.isDebugEnabled())
                    LOG.debug("Influenced by: " + oClass.getCanonicalName());
                if (typeAnnotation == null) {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Annotations not found for: {}", oClass.getCanonicalName());
                    continue;
                }
                for (Method method : oClass.getDeclaredMethods()) {
                    ManagedAttribute methodAttributeAnnotation = method.getAnnotation(ManagedAttribute.class);
                    if (methodAttributeAnnotation != null) {
                        // TODO sort out how a proper name could get here, its a method name as an attribute at this point.
                        if (LOG.isDebugEnabled())
                            LOG.debug("Attribute Annotation found for: {}", method.getName());
                        MBeanAttributeInfo mai = defineAttribute(method, methodAttributeAnnotation);
                        if (mai != null) {
                            attributes.add(mai);
                        }
                    }
                    ManagedOperation methodOperationAnnotation = method.getAnnotation(ManagedOperation.class);
                    if (methodOperationAnnotation != null) {
                        if (LOG.isDebugEnabled())
                            LOG.debug("Method Annotation found for: {}", method.getName());
                        MBeanOperationInfo oi = defineOperation(method, methodOperationAnnotation);
                        if (oi != null) {
                            operations.add(oi);
                        }
                    }
                }
            }
            _info = new MBeanInfo(o_class.getName(), desc, (MBeanAttributeInfo[]) attributes.toArray(new MBeanAttributeInfo[attributes.size()]), (MBeanConstructorInfo[]) constructors.toArray(new MBeanConstructorInfo[constructors.size()]), (MBeanOperationInfo[]) operations.toArray(new MBeanOperationInfo[operations.size()]), (MBeanNotificationInfo[]) notifications.toArray(new MBeanNotificationInfo[notifications.size()]));
        }
    } catch (RuntimeException e) {
        LOG.warn(e);
        throw e;
    }
    return _info;
}
Also used : MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ManagedAttribute(org.eclipse.jetty.util.annotation.ManagedAttribute) ManagedOperation(org.eclipse.jetty.util.annotation.ManagedOperation) MBeanConstructorInfo(javax.management.MBeanConstructorInfo) MBeanNotificationInfo(javax.management.MBeanNotificationInfo) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject)

Example 28 with MBeanOperationInfo

use of javax.management.MBeanOperationInfo in project jetty.project by eclipse.

the class ObjectMBeanTest method testDerivedOperations.

@Test
public void testDerivedOperations() throws Exception {
    Derived derived = new Derived();
    ObjectMBean mbean = (ObjectMBean) ObjectMBean.mbeanFor(derived);
    mbean.setMBeanContainer(container);
    container.beanAdded(null, derived);
    MBeanInfo info = mbean.getMBeanInfo();
    Assert.assertEquals("operation count does not match", 5, info.getOperations().length);
    MBeanOperationInfo[] opinfos = info.getOperations();
    boolean publish = false;
    boolean doodle = false;
    boolean good = false;
    for (int i = 0; i < opinfos.length; ++i) {
        MBeanOperationInfo opinfo = opinfos[i];
        if ("publish".equals(opinfo.getName())) {
            publish = true;
            Assert.assertEquals("description doesn't match", "publish something", opinfo.getDescription());
        }
        if ("doodle".equals(opinfo.getName())) {
            doodle = true;
            Assert.assertEquals("description doesn't match", "Doodle something", opinfo.getDescription());
            MBeanParameterInfo[] pinfos = opinfo.getSignature();
            Assert.assertEquals("parameter description doesn't match", "A description of the argument", pinfos[0].getDescription());
            Assert.assertEquals("parameter name doesn't match", "doodle", pinfos[0].getName());
        }
        // This is a proxied operation on the JMX wrapper
        if ("good".equals(opinfo.getName())) {
            good = true;
            Assert.assertEquals("description does not match", "test of proxy operations", opinfo.getDescription());
            Assert.assertEquals("execution contexts wrong", "not bad", mbean.invoke("good", new Object[] {}, new String[] {}));
        }
    }
    Assert.assertTrue("publish operation was not not found", publish);
    Assert.assertTrue("doodle operation was not not found", doodle);
    Assert.assertTrue("good operation was not not found", good);
}
Also used : Derived(com.acme.Derived) MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) MBeanParameterInfo(javax.management.MBeanParameterInfo) Test(org.junit.Test)

Example 29 with MBeanOperationInfo

use of javax.management.MBeanOperationInfo in project hazelcast by hazelcast.

the class HazelcastMBean method operationInfos.

private MBeanOperationInfo[] operationInfos() {
    MBeanOperationInfo[] array = new MBeanOperationInfo[operationMap.size()];
    int i = 0;
    for (BeanInfo beanInfo : operationMap.values()) {
        array[i++] = beanInfo.getOperationInfo();
    }
    return array;
}
Also used : MBeanOperationInfo(javax.management.MBeanOperationInfo) MBeanInfo(javax.management.MBeanInfo)

Example 30 with MBeanOperationInfo

use of javax.management.MBeanOperationInfo in project spring-framework by spring-projects.

the class MBeanClientInterceptor method retrieveMBeanInfo.

/**
	 * Loads the management interface info for the configured MBean into the caches.
	 * This information is used by the proxy when determining whether an invocation matches
	 * a valid operation or attribute on the management interface of the managed resource.
	 */
private void retrieveMBeanInfo() throws MBeanInfoRetrievalException {
    try {
        MBeanInfo info = this.serverToUse.getMBeanInfo(this.objectName);
        MBeanAttributeInfo[] attributeInfo = info.getAttributes();
        this.allowedAttributes = new HashMap<>(attributeInfo.length);
        for (MBeanAttributeInfo infoEle : attributeInfo) {
            this.allowedAttributes.put(infoEle.getName(), infoEle);
        }
        MBeanOperationInfo[] operationInfo = info.getOperations();
        this.allowedOperations = new HashMap<>(operationInfo.length);
        for (MBeanOperationInfo infoEle : operationInfo) {
            Class<?>[] paramTypes = JmxUtils.parameterInfoToTypes(infoEle.getSignature(), this.beanClassLoader);
            this.allowedOperations.put(new MethodCacheKey(infoEle.getName(), paramTypes), infoEle);
        }
    } catch (ClassNotFoundException ex) {
        throw new MBeanInfoRetrievalException("Unable to locate class specified in method signature", ex);
    } catch (IntrospectionException ex) {
        throw new MBeanInfoRetrievalException("Unable to obtain MBean info for bean [" + this.objectName + "]", ex);
    } catch (InstanceNotFoundException ex) {
        // if we are this far this shouldn't happen, but...
        throw new MBeanInfoRetrievalException("Unable to obtain MBean info for bean [" + this.objectName + "]: it is likely that this bean was unregistered during the proxy creation process", ex);
    } catch (ReflectionException ex) {
        throw new MBeanInfoRetrievalException("Unable to read MBean info for bean [ " + this.objectName + "]", ex);
    } catch (IOException ex) {
        throw new MBeanInfoRetrievalException("An IOException occurred when communicating with the " + "MBeanServer. It is likely that you are communicating with a remote MBeanServer. " + "Check the inner exception for exact details.", ex);
    }
}
Also used : ReflectionException(javax.management.ReflectionException) MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) InstanceNotFoundException(javax.management.InstanceNotFoundException) IntrospectionException(javax.management.IntrospectionException) IOException(java.io.IOException) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Aggregations

MBeanOperationInfo (javax.management.MBeanOperationInfo)46 MBeanInfo (javax.management.MBeanInfo)33 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)24 ObjectName (javax.management.ObjectName)18 MBeanParameterInfo (javax.management.MBeanParameterInfo)17 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)8 MBeanServer (javax.management.MBeanServer)8 MBeanConstructorInfo (javax.management.MBeanConstructorInfo)7 MBeanNotificationInfo (javax.management.MBeanNotificationInfo)7 ReflectionException (javax.management.ReflectionException)5 Method (java.lang.reflect.Method)4 Descriptor (javax.management.Descriptor)4 TreeMap (java.util.TreeMap)3 Attribute (javax.management.Attribute)3 InstanceNotFoundException (javax.management.InstanceNotFoundException)3 IntrospectionException (javax.management.IntrospectionException)3 MalformedObjectNameException (javax.management.MalformedObjectNameException)3 OpenType (javax.management.openmbean.OpenType)3 IOException (java.io.IOException)2