Search in sources :

Example 16 with MBeanException

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

the class MBeanExceptionTest method test.

private static int test(MBeanServer mbs, ObjectName name, boolean testChecked) throws Exception {
    System.out.println("--------" + name + "--------");
    int failures = 0;
    final String[] ops = { "getAttribute", "setAttribute", "invoke" };
    final int GET = 0, SET = 1, INVOKE = 2;
    final String[] targets = { "UncheckedException", "CheckedException" };
    final int UNCHECKED = 0, CHECKED = 1;
    for (int i = 0; i < ops.length; i++) {
        for (int j = 0; j < targets.length; j++) {
            if (j == CHECKED && !testChecked)
                continue;
            String target = targets[j];
            String what = ops[i] + "/" + target;
            System.out.println(what);
            try {
                switch(i) {
                    case GET:
                        mbs.getAttribute(name, target);
                        break;
                    case SET:
                        mbs.setAttribute(name, new Attribute(target, "x"));
                        break;
                    case INVOKE:
                        mbs.invoke(name, target, null, null);
                        break;
                    default:
                        throw new AssertionError();
                }
                System.out.println("failure: " + what + " returned!");
                failures++;
            } catch (RuntimeMBeanException e) {
                if (j == CHECKED) {
                    System.out.println("failure: RuntimeMBeanException " + "when checked expected: " + e);
                    failures++;
                } else {
                    Throwable cause = e.getCause();
                    if (cause == theUncheckedException)
                        System.out.println("ok: " + what);
                    else {
                        System.out.println("failure: " + what + " wrapped " + cause);
                        failures++;
                    }
                }
            } catch (MBeanException e) {
                if (j == UNCHECKED) {
                    System.out.println("failure: checked exception " + "when unchecked expected: " + e);
                    failures++;
                } else {
                    Throwable cause = e.getCause();
                    if (cause == theCheckedException)
                        System.out.println("ok: " + what);
                    else {
                        System.out.println("failure: " + what + " wrapped " + cause);
                        failures++;
                    }
                }
            } catch (Throwable t) {
                System.out.println("failure: " + what + " threw: " + t);
                while ((t = t.getCause()) != null) System.out.println("  ... " + t);
                failures++;
            }
        }
    }
    return failures;
}
Also used : RuntimeMBeanException(javax.management.RuntimeMBeanException) Attribute(javax.management.Attribute) MBeanException(javax.management.MBeanException) RuntimeMBeanException(javax.management.RuntimeMBeanException)

Example 17 with MBeanException

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

the class DynamicMBeanWrapper method invoke.

@Override
public Object invoke(final String actionName, final Object[] params, final String[] signature) throws MBeanException, ReflectionException {
    if (operations.containsKey(actionName)) {
        final ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(classloader);
        try {
            final Operation operation = operations.get(actionName);
            final Object result = operation.getOperation().invoke(instance(), params);
            return operation.isPresentAsTabularIfPossible() ? toResult(actionName, result) : result;
        } catch (InvocationTargetException e) {
            final Throwable cause = e.getCause();
            if (cause instanceof Error) {
                throw (Error) cause;
            }
            if (cause instanceof MBeanException) {
                throw (MBeanException) cause;
            }
            throw new MBeanException((Exception) cause, actionName + " failed with exception");
        } catch (IllegalAccessException e) {
            throw new ReflectionException(e, actionName + " could not be invoked");
        } catch (IllegalArgumentException e) {
            throw new ReflectionException(e, actionName + " could not be invoked");
        } finally {
            Thread.currentThread().setContextClassLoader(oldCl);
        }
    }
    throw new ReflectionException(new NoSuchMethodException(actionName + " doesn't exist"));
}
Also used : ReflectionException(javax.management.ReflectionException) MBeanException(javax.management.MBeanException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ReflectionException(javax.management.ReflectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) OpenDataException(javax.management.openmbean.OpenDataException) AttributeNotFoundException(javax.management.AttributeNotFoundException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException)

Example 18 with MBeanException

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

the class MX4JModelMBean method findLogger.

private Logger findLogger(Descriptor descriptor) {
    Logger logger = getLogger();
    if (descriptor == null) {
        if (logger.isEnabledFor(Logger.TRACE))
            logger.trace("Can't find MBean logger, descriptor is null");
        return null;
    }
    String log = (String) descriptor.getFieldValue("log");
    String location = (String) descriptor.getFieldValue("logFile");
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("Log fields: log=" + log + ", file=" + location);
    if (log == null || !Boolean.valueOf(log).booleanValue()) {
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Logging is not supported by this ModelMBean");
        return null;
    }
    // Logger is supported, where log to ?
    if (location == null) {
        // As an extension, see if the field logMBean has been defined
        location = (String) descriptor.getFieldValue("logMBean");
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Log fields: mbean=" + location);
        if (location == null) {
            if (logger.isEnabledFor(Logger.TRACE))
                logger.trace("Logging is not supported by this ModelMBean");
            return null;
        }
        // It seems that the user wants to delegate a registered mbean to log
        try {
            ObjectName objectName = new ObjectName(location);
            MBeanServer server = getMBeanServer();
            if (server == null)
                throw new MBeanException(new IllegalStateException(LocalizedStrings.MX4JModelMBean_MX4JMODELMBEAN_IS_NOT_REGISTERED.toLocalizedString()));
            if (server.isRegistered(objectName)) {
                MBeanLogger l = new MBeanLogger(server, objectName);
                if (logger.isEnabledFor(Logger.DEBUG))
                    logger.debug("ModelMBean log supported by delegating to this MBean: " + objectName);
                return l;
            }
            return null;
        } catch (MalformedObjectNameException x) {
            // Ah, was not a correct object name
            if (logger.isEnabledFor(Logger.DEBUG))
                logger.debug("Specified logMBean field does not contain a valid ObjectName: " + location);
            return null;
        } catch (MBeanException x) {
            if (logger.isEnabledFor(Logger.DEBUG))
                logger.debug("logMBean field does not specify an MBean that supports logging delegation", x);
            return null;
        }
    } else {
        // User decided to log to a file
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("ModelMBean log supported on file system");
        return new FileLogger(location);
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanLogger(mx4j.log.MBeanLogger) MBeanException(javax.management.MBeanException) FileLogger(mx4j.log.FileLogger) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer)

Example 19 with MBeanException

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

the class MX4JModelMBean method findPersister.

private PersisterMBean findPersister() throws MBeanException, InstanceNotFoundException {
    Logger logger = getLogger();
    ModelMBeanInfo info = getModelMBeanInfo();
    if (info == null) {
        // Not yet initialized
        if (logger.isEnabledFor(Logger.TRACE))
            logger.trace("Can't find persister, ModelMBeanInfo is null");
        return null;
    }
    Descriptor mbeanDescriptor = info.getMBeanDescriptor();
    if (mbeanDescriptor == null) {
        // This is normally should not happen if ModelMBeanInfoSupport is used
        if (logger.isEnabledFor(Logger.TRACE))
            logger.trace("Can't find persister, MBean descriptor is null");
        return null;
    }
    String location = (String) mbeanDescriptor.getFieldValue("persistLocation");
    String name = (String) mbeanDescriptor.getFieldValue("persistName");
    String mbeanName = (String) mbeanDescriptor.getFieldValue("name");
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("Persistence fields: location=" + location + ", name=" + name);
    if (mbeanName == null && name == null) {
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Persistence is not supported by this ModelMBean");
        return null;
    }
    // Try to see if this mbean should delegate to another mbean
    if (name != null) {
        try {
            ObjectName objectName = new ObjectName(name.trim());
            // OK, a valid object name
            MBeanServer server = getMBeanServer();
            if (server == null)
                throw new MBeanException(new IllegalStateException(LocalizedStrings.MX4JModelMBean_MX4JMODELMBEAN_IS_NOT_REGISTERED.toLocalizedString()));
            if (server.isRegistered(objectName) && server.isInstanceOf(objectName, PersisterMBean.class.getName())) {
                // OK, the given mbean is registered with this mbean server
                PersisterMBean persister = new MBeanPersister(server, objectName);
                if (logger.isEnabledFor(Logger.DEBUG))
                    logger.debug("Persistence is delegated to this MBean: " + objectName);
                return persister;
            } else {
                throw new InstanceNotFoundException(objectName.toString());
            }
        } catch (MalformedObjectNameException ignored) {
            // It does not delegates to another mbean, use default
            if (logger.isEnabledFor(Logger.TRACE))
                logger.trace("Persistence is not delegated to another MBean");
        }
        // Default is serialization to file
        FilePersister persister = new FilePersister(location, name);
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Persistence is realized through file system in " + persister.getFileName());
        return persister;
    } else {
        // Only location given, use MBean name
        FilePersister persister = new FilePersister(location, mbeanName);
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Persistence is realized through file system in " + persister.getFileName());
        return persister;
    }
}
Also used : MBeanPersister(mx4j.persist.MBeanPersister) MalformedObjectNameException(javax.management.MalformedObjectNameException) PersisterMBean(mx4j.persist.PersisterMBean) FilePersister(mx4j.persist.FilePersister) InstanceNotFoundException(javax.management.InstanceNotFoundException) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) ObjectName(javax.management.ObjectName) Descriptor(javax.management.Descriptor) MBeanException(javax.management.MBeanException) MBeanServer(javax.management.MBeanServer)

Example 20 with MBeanException

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

the class MX4JModelMBean method setAttribute.

public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
    if (attribute == null)
        throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_ATTRIBUTE_CANNOT_BE_NULL.toLocalizedString()));
    Logger logger = getLogger();
    // No need to synchronize: I work mostly on clones
    // I want the real info, not its clone
    ModelMBeanInfo info = getModelMBeanInfo();
    if (info == null)
        throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_MODELMBEANINFO_IS_NULL.toLocalizedString());
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("ModelMBeanInfo is: " + info);
    String attrName = attribute.getName();
    Object attrValue = attribute.getValue();
    // This is a clone, we use it read only
    ModelMBeanAttributeInfo attrInfo = info.getAttribute(attrName);
    if (attrInfo == null)
        throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_CANNOT_FIND_MODELMBEANATTRIBUTEINFO_FOR_ATTRIBUTE_0.toLocalizedString(attrName));
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("Attribute info is: " + attrInfo);
    if (!attrInfo.isWritable())
        throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_ATTRIBUTE_0_IS_NOT_WRITABLE.toLocalizedString(attrName));
    // This returns a clone of the mbean descriptor, we use it read only
    Descriptor mbeanDescriptor = info.getMBeanDescriptor();
    if (mbeanDescriptor == null)
        throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_MBEAN_DESCRIPTOR_CANNOT_BE_NULL.toLocalizedString());
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("MBean descriptor is: " + mbeanDescriptor);
    // This descriptor is a clone
    Descriptor attributeDescriptor = attrInfo.getDescriptor();
    if (attributeDescriptor == null)
        throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_ATTRIBUTE_DESCRIPTOR_FOR_ATTRIBUTE_0_CANNOT_BE_NULL.toLocalizedString(attrName));
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("Attribute descriptor is: " + attributeDescriptor);
    String lastUpdateField = "lastUpdatedTimeStamp";
    Object oldValue = null;
    try {
        oldValue = getAttribute(attrName);
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Previous value of attribute " + attrName + ": " + oldValue);
    } catch (Exception x) {
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Cannot get previous value of attribute " + attrName, x);
    }
    // Check if setMethod is present
    String method = (String) attributeDescriptor.getFieldValue("setMethod");
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("setMethod field is: " + method);
    if (method != null) {
        Class declared = loadClassWithContextClassLoader(attrInfo.getType());
        if (attrValue != null) {
            Class parameter = attrValue.getClass();
            checkAssignability(parameter, declared);
        }
        // As an extension, allow attributes to be called on target objects also
        Object target = resolveTargetObject(attributeDescriptor);
        invokeMethod(target, method, new Class[] { declared }, new Object[] { attrValue });
        // Cache the value only if currencyTimeLimit is not 0, ie it is not always stale
        int staleness = getStaleness(attributeDescriptor, mbeanDescriptor, lastUpdateField);
        if (staleness != ALWAYS_STALE) {
            attributeDescriptor.setField("value", attrValue);
            attributeDescriptor.setField(lastUpdateField, Long.valueOf(System.currentTimeMillis()));
            if (logger.isEnabledFor(Logger.TRACE))
                logger.trace("Attribute's value has been cached");
        } else {
            if (logger.isEnabledFor(Logger.TRACE))
                logger.trace("Always stale, avoiding to cache attribute's value");
        }
    } else {
        if (attrValue != null) {
            Class parameter = attrValue.getClass();
            Class declared = loadClassWithContextClassLoader(attrInfo.getType());
            checkAssignability(parameter, declared);
        }
        // Always store the value in the descriptor: no setMethod
        attributeDescriptor.setField("value", attrValue);
    }
    // And now replace the descriptor with the updated clone
    info.setDescriptor(attributeDescriptor, "attribute");
    // Send notifications to listeners
    if (logger.isEnabledFor(Logger.TRACE))
        logger.trace("Sending attribute change notifications");
    sendAttributeChangeNotification(new Attribute(attrName, oldValue), attribute);
    // Persist this ModelMBean
    boolean persistNow = shouldPersistNow(attributeDescriptor, mbeanDescriptor, lastUpdateField);
    if (persistNow) {
        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_SETATTRIBUTE, x);
            if (x instanceof MBeanException)
                throw (MBeanException) x;
            else
                throw new MBeanException(x);
        }
    }
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) Attribute(javax.management.Attribute) 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) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) Descriptor(javax.management.Descriptor) MBeanException(javax.management.MBeanException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

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