Search in sources :

Example 91 with ReflectionException

use of javax.management.ReflectionException 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 92 with ReflectionException

use of javax.management.ReflectionException 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 93 with ReflectionException

use of javax.management.ReflectionException 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)

Example 94 with ReflectionException

use of javax.management.ReflectionException in project common by zenlunatics.

the class Admin method writeTomcatState.

// --------------------------------------------------------------------------
private void writeTomcatState(Request request) throws IOException {
    DecimalFormat df = new DecimalFormat();
    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    try {
        request.writer.h4("Memory");
        Table table = new Table(request.writer).addClass("table table-condensed table-striped").addStyle("width", "auto");
        Runtime runtime = Runtime.getRuntime();
        table.tr().td("max").td(df.format(runtime.maxMemory()));
        table.tr().td("total").td(df.format(runtime.totalMemory()));
        table.tr().td("free").td(df.format(runtime.freeMemory()));
        table.tr().td("used").td(df.format(runtime.totalMemory() - runtime.freeMemory()));
        table.close();
        request.writer.h4("Sessions");
        String context = request.getContext();
        if (context == null || context.length() == 0)
            context = "/";
        ObjectName objectName = new ObjectName("Catalina:type=Manager,context=" + context + ",host=localhost");
        table = new Table(request.writer).addClass("table table-condensed table-striped").addStyle("width", "auto");
        table.tr().td("count").td(mBeanServer.getAttribute(objectName, "sessionCounter").toString());
        table.tr().td("active").td(mBeanServer.getAttribute(objectName, "activeSessions").toString());
        table.tr().td("expired").td(mBeanServer.getAttribute(objectName, "expiredSessions").toString());
        table.close();
    } catch (MalformedObjectNameException | AttributeNotFoundException | InstanceNotFoundException | MBeanException | ReflectionException e) {
        System.out.println(e);
    }
}
Also used : ReflectionException(javax.management.ReflectionException) MalformedObjectNameException(javax.management.MalformedObjectNameException) AttributeNotFoundException(javax.management.AttributeNotFoundException) Table(web.Table) JDBCTable(db.JDBCTable) DecimalFormat(java.text.DecimalFormat) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 95 with ReflectionException

use of javax.management.ReflectionException in project tomcat70 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)

Aggregations

ReflectionException (javax.management.ReflectionException)367 InstanceNotFoundException (javax.management.InstanceNotFoundException)266 MBeanException (javax.management.MBeanException)239 AttributeNotFoundException (javax.management.AttributeNotFoundException)175 IntrospectionException (javax.management.IntrospectionException)115 Attribute (javax.management.Attribute)113 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)111 ObjectName (javax.management.ObjectName)100 AttributeList (javax.management.AttributeList)85 Test (org.testng.annotations.Test)83 MalformedObjectNameException (javax.management.MalformedObjectNameException)67 RuntimeOperationsException (javax.management.RuntimeOperationsException)64 IOException (java.io.IOException)60 MBeanInfo (javax.management.MBeanInfo)60 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)53 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)52 ListenerNotFoundException (javax.management.ListenerNotFoundException)42 InvocationTargetException (java.lang.reflect.InvocationTargetException)41 MBeanRegistrationException (javax.management.MBeanRegistrationException)38 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)34