Search in sources :

Example 26 with AttributeNotFoundException

use of javax.management.AttributeNotFoundException in project openj9 by eclipse.

the class TestMemoryPoolMXBean method testGetAttribute.

@Test
public final void testGetAttribute() {
    MemoryPoolMXBean testImpl = testBean;
    try {
        // The 14 good public attributes...
        {
            // null return here.
            try {
                CompositeData cd = (CompositeData) mbs.getAttribute(objName, "CollectionUsage");
                if (cd != null) {
                    logger.debug("CollectionUsage : cd=" + cd);
                    MemoryUsage mu = MemoryUsage.from(cd);
                    AssertJUnit.assertTrue(mu.getCommitted() != -1);
                    AssertJUnit.assertTrue(mu.getUsed() != -1);
                    logger.debug("CollectionUsage : " + mu);
                }
            } catch (Exception e) {
                // logger.debug("**Error TestMemoryPoolMXBean testImpl: "+ testImpl.getName() +", testGetAttribute() Exception: "+ e.getClass().getName());
                Assert.fail("Unexpected exception : " + e.getClass().getName());
            }
        }
        try {
            if (testImpl.isCollectionUsageThresholdSupported()) {
                Long l = (Long) mbs.getAttribute(objName, "CollectionUsageThreshold");
                AssertJUnit.assertNotNull(l);
                AssertJUnit.assertTrue(l > -1);
            } else {
                try {
                    Long l = (Long) mbs.getAttribute(objName, "CollectionUsageThreshold");
                } catch (Exception e) {
                    AssertJUnit.assertTrue(e instanceof javax.management.RuntimeMBeanException);
                    logger.debug("Exception occurred: as expected (collection usage threshold not supported).");
                }
            }
        // end else collection usage threshold is not supported
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            if (testImpl.isCollectionUsageThresholdSupported()) {
                Long l = (Long) mbs.getAttribute(objName, "CollectionUsageThresholdCount");
                AssertJUnit.assertNotNull(l);
                AssertJUnit.assertTrue(l > -1);
            } else {
                try {
                    Long l = (Long) mbs.getAttribute(objName, "CollectionUsageThresholdCount");
                } catch (Exception e) {
                    AssertJUnit.assertTrue(e instanceof javax.management.RuntimeMBeanException);
                    logger.debug("Exception occurred: as expected (collection usage threshold count not supported).");
                }
            }
        // end else collection usage threshold is not supported
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            String[] names = (String[]) mbs.getAttribute(objName, "MemoryManagerNames");
            AssertJUnit.assertNotNull(names);
            for (int i = 0; i < names.length; i++) {
                String string = names[i];
                AssertJUnit.assertNotNull(string);
                AssertJUnit.assertTrue(string.length() > 0);
                logger.debug("MemoryManagerNames[" + i + "] = " + string);
            }
        // end for
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            String name = (String) mbs.getAttribute(objName, "Name");
            AssertJUnit.assertNotNull(name);
            AssertJUnit.assertTrue(name.length() > 0);
            logger.debug("Name is " + name);
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            CompositeData cd = (CompositeData) mbs.getAttribute(objName, "PeakUsage");
            if (testImpl.isValid()) {
                AssertJUnit.assertNotNull(cd);
                MemoryUsage mu = MemoryUsage.from(cd);
                AssertJUnit.assertTrue(mu.getCommitted() != -1);
                AssertJUnit.assertTrue(mu.getUsed() != -1);
                logger.debug("PeakUsage : " + mu);
            } else {
                AssertJUnit.assertNull(cd);
            }
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            String name = (String) mbs.getAttribute(objName, "Type");
            AssertJUnit.assertNotNull(name);
            AssertJUnit.assertTrue(name.length() > 0);
            logger.debug("Type is " + name);
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            CompositeData cd = (CompositeData) mbs.getAttribute(objName, "Usage");
            if (testImpl.isValid()) {
                AssertJUnit.assertNotNull(cd);
                MemoryUsage mu = MemoryUsage.from(cd);
                AssertJUnit.assertTrue(mu.getCommitted() != -1);
                AssertJUnit.assertTrue(mu.getUsed() != -1);
                logger.debug("Usage : " + mu);
            } else {
                AssertJUnit.assertNull(cd);
            }
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            if (testImpl.isUsageThresholdSupported()) {
                Long l = (Long) mbs.getAttribute(objName, "UsageThreshold");
                AssertJUnit.assertNotNull(l);
                AssertJUnit.assertTrue(l > -1);
                logger.debug("Usage threshold = " + l);
            } else {
                try {
                    Long l = (Long) mbs.getAttribute(objName, "UsageThreshold");
                    Assert.fail("Unreacheable code: should have thrown exception");
                } catch (Exception e) {
                    AssertJUnit.assertTrue(e instanceof javax.management.RuntimeMBeanException);
                    logger.debug("Exception occurred: as expected (usage threshold not supported).");
                }
            }
        // end else usage threshold not supported
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            if (testImpl.isUsageThresholdSupported()) {
                Long l = (Long) mbs.getAttribute(objName, "UsageThresholdCount");
                AssertJUnit.assertNotNull(l);
                AssertJUnit.assertTrue(l > -1);
                logger.debug("Usage threshold count = " + l);
            } else {
                try {
                    Long l = (Long) mbs.getAttribute(objName, "UsageThresholdCount");
                    Assert.fail("Unreacheable code: should have thrown exception");
                } catch (Exception e) {
                    AssertJUnit.assertTrue(e instanceof javax.management.RuntimeMBeanException);
                    logger.debug("Exception occurred: as expected (usage threshold count not supported).");
                }
            }
        // end else usage threshold not supported
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            if (testImpl.isCollectionUsageThresholdSupported()) {
                Boolean b = (Boolean) mbs.getAttribute(objName, "CollectionUsageThresholdExceeded");
                AssertJUnit.assertNotNull(b);
                logger.debug("CollectionUsageThresholdExceeded is " + b);
            } else {
                try {
                    Boolean b = (Boolean) mbs.getAttribute(objName, "CollectionUsageThresholdExceeded");
                    Assert.fail("Unreacheable code: should have thrown exception");
                } catch (Exception e) {
                    AssertJUnit.assertTrue(e instanceof javax.management.RuntimeMBeanException);
                    logger.debug("Exception occurred: " + "as expected (collection usage threshold exceeded not supported).");
                }
            }
        // end else collection usage threshold not supported
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            Boolean b = (Boolean) mbs.getAttribute(objName, "CollectionUsageThresholdSupported");
            AssertJUnit.assertNotNull(b);
            logger.debug("CollectionUsageThresholdSupported is " + b);
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            if (testImpl.isUsageThresholdSupported()) {
                Boolean b = (Boolean) mbs.getAttribute(objName, "UsageThresholdExceeded");
                AssertJUnit.assertNotNull(b);
                logger.debug("UsageThresholdExceeded is " + b);
            } else {
                try {
                    Boolean b = (Boolean) mbs.getAttribute(objName, "UsageThresholdExceeded");
                    Assert.fail("Unreacheable code: should have thrown exception");
                } catch (Exception e) {
                    AssertJUnit.assertTrue(e instanceof javax.management.RuntimeMBeanException);
                    logger.debug("Exception occurred: as expected (usage threshold exceeded not supported).");
                }
            }
        // end else usage threshold not supported
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            Boolean b = (Boolean) mbs.getAttribute(objName, "UsageThresholdSupported");
            AssertJUnit.assertNotNull(b);
            logger.debug("UsageThresholdSupported is " + b);
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            Boolean b = (Boolean) mbs.getAttribute(objName, "Valid");
            AssertJUnit.assertNotNull(b);
            logger.debug("Valid is " + b);
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        // The 1 good IBM attribute ...
        try {
            // If not supported on the VM then permissible to get a
            // null return value.
            CompositeData cd = (CompositeData) mbs.getAttribute(objName, "PreCollectionUsage");
            if (null != cd) {
                MemoryUsage mu = MemoryUsage.from(cd);
                AssertJUnit.assertTrue(mu.getCommitted() != -1);
                AssertJUnit.assertTrue(mu.getUsed() != -1);
                logger.debug("(IBM) PreCollectionUsage : " + mu);
            }
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
    } catch (AttributeNotFoundException e) {
        Assert.fail("Unexpected AttributeNotFoundException : " + e.getMessage());
    } catch (MBeanException e) {
        Assert.fail("Unexpected MBeanException : " + e.getMessage());
    } catch (ReflectionException e) {
        Assert.fail("Unexpected ReflectionException : " + e.getMessage());
    }
    // A nonexistent attribute should throw an AttributeNotFoundException
    try {
        long rpm = ((Long) (mbs.getAttribute(objName, "RPM")));
        Assert.fail("Unreacheable code: should have thrown an AttributeNotFoundException.");
    } catch (Exception e) {
        AssertJUnit.assertTrue(e instanceof AttributeNotFoundException);
        logger.debug("Exception occurred: as expected, no such attribute found (RPM).");
    }
    // Type mismatch should result in a casting exception
    try {
        Long bad = (Long) (mbs.getAttribute(objName, "Name"));
        Assert.fail("Unreacheable code: should have thrown a ClassCastException");
    } catch (InstanceNotFoundException e) {
        Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
    } catch (Exception e) {
        AssertJUnit.assertTrue(e instanceof ClassCastException);
        logger.debug("Exception occurred: as expected, invalid type cast attempted.");
    }
}
Also used : ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) CompositeData(javax.management.openmbean.CompositeData) InstanceNotFoundException(javax.management.InstanceNotFoundException) MemoryUsage(java.lang.management.MemoryUsage) AttributeNotFoundException(javax.management.AttributeNotFoundException) IntrospectionException(javax.management.IntrospectionException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) MBeanException(javax.management.MBeanException) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) Test(org.testng.annotations.Test)

Example 27 with AttributeNotFoundException

use of javax.management.AttributeNotFoundException in project openj9 by eclipse.

the class TestMemoryPoolMXBean method testSetUsageThresholdAttribute.

@Test
public void testSetUsageThresholdAttribute() {
    MemoryPoolMXBean testImpl = testBean;
    if (testImpl.isUsageThresholdSupported()) {
        try {
            long originalUT = (Long) mbs.getAttribute(objName, "UsageThreshold");
            long newUT = originalUT + 1024;
            Attribute newUTAttr = new Attribute("UsageThreshold", new Long(newUT));
            mbs.setAttribute(objName, newUTAttr);
            AssertJUnit.assertEquals(new Long(newUT), (Long) mbs.getAttribute(objName, "UsageThreshold"));
        } catch (AttributeNotFoundException e) {
            e.printStackTrace();
            Assert.fail("Unexpected AttributeNotFoundException occurred: " + e.getMessage());
        } catch (InstanceNotFoundException e) {
            e.printStackTrace();
            Assert.fail("Unexpected InstanceNotFoundException occurred: " + e.getMessage());
        } catch (MBeanException e) {
            e.printStackTrace();
            Assert.fail("Unexpected MBeanException occurred: " + e.getMessage());
        } catch (ReflectionException e) {
            e.printStackTrace();
            Assert.fail("Unexpected ReflectionException occurred: " + e.getMessage());
        } catch (InvalidAttributeValueException e) {
            e.printStackTrace();
            Assert.fail("Unexpected InvalidAttributeValueException occurred: " + e.getMessage());
        }
    } else {
        try {
            Attribute newUTAttr = new Attribute("UsageThreshold", new Long(100 * 1024));
            mbs.setAttribute(objName, newUTAttr);
            Assert.fail("Unreacheable code: should have thrown exception!");
        } catch (Exception e) {
            AssertJUnit.assertTrue(e instanceof javax.management.RuntimeMBeanException);
            logger.debug("Exception occurred, as expected: cannot set attribute (UsageThreshold).");
        }
    }
// end else usage threshold is not supported
}
Also used : ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) Attribute(javax.management.Attribute) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) AttributeNotFoundException(javax.management.AttributeNotFoundException) IntrospectionException(javax.management.IntrospectionException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) Test(org.testng.annotations.Test)

Example 28 with AttributeNotFoundException

use of javax.management.AttributeNotFoundException in project tomcat70 by apache.

the class ContextResourceMBean method getAttribute.

// ----------------------------------------------------- Instance Variables
// ------------------------------------------------------------- Attributes
/**
 * Obtain and return the value of a specific attribute of this MBean.
 *
 * @param name Name of the requested attribute
 *
 * @exception AttributeNotFoundException if this attribute is not
 *  supported by this MBean
 * @exception MBeanException if the initializer of an object
 *  throws an exception
 * @exception ReflectionException if a Java reflection exception
 *  occurs when invoking the getter
 */
@Override
public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException {
    // Validate the input parameters
    if (name == null)
        throw new RuntimeOperationsException(new IllegalArgumentException("Attribute name is null"), "Attribute name is null");
    ContextResource cr = null;
    try {
        cr = (ContextResource) getManagedResource();
    } catch (InstanceNotFoundException e) {
        throw new MBeanException(e);
    } catch (InvalidTargetObjectTypeException e) {
        throw new MBeanException(e);
    }
    String value = null;
    if ("auth".equals(name)) {
        return (cr.getAuth());
    } else if ("description".equals(name)) {
        return (cr.getDescription());
    } else if ("name".equals(name)) {
        return (cr.getName());
    } else if ("scope".equals(name)) {
        return (cr.getScope());
    } else if ("type".equals(name)) {
        return (cr.getType());
    } else {
        value = (String) cr.getProperty(name);
        if (value == null) {
            throw new AttributeNotFoundException("Cannot find attribute " + name);
        }
    }
    return value;
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) RuntimeOperationsException(javax.management.RuntimeOperationsException) ContextResource(org.apache.catalina.deploy.ContextResource)

Example 29 with AttributeNotFoundException

use of javax.management.AttributeNotFoundException in project tomcat70 by apache.

the class ManagedBean method getSetter.

public Method getSetter(String aname, BaseModelMBean bean, Object resource) throws AttributeNotFoundException, ReflectionException {
    Method m = null;
    AttributeInfo attrInfo = attributes.get(aname);
    if (attrInfo == null)
        throw new AttributeNotFoundException(" Cannot find attribute " + aname);
    // Look up the actual operation to be used
    String setMethod = attrInfo.getSetMethod();
    if (setMethod == null)
        throw new AttributeNotFoundException("Cannot find attribute " + aname + " set method name");
    String argType = attrInfo.getType();
    Class<?>[] signature = new Class[] { BaseModelMBean.getAttributeClass(argType) };
    Object object = null;
    NoSuchMethodException exception = null;
    try {
        object = bean;
        m = object.getClass().getMethod(setMethod, signature);
    } catch (NoSuchMethodException e) {
        exception = e;
    }
    if (m == null && resource != null) {
        try {
            object = resource;
            m = object.getClass().getMethod(setMethod, signature);
            exception = null;
        } catch (NoSuchMethodException e) {
            exception = e;
        }
    }
    if (exception != null)
        throw new ReflectionException(exception, "Cannot find setter method " + setMethod + " " + resource);
    return m;
}
Also used : MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) Method(java.lang.reflect.Method)

Example 30 with AttributeNotFoundException

use of javax.management.AttributeNotFoundException in project tomcat70 by apache.

the class BaseModelMBean method setAttribute.

/**
 * Set the value of a specific attribute of this MBean.
 *
 * @param attribute The identification of the attribute to be set
 *  and the new value
 *
 * @exception AttributeNotFoundException if this attribute is not
 *  supported by this MBean
 * @exception MBeanException if the initializer of an object
 *  throws an exception
 * @exception ReflectionException if a Java reflection exception
 *  occurs when invoking the getter
 */
@Override
public void setAttribute(Attribute attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
    if (log.isDebugEnabled())
        log.debug("Setting attribute " + this + " " + attribute);
    if ((resource instanceof DynamicMBean) && !(resource instanceof BaseModelMBean)) {
        try {
            ((DynamicMBean) resource).setAttribute(attribute);
        } catch (InvalidAttributeValueException e) {
            throw new MBeanException(e);
        }
        return;
    }
    // Validate the input parameters
    if (attribute == null)
        throw new RuntimeOperationsException(new IllegalArgumentException("Attribute is null"), "Attribute is null");
    String name = attribute.getName();
    Object value = attribute.getValue();
    if (name == null)
        throw new RuntimeOperationsException(new IllegalArgumentException("Attribute name is null"), "Attribute name is null");
    Object oldValue = null;
    // if( getAttMap.get(name) != null )
    // oldValue=getAttribute( name );
    Method m = managedBean.getSetter(name, this, resource);
    try {
        if (m.getDeclaringClass().isAssignableFrom(this.getClass())) {
            m.invoke(this, new Object[] { value });
        } else {
            m.invoke(resource, new Object[] { value });
        }
    } catch (InvocationTargetException e) {
        Throwable t = e.getTargetException();
        if (t == null)
            t = e;
        if (t instanceof RuntimeException)
            throw new RuntimeOperationsException((RuntimeException) t, "Exception invoking method " + name);
        else if (t instanceof Error)
            throw new RuntimeErrorException((Error) t, "Error invoking method " + name);
        else
            throw new MBeanException(e, "Exception invoking method " + name);
    } catch (Exception e) {
        log.error("Exception invoking method " + name, e);
        throw new MBeanException(e, "Exception invoking method " + name);
    }
    try {
        sendAttributeChangeNotification(new Attribute(name, oldValue), attribute);
    } catch (Exception ex) {
        log.error("Error sending notification " + name, ex);
    }
// attributes.put( name, value );
// if( source != null ) {
// // this mbean is associated with a source - maybe we want to persist
// source.updateField(oname, name, value);
// }
}
Also used : DynamicMBean(javax.management.DynamicMBean) RuntimeErrorException(javax.management.RuntimeErrorException) Attribute(javax.management.Attribute) Method(java.lang.reflect.Method) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AttributeNotFoundException(javax.management.AttributeNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) RuntimeErrorException(javax.management.RuntimeErrorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ListenerNotFoundException(javax.management.ListenerNotFoundException) RuntimeOperationsException(javax.management.RuntimeOperationsException) MBeanException(javax.management.MBeanException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Aggregations

AttributeNotFoundException (javax.management.AttributeNotFoundException)78 ReflectionException (javax.management.ReflectionException)57 MBeanException (javax.management.MBeanException)53 InstanceNotFoundException (javax.management.InstanceNotFoundException)40 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)31 Attribute (javax.management.Attribute)25 ObjectName (javax.management.ObjectName)24 RuntimeOperationsException (javax.management.RuntimeOperationsException)16 IntrospectionException (javax.management.IntrospectionException)13 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)13 Method (java.lang.reflect.Method)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)11 AttributeList (javax.management.AttributeList)11 MalformedObjectNameException (javax.management.MalformedObjectNameException)11 Test (org.testng.annotations.Test)9 MBeanInfo (javax.management.MBeanInfo)8 ListenerNotFoundException (javax.management.ListenerNotFoundException)7 RuntimeErrorException (javax.management.RuntimeErrorException)7 RuntimeMBeanException (javax.management.RuntimeMBeanException)7 InvalidTargetObjectTypeException (javax.management.modelmbean.InvalidTargetObjectTypeException)7