Search in sources :

Example 1 with ServiceNotFoundException

use of javax.management.ServiceNotFoundException in project geode by apache.

the class MX4JModelMBean method invoke.

public Object invoke(String method, Object[] arguments, String[] params) throws MBeanException, ReflectionException {
    if (method == null)
        throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_METHOD_NAME_CANNOT_BE_NULL.toLocalizedString()));
    if (arguments == null)
        arguments = new Object[0];
    if (params == null)
        params = new String[0];
    Logger logger = getLogger();
    // Find operation descriptor
    ModelMBeanInfo info = getModelMBeanInfo();
    if (info == null)
        throw new MBeanException(new ServiceNotFoundException(LocalizedStrings.MX4JModelMBean_MODELMBEANINFO_IS_NULL.toLocalizedString()));
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("ModelMBeanInfo is: " + info);
    // This is a clone, we use it read only
    ModelMBeanOperationInfo operInfo = info.getOperation(method);
    if (operInfo == null)
        throw new MBeanException(new ServiceNotFoundException(LocalizedStrings.MX4JModelMBean_CANNOT_FIND_MODELMBEANOPERATIONINFO_FOR_OPERATION_0.toLocalizedString(method)));
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("Operation info is: " + operInfo);
    // This descriptor is a clone
    Descriptor operationDescriptor = operInfo.getDescriptor();
    if (operationDescriptor == null)
        throw new MBeanException(new ServiceNotFoundException(LocalizedStrings.MX4JModelMBean_OPERATION_DESCRIPTOR_FOR_OPERATION_0_CANNOT_BE_NULL.toLocalizedString(method)));
    String role = (String) operationDescriptor.getFieldValue("role");
    if (role == null || !role.equals("operation"))
        throw new MBeanException(new ServiceNotFoundException(LocalizedStrings.MX4JModelMBean_OPERATION_DESCRIPTOR_FIELD_ROLE_MUST_BE_OPERATION_NOT_0.toLocalizedString(role)));
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("Operation descriptor is: " + operationDescriptor);
    // This returns a clone of the mbean descriptor, we use it read only
    Descriptor mbeanDescriptor = info.getMBeanDescriptor();
    if (mbeanDescriptor == null)
        throw new MBeanException(new ServiceNotFoundException(LocalizedStrings.MX4JModelMBean_MBEAN_DESCRIPTOR_CANNOT_BE_NULL.toLocalizedString()));
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("MBean descriptor is: " + mbeanDescriptor);
    Object returnValue = null;
    String lastUpdateField = "lastReturnedTimeStamp";
    // Check if the method should be invoked given the cache settings
    int staleness = getStaleness(operationDescriptor, mbeanDescriptor, lastUpdateField);
    if (staleness == ALWAYS_STALE || staleness == STALE) {
        if (logger.isEnabledFor(Logger.TRACE))
            logger.trace("Value is stale");
        // Find parameters classes
        Class[] parameters = null;
        try {
            parameters = Utils.loadClasses(Thread.currentThread().getContextClassLoader(), params);
        } catch (ClassNotFoundException x) {
            logger.error(LocalizedStrings.MX4JModelMBean_CANNOT_FIND_OPERATIONS_PARAMETER_CLASSES, x);
            throw new ReflectionException(x);
        }
        if (logger.isEnabledFor(Logger.TRACE))
            logger.trace("Invoking operation...");
        // Find target object
        Object target = resolveTargetObject(operationDescriptor);
        returnValue = invokeMethod(target, method, parameters, arguments);
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Returned value is: " + returnValue);
        if (returnValue != null) {
            Class parameter = returnValue.getClass();
            Class declared = loadClassWithContextClassLoader(operInfo.getReturnType());
            checkAssignability(parameter, declared);
        }
        // Cache the new value only if caching is needed
        if (staleness != ALWAYS_STALE) {
            operationDescriptor.setField("lastReturnedValue", returnValue);
            operationDescriptor.setField(lastUpdateField, Long.valueOf(System.currentTimeMillis()));
            if (logger.isEnabledFor(Logger.TRACE)) {
                logger.trace("Returned value has been cached");
            }
            // And now replace the descriptor with the updated clone
            info.setDescriptor(operationDescriptor, "operation");
        }
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("invoke for operation " + method + " returns invoked value: " + returnValue);
    } else {
        // Return cached value
        returnValue = operationDescriptor.getFieldValue("lastReturnedValue");
        if (returnValue != null) {
            Class parameter = returnValue.getClass();
            Class declared = loadClassWithContextClassLoader(operInfo.getReturnType());
            checkAssignability(parameter, declared);
        }
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("invoke for operation " + method + " returns cached value: " + returnValue);
    }
    // As an extension, persist this model mbean also after operation invocation, but using only
    // settings provided in the operation descriptor, without falling back to defaults set in
    // the MBean descriptor
    boolean persistNow = shouldPersistNow(operationDescriptor, null, lastUpdateField);
    int impact = operInfo.getImpact();
    if (persistNow && impact != MBeanOperationInfo.INFO) {
        if (logger.isEnabledFor(Logger.TRACE))
            logger.trace("Persisting this ModelMBean...");
        try {
            store();
            if (logger.isEnabledFor(Logger.TRACE))
                logger.trace("ModelMBean persisted successfully");
        } catch (Exception x) {
            logger.error(LocalizedStrings.MX4JModelMBean_CANNOT_STORE_MODELMBEAN_AFTER_OPERATION_INVOCATION, x);
            if (x instanceof MBeanException)
                throw (MBeanException) x;
            else
                throw new MBeanException(x);
        }
    }
    return returnValue;
}
Also used : ModelMBeanOperationInfo(javax.management.modelmbean.ModelMBeanOperationInfo) ReflectionException(javax.management.ReflectionException) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) AttributeNotFoundException(javax.management.AttributeNotFoundException) ServiceNotFoundException(javax.management.ServiceNotFoundException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) RuntimeErrorException(javax.management.RuntimeErrorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedObjectNameException(javax.management.MalformedObjectNameException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ListenerNotFoundException(javax.management.ListenerNotFoundException) ImplementationException(mx4j.ImplementationException) RuntimeOperationsException(javax.management.RuntimeOperationsException) ServiceNotFoundException(javax.management.ServiceNotFoundException) MBeanException(javax.management.MBeanException) Descriptor(javax.management.Descriptor) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 2 with ServiceNotFoundException

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

the class SnmpMibAgent method setSnmpAdaptorName.

/**
     * Sets the reference to the SNMP protocol adaptor through which the MIB
     * will be SNMP accessible and add this new MIB in the SNMP MIB handler
     * associated to the specified <CODE>name</CODE>.
     *
     * @param name The name of the SNMP protocol adaptor.
     *
     * @exception InstanceNotFoundException The SNMP protocol adaptor does
     *     not exist in the MBean server.
     *
     * @exception ServiceNotFoundException This SNMP MIB is not registered
     *     in the MBean server or the requested service is not supported.
     */
@Override
public void setSnmpAdaptorName(ObjectName name) throws InstanceNotFoundException, ServiceNotFoundException {
    if (server == null) {
        throw new ServiceNotFoundException(mibName + " is not registered in the MBean server");
    }
    //
    if (adaptor != null) {
        adaptor.removeMib(this);
    }
    // Then update the reference to the new adaptor server.
    //
    Object[] params = { this };
    String[] signature = { "com.sun.jmx.snmp.agent.SnmpMibAgent" };
    try {
        adaptor = (SnmpMibHandler) (server.invoke(name, "addMib", params, signature));
    } catch (InstanceNotFoundException e) {
        throw new InstanceNotFoundException(name.toString());
    } catch (ReflectionException e) {
        throw new ServiceNotFoundException(name.toString());
    } catch (MBeanException e) {
    // Should never occur...
    }
    adaptorName = name;
}
Also used : ReflectionException(javax.management.ReflectionException) ServiceNotFoundException(javax.management.ServiceNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException)

Example 3 with ServiceNotFoundException

use of javax.management.ServiceNotFoundException in project tomcat by apache.

the class ManagedBean method getInvoke.

public Method getInvoke(String aname, Object[] params, String[] signature, BaseModelMBean bean, Object resource) throws MBeanException, ReflectionException {
    Method 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 = operations.get(createOperationKey(aname, signature));
    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 = bean;
        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");
    }
    return method;
}
Also used : MBeanOperationInfo(javax.management.MBeanOperationInfo) ReflectionException(javax.management.ReflectionException) Method(java.lang.reflect.Method) AttributeNotFoundException(javax.management.AttributeNotFoundException) ServiceNotFoundException(javax.management.ServiceNotFoundException) MBeanException(javax.management.MBeanException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) RuntimeOperationsException(javax.management.RuntimeOperationsException) ServiceNotFoundException(javax.management.ServiceNotFoundException) MBeanException(javax.management.MBeanException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 4 with ServiceNotFoundException

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

the class SnmpMibAgent method setSnmpAdaptorName.

/**
     * Sets the reference to the SNMP protocol adaptor through which the MIB
     * will be SNMP accessible and add this new MIB in the SNMP MIB handler
     * associated to the specified <CODE>name</CODE>.
     *
     * @param name The name of the SNMP protocol adaptor.
     * @param contextName The MIB context name. If null is passed, will be registered in the default context.
     * @exception InstanceNotFoundException The SNMP protocol adaptor does
     *     not exist in the MBean server.
     *
     * @exception ServiceNotFoundException This SNMP MIB is not registered
     *     in the MBean server or the requested service is not supported.
     *
     * @since 1.5
     */
@Override
public void setSnmpAdaptorName(ObjectName name, String contextName) throws InstanceNotFoundException, ServiceNotFoundException {
    if (server == null) {
        throw new ServiceNotFoundException(mibName + " is not registered in the MBean server");
    }
    //
    if (adaptor != null) {
        adaptor.removeMib(this, contextName);
    }
    // Then update the reference to the new adaptor server.
    //
    Object[] params = { this, contextName };
    String[] signature = { "com.sun.jmx.snmp.agent.SnmpMibAgent", "java.lang.String" };
    try {
        adaptor = (SnmpMibHandler) (server.invoke(name, "addMib", params, signature));
    } catch (InstanceNotFoundException e) {
        throw new InstanceNotFoundException(name.toString());
    } catch (ReflectionException e) {
        throw new ServiceNotFoundException(name.toString());
    } catch (MBeanException e) {
    // Should never occur...
    }
    adaptorName = name;
}
Also used : ReflectionException(javax.management.ReflectionException) ServiceNotFoundException(javax.management.ServiceNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException)

Example 5 with ServiceNotFoundException

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

the class SnmpMibAgent method setSnmpAdaptorName.

/**
     * Sets the reference to the SNMP protocol adaptor through which the MIB
     * will be SNMP accessible and add this new MIB in the SNMP MIB handler
     * associated to the specified <CODE>name</CODE>.
     * This method is to be called to set a specific agent to a specific OID. This can be useful when dealing with MIB overlapping.
     * Some OID can be implemented in more than one MIB. In this case, the OID nearer agent will be used on SNMP operations.
     * @param name The name of the SNMP protocol adaptor.
     * @param oids The set of OIDs this agent implements.
     * @exception InstanceNotFoundException The SNMP protocol adaptor does
     *     not exist in the MBean server.
     *
     * @exception ServiceNotFoundException This SNMP MIB is not registered
     *     in the MBean server or the requested service is not supported.
     *
     * @since 1.5
     */
@Override
public void setSnmpAdaptorName(ObjectName name, SnmpOid[] oids) throws InstanceNotFoundException, ServiceNotFoundException {
    if (server == null) {
        throw new ServiceNotFoundException(mibName + " is not registered in the MBean server");
    }
    //
    if (adaptor != null) {
        adaptor.removeMib(this);
    }
    // Then update the reference to the new adaptor server.
    //
    Object[] params = { this, oids };
    String[] signature = { "com.sun.jmx.snmp.agent.SnmpMibAgent", oids.getClass().getName() };
    try {
        adaptor = (SnmpMibHandler) (server.invoke(name, "addMib", params, signature));
    } catch (InstanceNotFoundException e) {
        throw new InstanceNotFoundException(name.toString());
    } catch (ReflectionException e) {
        throw new ServiceNotFoundException(name.toString());
    } catch (MBeanException e) {
    // Should never occur...
    }
    adaptorName = name;
}
Also used : ReflectionException(javax.management.ReflectionException) ServiceNotFoundException(javax.management.ServiceNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException)

Aggregations

ServiceNotFoundException (javax.management.ServiceNotFoundException)14 MBeanException (javax.management.MBeanException)12 ReflectionException (javax.management.ReflectionException)10 InstanceNotFoundException (javax.management.InstanceNotFoundException)9 AttributeNotFoundException (javax.management.AttributeNotFoundException)5 Descriptor (javax.management.Descriptor)5 RuntimeOperationsException (javax.management.RuntimeOperationsException)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)4 ObjectName (javax.management.ObjectName)4 RuntimeErrorException (javax.management.RuntimeErrorException)4 Method (java.lang.reflect.Method)3 ListenerNotFoundException (javax.management.ListenerNotFoundException)3 MBeanServer (javax.management.MBeanServer)3 IOException (java.io.IOException)2 AccessControlContext (java.security.AccessControlContext)2 Date (java.util.Date)2 Attribute (javax.management.Attribute)2 MBeanRegistrationException (javax.management.MBeanRegistrationException)2 MLet (javax.management.loading.MLet)2