Search in sources :

Example 86 with OpenDataException

use of javax.management.openmbean.OpenDataException in project fabric8 by jboss-fuse.

the class OpenTypeGenerator method toOpenData.

public static Object toOpenData(OpenType<?> otype, Object value) throws OpenDataException {
    Object result;
    if (value != null && !otype.isValue(value)) {
        if (otype instanceof CompositeType) {
            result = toCompositeData((CompositeType) otype, value);
        } else if (otype instanceof TabularType) {
            result = toTabularData((TabularType) otype, (Map<?, ?>) value);
        } else if (otype instanceof ArrayType) {
            result = toArrayData((ArrayType<?>) otype, value);
        } else if (otype == SimpleType.BYTE && value instanceof Number) {
            result = Byte.parseByte(value.toString());
        } else {
            throw new OpenDataException("Unsupported open type: " + otype);
        }
    } else {
        result = value;
    }
    boolean isAssignable = result == null || otype.isValue(result);
    IllegalStateAssertion.assertTrue(isAssignable, "Value " + result + " is not a value of: " + otype);
    return result;
}
Also used : ArrayType(javax.management.openmbean.ArrayType) OpenDataException(javax.management.openmbean.OpenDataException) TabularType(javax.management.openmbean.TabularType) CompositeType(javax.management.openmbean.CompositeType)

Example 87 with OpenDataException

use of javax.management.openmbean.OpenDataException in project fabric8 by jboss-fuse.

the class OpenTypeGenerator method getOpenTypeArray.

static Object getOpenTypeArray(ArrayType<?> atype, ClassLoader classLoader, int dimension) throws OpenDataException {
    Class<?> compType;
    OpenType<?> elementType = atype.getElementOpenType();
    try {
        if (atype.isPrimitiveArray()) {
            compType = Class.forName(atype.getTypeName()).getComponentType();
        } else if (elementType instanceof CompositeType) {
            compType = CompositeData.class;
        } else if (elementType instanceof TabularType) {
            compType = TabularData.class;
        } else {
            compType = classLoader.loadClass(elementType.getTypeName());
        }
    } catch (ClassNotFoundException ex) {
        OpenDataException odex = new OpenDataException("Cannot load array type: " + atype);
        odex.initCause(ex);
        throw odex;
    }
    return Array.newInstance(compType, dimension);
}
Also used : OpenDataException(javax.management.openmbean.OpenDataException) CompositeData(javax.management.openmbean.CompositeData) TabularType(javax.management.openmbean.TabularType) CompositeType(javax.management.openmbean.CompositeType)

Example 88 with OpenDataException

use of javax.management.openmbean.OpenDataException in project fabric8 by jboss-fuse.

the class OpenTypeGenerator method fromCompositeData.

private static Object fromCompositeData(ClassLoader classLoader, CompositeData cdata) throws OpenDataException {
    if (cdata == null)
        return null;
    Object result;
    CompositeType ctype = cdata.getCompositeType();
    String typeName = ctype.getTypeName();
    if (typeName.startsWith("java.util.Map")) {
        Object openKey = cdata.get("key");
        Object openVal = cdata.get("value");
        OpenType<?> keyType = ctype.getType("key");
        OpenType<?> valType = ctype.getType("value");
        Object key = fromOpenData(keyType, classLoader, openKey);
        Object value = fromOpenData(valType, classLoader, openVal);
        result = Collections.singletonMap(key, value);
    } else {
        Class<?> targetType;
        try {
            targetType = classLoader.loadClass(typeName);
        } catch (ClassNotFoundException ex) {
            OpenDataException odex = new OpenDataException("Cannot load target type: " + typeName);
            odex.initCause(ex);
            throw odex;
        }
        Constructor<?> ctor = null;
        boolean isDefaultCtor = false;
        for (Constructor<?> aux : targetType.getConstructors()) {
            isDefaultCtor = aux.getParameterTypes().length == 0;
            if (isDefaultCtor) {
                ctor = aux;
                break;
            } else if (aux.getAnnotation(ConstructorProperties.class) != null) {
                ctor = aux;
            }
        }
        IllegalStateAssertion.assertNotNull(ctor, "Cannot mxbean compliant constructor for: " + targetType.getName());
        try {
            if (isDefaultCtor) {
                result = ctor.newInstance((Object[]) null);
                for (String key : ctype.keySet()) {
                    OpenType<?> itemType = ctype.getType(key);
                    Object itemValue = cdata.get(key);
                    Object javaValue = fromOpenData(itemType, classLoader, itemValue);
                    invokeSetter(result, key, javaValue);
                }
            } else {
                List<Object> params = new ArrayList<>();
                ConstructorProperties props = ctor.getAnnotation(ConstructorProperties.class);
                for (String key : props.value()) {
                    OpenType<?> itemType = ctype.getType(key);
                    Object itemValue = cdata.get(key);
                    Object javaValue = fromOpenData(itemType, classLoader, itemValue);
                    params.add(javaValue);
                }
                Class<?>[] paramTypes = ctor.getParameterTypes();
                for (Object param : params) {
                    int index = params.indexOf(param);
                    Class<?> paramType = paramTypes[index];
                    param = toTargetType(paramType, param);
                    if (param != params.get(index)) {
                        params.set(index, param);
                    }
                }
                result = ctor.newInstance(params.toArray());
            }
        } catch (Exception ex) {
            OpenDataException odex = new OpenDataException("Cannot construct object from: " + cdata);
            odex.initCause(ex);
            throw odex;
        }
    }
    return result;
}
Also used : ConstructorProperties(java.beans.ConstructorProperties) ArrayList(java.util.ArrayList) OpenDataException(javax.management.openmbean.OpenDataException) OpenDataException(javax.management.openmbean.OpenDataException) CompositeType(javax.management.openmbean.CompositeType)

Example 89 with OpenDataException

use of javax.management.openmbean.OpenDataException in project fabric8 by jboss-fuse.

the class OpenTypeGenerator method getterValue.

private static Object getterValue(Object bean, OpenType<?> itemType, String itemName) throws OpenDataException {
    try {
        Method method = null;
        Class<? extends Object> beanClass = bean.getClass();
        String prefix = itemType == SimpleType.BOOLEAN ? "is" : "get";
        String methodName = prefix + itemName.substring(0, 1).toUpperCase() + itemName.substring(1);
        for (Method aux : beanClass.getMethods()) {
            if (methodName.equals(aux.getName()) && aux.getParameterTypes().length == 0) {
                method = aux;
                break;
            }
        }
        IllegalStateAssertion.assertNotNull(method, "Cannot find getter: " + beanClass.getName() + "." + methodName);
        return method.invoke(bean, (Object[]) null);
    } catch (Exception ex) {
        OpenDataException odex = new OpenDataException("Cannot invoke getter for: " + itemName);
        odex.initCause(ex);
        throw odex;
    }
}
Also used : OpenDataException(javax.management.openmbean.OpenDataException) Method(java.lang.reflect.Method) OpenDataException(javax.management.openmbean.OpenDataException)

Example 90 with OpenDataException

use of javax.management.openmbean.OpenDataException in project spf4j by zolyfarkas.

the class DefaultMXBeanMappingFactoryOpenTypeMapper method getMXBeanMappingInternal.

/**
 * returns MXBeanMapping or null if type is not mappable to a OpenType.
 */
@Nullable
private synchronized JMXBeanMapping getMXBeanMappingInternal(final Type type) throws NotSerializableException {
    try {
        MXBeanMapping mapping = MXBeanMappingFactory.DEFAULT.mappingForType(type, new MXBeanMappingFactory() {

            @Override
            public MXBeanMapping mappingForType(final Type t, final MXBeanMappingFactory f) throws OpenDataException {
                try {
                    return MXBeanMappings.convert(appenderMap.get(t).get(t));
                } catch (NotSerializableException ex) {
                    OpenDataException tex = new OpenDataException(t + " is not serializable");
                    tex.initCause(ex);
                    throw tex;
                }
            }
        });
        return MXBeanMappings.convert(mapping);
    } catch (OpenDataException ex) {
        return null;
    }
}
Also used : Type(java.lang.reflect.Type) NotSerializableException(java.io.NotSerializableException) MXBeanMappingFactory(com.sun.jmx.mbeanserver.MXBeanMappingFactory) OpenDataException(javax.management.openmbean.OpenDataException) MXBeanMapping(com.sun.jmx.mbeanserver.MXBeanMapping) JMXBeanMapping(org.spf4j.jmx.JMXBeanMapping) Nullable(javax.annotation.Nullable)

Aggregations

OpenDataException (javax.management.openmbean.OpenDataException)139 CompositeType (javax.management.openmbean.CompositeType)94 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)67 CompositeData (javax.management.openmbean.CompositeData)54 OpenType (javax.management.openmbean.OpenType)52 TabularType (javax.management.openmbean.TabularType)33 TabularDataSupport (javax.management.openmbean.TabularDataSupport)32 HashMap (java.util.HashMap)14 Map (java.util.Map)12 TabularData (javax.management.openmbean.TabularData)11 ObjectName (javax.management.ObjectName)8 ArrayType (javax.management.openmbean.ArrayType)8 Type (java.lang.reflect.Type)5 ArrayList (java.util.ArrayList)5 Method (java.lang.reflect.Method)4 Comparator (java.util.Comparator)4 MBeanServer (javax.management.MBeanServer)4 SimpleType (javax.management.openmbean.SimpleType)4 IOException (java.io.IOException)3 InvalidObjectException (java.io.InvalidObjectException)3