Search in sources :

Example 21 with MBeanException

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

the class ConnectionNotificationFilterImpl method stopRMIConnectorServer.

/** Stops the RMIConnectorServer and unregisters its MBean. */
private void stopRMIConnectorServer() {
    if (!this.agentConfig.isRmiEnabled())
        return;
    // stop the RMI Connector server...
    try {
        this.rmiConnector.stop();
    } catch (Exception e) {
        logger.warn(e.getMessage(), e);
    }
    try {
        ObjectName rmiRegistryNamingName = getRMIRegistryNamingName();
        if (this.agentConfig.isRmiRegistryEnabled() && mBeanServer.isRegistered(rmiRegistryNamingName)) {
            String[] empty = new String[0];
            mBeanServer.invoke(rmiRegistryNamingName, "stop", empty, empty);
            MBeanUtil.unregisterMBean(rmiRegistryNamingName);
        }
    } catch (MalformedObjectNameException e) {
        logger.warn(e.getMessage(), e);
    } catch (InstanceNotFoundException e) {
        logger.warn(e.getMessage(), e);
    } catch (ReflectionException e) {
        logger.warn(e.getMessage(), e);
    } catch (MBeanException e) {
        logger.warn(e.getMessage(), e);
    }
    try {
        ObjectName rmiConnectorServerName = getRMIConnectorServerName();
        if (mBeanServer.isRegistered(rmiConnectorServerName)) {
            MBeanUtil.unregisterMBean(rmiConnectorServerName);
        }
    } catch (MalformedObjectNameException e) {
        logger.warn(e.getMessage(), e);
    }
}
Also used : ReflectionException(javax.management.ReflectionException) MalformedObjectNameException(javax.management.MalformedObjectNameException) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException) OperationsException(javax.management.OperationsException) ReflectionException(javax.management.ReflectionException) AdminException(org.apache.geode.admin.AdminException) MalformedObjectNameException(javax.management.MalformedObjectNameException) GemFireException(org.apache.geode.GemFireException) GemFireIOException(org.apache.geode.GemFireIOException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) IOException(java.io.IOException) MBeanException(javax.management.MBeanException) ObjectName(javax.management.ObjectName)

Example 22 with MBeanException

use of javax.management.MBeanException 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 23 with MBeanException

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

the class MX4JModelMBean method resolveTargetObject.

private Object resolveTargetObject(Descriptor descriptor) throws MBeanException {
    Logger logger = getLogger();
    Object target = descriptor.getFieldValue("targetObject");
    if (logger.isEnabledFor(Logger.TRACE))
        logger.trace("targetObject is: " + target);
    if (target == null) {
        target = getManagedResource();
    } else {
        String targetObjectType = (String) descriptor.getFieldValue("targetObjectType");
        if (logger.isEnabledFor(Logger.TRACE))
            logger.trace("targetObjectType is: " + targetObjectType);
        if (targetObjectType == null) {
            // Not defined, assume object reference
            targetObjectType = OBJECT_RESOURCE_TYPE;
        }
        if (!isResourceTypeSupported(targetObjectType))
            throw new MBeanException(new InvalidTargetObjectTypeException(targetObjectType));
    }
    return target;
}
Also used : MBeanException(javax.management.MBeanException) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException)

Example 24 with MBeanException

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

the class MBeanProcessController method invokeOperationOnTargetMBean.

/**
   * Connects to the process and use its MBean to stop it.
   * 
   * @param namePattern the name pattern of the MBean to use for stopping
   * @param pidAttribute the name of the MBean attribute with the process id to compare against
   * @param methodName the name of the MBean operation to invoke
   * @param attributes the names of the MBean attributes to compare with expected values
   * @param values the expected values of the specified MBean attributes
   *
   * @throws ConnectionFailedException if there was a failure to connect to the local JMX connector
   *         in the process
   * @throws IOException if a communication problem occurred when talking to the MBean server
   * @throws MBeanInvocationFailedException if failed to invoke stop on the MBean for any reason
   */
private Object invokeOperationOnTargetMBean(final ObjectName namePattern, final String pidAttribute, final String methodName, final String[] attributes, final Object[] values) throws ConnectionFailedException, IOException, MBeanInvocationFailedException {
    ObjectName objectName = namePattern;
    connect();
    try {
        final QueryExp constraint = buildQueryExp(pidAttribute, attributes, values);
        final Set<ObjectName> mbeanNames = this.server.queryNames(namePattern, constraint);
        if (mbeanNames.isEmpty()) {
            throw new MBeanInvocationFailedException("Failed to find mbean matching '" + namePattern + "' with attribute '" + pidAttribute + "' of value '" + this.pid + "'");
        }
        if (mbeanNames.size() > 1) {
            throw new MBeanInvocationFailedException("Found more than one mbean matching '" + namePattern + "' with attribute '" + pidAttribute + "' of value '" + this.pid + "'");
        }
        objectName = mbeanNames.iterator().next();
        return invoke(objectName, methodName);
    } catch (InstanceNotFoundException e) {
        throw new MBeanInvocationFailedException("Failed to invoke " + methodName + " on " + objectName, e);
    } catch (MBeanException e) {
        throw new MBeanInvocationFailedException("Failed to invoke " + methodName + " on " + objectName, e);
    } catch (ReflectionException e) {
        throw new MBeanInvocationFailedException("Failed to invoke " + methodName + " on " + objectName, e);
    } finally {
        disconnect();
    }
}
Also used : ReflectionException(javax.management.ReflectionException) QueryExp(javax.management.QueryExp) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException) ObjectName(javax.management.ObjectName)

Example 25 with MBeanException

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

the class LocalProcessController method invokeOperationOnTargetMBean.

/**
   * Connects to the process and use its MBean to stop it.
   * 
   * @param namePattern the name pattern of the MBean to use for stopping
   * @param pidAttribute the name of the MBean attribute with the process id to compare against
   * @param methodName the name of the MBean operation to invoke
   * @param attributes the names of the MBean attributes to compare with expected values
   * @param values the expected values of the specified MBean attributes
   *
   * @throws ConnectionFailedException if there was a failure to connect to the local JMX connector
   *         in the process
   * @throws IOException if a communication problem occurred when talking to the MBean server
   * @throws MBeanInvocationFailedException if failed to invoke stop on the MBean for any reason
   * @throws PidUnavailableException if parsing the pid from the RuntimeMXBean name fails
   */
private Object invokeOperationOnTargetMBean(final ObjectName namePattern, final String pidAttribute, final String methodName, final String[] attributes, final Object[] values) throws ConnectionFailedException, IOException, MBeanInvocationFailedException, PidUnavailableException {
    ObjectName objectName = namePattern;
    connect();
    try {
        final QueryExp constraint = buildQueryExp(pidAttribute, attributes, values);
        final Set<ObjectName> mbeanNames = this.server.queryNames(namePattern, constraint);
        if (mbeanNames.isEmpty()) {
            throw new MBeanInvocationFailedException("Failed to find mbean matching '" + namePattern + "' with attribute '" + pidAttribute + "' of value '" + this.pid + "'");
        }
        if (mbeanNames.size() > 1) {
            throw new MBeanInvocationFailedException("Found more than one mbean matching '" + namePattern + "' with attribute '" + pidAttribute + "' of value '" + this.pid + "'");
        }
        objectName = mbeanNames.iterator().next();
        return invoke(objectName, methodName);
    } catch (InstanceNotFoundException e) {
        throw new MBeanInvocationFailedException("Failed to invoke " + methodName + " on " + objectName, e);
    } catch (MBeanException e) {
        throw new MBeanInvocationFailedException("Failed to invoke " + methodName + " on " + objectName, e);
    } catch (ReflectionException e) {
        throw new MBeanInvocationFailedException("Failed to invoke " + methodName + " on " + objectName, e);
    } finally {
        disconnect();
    }
}
Also used : ReflectionException(javax.management.ReflectionException) QueryExp(javax.management.QueryExp) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException) ObjectName(javax.management.ObjectName)

Aggregations

MBeanException (javax.management.MBeanException)101 ReflectionException (javax.management.ReflectionException)47 InstanceNotFoundException (javax.management.InstanceNotFoundException)39 AttributeNotFoundException (javax.management.AttributeNotFoundException)32 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)31 ObjectName (javax.management.ObjectName)31 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)25 InvocationTargetException (java.lang.reflect.InvocationTargetException)18 MalformedObjectNameException (javax.management.MalformedObjectNameException)17 RuntimeOperationsException (javax.management.RuntimeOperationsException)17 ServiceNotFoundException (javax.management.ServiceNotFoundException)17 RuntimeErrorException (javax.management.RuntimeErrorException)14 Attribute (javax.management.Attribute)12 Method (java.lang.reflect.Method)10 DynamicMBean (javax.management.DynamicMBean)10 ListenerNotFoundException (javax.management.ListenerNotFoundException)10 Descriptor (javax.management.Descriptor)9 MalformedURLException (java.net.MalformedURLException)7 MBeanRegistrationException (javax.management.MBeanRegistrationException)7 MBeanServer (javax.management.MBeanServer)7