Search in sources :

Example 31 with OpenDataException

use of javax.management.openmbean.OpenDataException in project jdk8u_jdk by JetBrains.

the class DefaultMXBeanMappingFactory method makeCompositeMapping.

private MXBeanMapping makeCompositeMapping(Class<?> c, MXBeanMappingFactory factory) throws OpenDataException {
    // For historical reasons GcInfo implements CompositeData but we
    // shouldn't count its CompositeData.getCompositeType() field as
    // an item in the computed CompositeType.
    final boolean gcInfoHack = (c.getName().equals("com.sun.management.GcInfo") && c.getClassLoader() == null);
    ReflectUtil.checkPackageAccess(c);
    final List<Method> methods = MBeanAnalyzer.eliminateCovariantMethods(Arrays.asList(c.getMethods()));
    final SortedMap<String, Method> getterMap = newSortedMap();
    /* Select public methods that look like "T getX()" or "boolean
           isX()", where T is not void and X is not the empty
           string.  Exclude "Class getClass()" inherited from Object.  */
    for (Method method : methods) {
        final String propertyName = propertyName(method);
        if (propertyName == null)
            continue;
        if (gcInfoHack && propertyName.equals("CompositeType"))
            continue;
        Method old = getterMap.put(decapitalize(propertyName), method);
        if (old != null) {
            final String msg = "Class " + c.getName() + " has method name clash: " + old.getName() + ", " + method.getName();
            throw new OpenDataException(msg);
        }
    }
    final int nitems = getterMap.size();
    if (nitems == 0) {
        throw new OpenDataException("Can't map " + c.getName() + " to an open data type");
    }
    final Method[] getters = new Method[nitems];
    final String[] itemNames = new String[nitems];
    final OpenType<?>[] openTypes = new OpenType<?>[nitems];
    int i = 0;
    for (Map.Entry<String, Method> entry : getterMap.entrySet()) {
        itemNames[i] = entry.getKey();
        final Method getter = entry.getValue();
        getters[i] = getter;
        final Type retType = getter.getGenericReturnType();
        openTypes[i] = factory.mappingForType(retType, factory).getOpenType();
        i++;
    }
    CompositeType compositeType = new CompositeType(c.getName(), c.getName(), // field names
    itemNames, // field descriptions
    itemNames, openTypes);
    return new CompositeMapping(c, compositeType, itemNames, getters, factory);
}
Also used : OpenType(javax.management.openmbean.OpenType) Method(java.lang.reflect.Method) GenericArrayType(java.lang.reflect.GenericArrayType) SimpleType(javax.management.openmbean.SimpleType) ArrayType(javax.management.openmbean.ArrayType) CompositeType(javax.management.openmbean.CompositeType) ElementType(java.lang.annotation.ElementType) OpenType(javax.management.openmbean.OpenType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TabularType(javax.management.openmbean.TabularType) OpenDataException(javax.management.openmbean.OpenDataException) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) SortedMap(java.util.SortedMap) CompositeType(javax.management.openmbean.CompositeType)

Example 32 with OpenDataException

use of javax.management.openmbean.OpenDataException in project jdk8u_jdk by JetBrains.

the class ThreadInfoCompositeData method getCompositeData.

protected CompositeData getCompositeData() {
    // Convert StackTraceElement[] to CompositeData[]
    StackTraceElement[] stackTrace = threadInfo.getStackTrace();
    CompositeData[] stackTraceData = new CompositeData[stackTrace.length];
    for (int i = 0; i < stackTrace.length; i++) {
        StackTraceElement ste = stackTrace[i];
        stackTraceData[i] = StackTraceElementCompositeData.toCompositeData(ste);
    }
    // Convert MonitorInfo[] and LockInfo[] to CompositeData[]
    CompositeData lockInfoData = LockInfoCompositeData.toCompositeData(threadInfo.getLockInfo());
    // Convert LockInfo[] and MonitorInfo[] to CompositeData[]
    LockInfo[] lockedSyncs = threadInfo.getLockedSynchronizers();
    CompositeData[] lockedSyncsData = new CompositeData[lockedSyncs.length];
    for (int i = 0; i < lockedSyncs.length; i++) {
        LockInfo li = lockedSyncs[i];
        lockedSyncsData[i] = LockInfoCompositeData.toCompositeData(li);
    }
    MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
    CompositeData[] lockedMonitorsData = new CompositeData[lockedMonitors.length];
    for (int i = 0; i < lockedMonitors.length; i++) {
        MonitorInfo mi = lockedMonitors[i];
        lockedMonitorsData[i] = MonitorInfoCompositeData.toCompositeData(mi);
    }
    // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
    // threadInfoItemNames!
    final Object[] threadInfoItemValues = { new Long(threadInfo.getThreadId()), threadInfo.getThreadName(), threadInfo.getThreadState().name(), new Long(threadInfo.getBlockedTime()), new Long(threadInfo.getBlockedCount()), new Long(threadInfo.getWaitedTime()), new Long(threadInfo.getWaitedCount()), lockInfoData, threadInfo.getLockName(), new Long(threadInfo.getLockOwnerId()), threadInfo.getLockOwnerName(), stackTraceData, new Boolean(threadInfo.isSuspended()), new Boolean(threadInfo.isInNative()), lockedMonitorsData, lockedSyncsData };
    try {
        return new CompositeDataSupport(threadInfoCompositeType, threadInfoItemNames, threadInfoItemValues);
    } catch (OpenDataException e) {
        // Should never reach here
        throw new AssertionError(e);
    }
}
Also used : MonitorInfo(java.lang.management.MonitorInfo) CompositeData(javax.management.openmbean.CompositeData) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) OpenDataException(javax.management.openmbean.OpenDataException) LockInfo(java.lang.management.LockInfo)

Example 33 with OpenDataException

use of javax.management.openmbean.OpenDataException in project jdk8u_jdk by JetBrains.

the class NotCompliantCauseTest method test1.

/**
     * Test that NotCompliantMBeanException has a cause in case of
     * type mapping problems.
     **/
void test1() {
    try {
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
        ObjectName oname = new ObjectName("domain:type=test");
        mbs.createMBean(NotCompliant.class.getName(), oname);
        System.err.println("ERROR: expected " + "NotCompliantMBeanException not thrown");
        throw new RuntimeTestException("NotCompliantMBeanException not thrown");
    } catch (RuntimeTestException e) {
        throw e;
    } catch (NotCompliantMBeanException e) {
        Throwable cause = e.getCause();
        if (cause == null)
            throw new RuntimeTestException("NotCompliantMBeanException " + "doesn't have any cause.", e);
        while (cause.getCause() != null) {
            if (cause instanceof OpenDataException)
                break;
            cause = cause.getCause();
        }
        if (!(cause instanceof OpenDataException))
            throw new RuntimeTestException("NotCompliantMBeanException " + "doesn't have expected cause (" + OpenDataException.class.getName() + "): " + cause, e);
        System.err.println("SUCCESS: Found expected cause: " + cause);
    } catch (Exception e) {
        System.err.println("Unexpected exception: " + e);
        throw new RuntimeException("Unexpected exception: " + e, e);
    }
}
Also used : OpenDataException(javax.management.openmbean.OpenDataException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) OpenDataException(javax.management.openmbean.OpenDataException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 34 with OpenDataException

use of javax.management.openmbean.OpenDataException in project jdk8u_jdk by JetBrains.

the class MXBeanRefTest method main.

public static void main(String[] args) throws Exception {
    MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    ObjectName productName = new ObjectName("d:type=Product,n=1");
    ObjectName product2Name = new ObjectName("d:type=Product,n=2");
    ObjectName moduleName = new ObjectName("d:type=Module");
    mbs.registerMBean(product, productName);
    mbs.registerMBean(product2, product2Name);
    mbs.registerMBean(module, moduleName);
    ModuleMXBean moduleProxy = JMX.newMXBeanProxy(mbs, moduleName, ModuleMXBean.class);
    ObjectName on;
    on = (ObjectName) mbs.getAttribute(moduleName, "Product");
    check("ObjectName attribute value", on.equals(productName));
    ProductMXBean productProxy = moduleProxy.getProduct();
    MBeanServerInvocationHandler mbsih = (MBeanServerInvocationHandler) Proxy.getInvocationHandler(productProxy);
    check("ObjectName in proxy", mbsih.getObjectName().equals(productName));
    mbs.setAttribute(moduleName, new Attribute("Product", product2Name));
    ProductMXBean product2Proxy = module.getProduct();
    mbsih = (MBeanServerInvocationHandler) Proxy.getInvocationHandler(product2Proxy);
    check("Proxy after setAttribute", mbsih.getObjectName().equals(product2Name));
    moduleProxy.setProduct(productProxy);
    ProductMXBean productProxyAgain = module.getProduct();
    mbsih = (MBeanServerInvocationHandler) Proxy.getInvocationHandler(productProxyAgain);
    check("Proxy after proxied set", mbsih.getObjectName().equals(productName));
    MBeanServer mbs2 = MBeanServerFactory.createMBeanServer();
    ProductMXBean productProxy2 = JMX.newMXBeanProxy(mbs2, productName, ProductMXBean.class);
    try {
        moduleProxy.setProduct(productProxy2);
        check("Proxy for wrong MBeanServer worked but shouldn't", false);
    } catch (Exception e) {
        if (e instanceof UndeclaredThrowableException && e.getCause() instanceof OpenDataException)
            check("Proxy for wrong MBeanServer correctly rejected", true);
        else {
            e.printStackTrace(System.out);
            check("Proxy for wrong MBeanServer got wrong exception", false);
        }
    }
    // Test 6283873
    ObjectName dup = new ObjectName("a:b=c");
    mbs.registerMBean(new MBeanServerDelegate(), dup);
    try {
        mbs.registerMBean(new ProductImpl(), dup);
        check("Duplicate register succeeded but should fail", false);
    } catch (InstanceAlreadyExistsException e) {
        check("Got correct exception from duplicate name", true);
    } catch (Exception e) {
        e.printStackTrace(System.out);
        check("Got wrong exception from duplicate name", false);
    }
    if (failure != null)
        throw new Exception("TEST FAILED: " + failure);
    System.out.println("TEST PASSED");
}
Also used : Attribute(javax.management.Attribute) OpenDataException(javax.management.openmbean.OpenDataException) MBeanServerDelegate(javax.management.MBeanServerDelegate) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) MBeanServerInvocationHandler(javax.management.MBeanServerInvocationHandler) OpenDataException(javax.management.openmbean.OpenDataException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 35 with OpenDataException

use of javax.management.openmbean.OpenDataException in project aries by apache.

the class BundleWiringData method getRequirementsCompositeData.

public static CompositeData[] getRequirementsCompositeData(List<BundleRequirement> bundleRequirements) {
    try {
        CompositeData[] data = new CompositeData[bundleRequirements.size()];
        for (int i = 0; i < bundleRequirements.size(); i++) {
            BundleRequirement requirement = bundleRequirements.get(i);
            CompositeData cd = BundleWiringData.getCapReqCompositeData(BundleWiringStateMBean.BUNDLE_REQUIREMENT_TYPE, requirement.getNamespace(), requirement.getAttributes().entrySet(), requirement.getDirectives().entrySet());
            data[i] = cd;
        }
        return data;
    } catch (OpenDataException e) {
        throw new IllegalStateException("Can't create CompositeData", e);
    }
}
Also used : OpenDataException(javax.management.openmbean.OpenDataException) CompositeData(javax.management.openmbean.CompositeData) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Aggregations

OpenDataException (javax.management.openmbean.OpenDataException)51 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)24 TabularDataSupport (javax.management.openmbean.TabularDataSupport)22 TabularType (javax.management.openmbean.TabularType)22 CompositeType (javax.management.openmbean.CompositeType)21 CompositeData (javax.management.openmbean.CompositeData)17 OpenType (javax.management.openmbean.OpenType)15 ObjectName (javax.management.ObjectName)8 HashMap (java.util.HashMap)6 Map (java.util.Map)5 MBeanServer (javax.management.MBeanServer)5 TabularData (javax.management.openmbean.TabularData)5 ArrayType (javax.management.openmbean.ArrayType)4 InvalidObjectException (java.io.InvalidObjectException)3 SortedMap (java.util.SortedMap)3 JMException (javax.management.JMException)3 StandardMBean (javax.management.StandardMBean)3 IOException (java.io.IOException)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 Method (java.lang.reflect.Method)2