Search in sources :

Example 1 with JMXBeanMapping

use of org.spf4j.jmx.JMXBeanMapping in project spf4j by zolyfarkas.

the class SpecificRecordOpenTypeMapping method fromSpecificRecord.

private CompositeData fromSpecificRecord(final SpecificRecordBase r) throws OpenDataException {
    CompositeType ctype;
    try {
        ctype = typeFromSpecificRecord(r, typeMapper);
    } catch (NotSerializableException ex) {
        // this should never happen
        throw new UncheckedIOException(ex);
    }
    Schema schema = r.getSchema();
    List<Schema.Field> fields = schema.getFields();
    int size = fields.size();
    String[] names = new String[size];
    Object[] values = new Object[size];
    for (Schema.Field field : fields) {
        int pos = field.pos();
        names[pos] = field.name();
        Object val = r.get(pos);
        JMXBeanMapping mapping;
        try {
            mapping = typeMapper.get(getGenericType(field.schema()));
        } catch (NotSerializableException ex) {
            // this should never happen
            throw new UncheckedIOException(ex);
        }
        values[pos] = mapping.toOpenValue(val);
    }
    return new CompositeDataSupport(ctype, names, values);
}
Also used : Schema(org.apache.avro.Schema) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) UncheckedIOException(java.io.UncheckedIOException) JMXBeanMapping(org.spf4j.jmx.JMXBeanMapping) NotSerializableException(java.io.NotSerializableException) CompositeType(javax.management.openmbean.CompositeType)

Example 2 with JMXBeanMapping

use of org.spf4j.jmx.JMXBeanMapping in project spf4j by zolyfarkas.

the class MapEntryOpenTypeMapping method typeFromMapEntry.

private static CompositeType typeFromMapEntry(final ParameterizedType type, final JMXBeanMappingSupplier typeMapper) throws NotSerializableException {
    Type[] typeArgs = type.getActualTypeArguments();
    String[] descriptions = new String[2];
    OpenType<?>[] types = new OpenType<?>[2];
    descriptions[0] = "key of " + type;
    Type typeArg0 = typeArgs[0];
    JMXBeanMapping km = typeMapper.get(typeArg0);
    if (km == null) {
        throw new IllegalArgumentException("Cannot map to open type " + typeArg0);
    }
    types[0] = km.getOpenType();
    descriptions[1] = "value of " + type;
    Type typeArg1 = typeArgs[1];
    JMXBeanMapping km2 = typeMapper.get(typeArg1);
    if (km2 == null) {
        throw new IllegalArgumentException("Cannot map to open type " + typeArg1);
    }
    types[1] = km2.getOpenType();
    try {
        return new CompositeType(type.getTypeName(), type.toString(), NAMES, descriptions, types);
    } catch (OpenDataException ex) {
        throw new IllegalArgumentException(ex);
    }
}
Also used : JMXBeanMapping(org.spf4j.jmx.JMXBeanMapping) CompositeType(javax.management.openmbean.CompositeType) OpenType(javax.management.openmbean.OpenType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) OpenType(javax.management.openmbean.OpenType) OpenDataException(javax.management.openmbean.OpenDataException) CompositeType(javax.management.openmbean.CompositeType)

Example 3 with JMXBeanMapping

use of org.spf4j.jmx.JMXBeanMapping in project spf4j by zolyfarkas.

the class Spf4jJMXBeanMapping method defaultHandler.

@SuppressFBWarnings("LEST_LOST_EXCEPTION_STACK_TRACE")
public static JMXBeanMapping defaultHandler(final Type javaType, final JMXBeanMappingSupplier mappings) throws NotSerializableException {
    // falling back to MXBeanMappingFactory.DEFAULT
    try {
        MXBeanMapping mapping = MXBeanMappingFactory.DEFAULT.mappingForType(javaType, new MXBeanMappingFactory() {

            @Override
            public MXBeanMapping mappingForType(final Type t, final MXBeanMappingFactory f) throws OpenDataException {
                JMXBeanMapping m;
                try {
                    m = mappings.get(t);
                } catch (NotSerializableException ex) {
                    OpenDataException tex = new OpenDataException(t + " is not seriablizable ");
                    tex.initCause(ex);
                    throw tex;
                }
                if (m != null) {
                    return MXBeanMappings.convert(m);
                }
                return MXBeanMappingFactory.DEFAULT.mappingForType(t, f);
            }
        });
        return MXBeanMappings.convert(mapping);
    } catch (OpenDataException ex) {
        NotSerializableException nsex = Throwables.first(ex, NotSerializableException.class);
        if (nsex != null) {
            throw nsex;
        } else {
            throw new IllegalArgumentException("No type mapping for " + javaType, ex);
        }
    }
}
Also used : JMXBeanMapping(org.spf4j.jmx.JMXBeanMapping) GenericArrayType(java.lang.reflect.GenericArrayType) ArrayType(javax.management.openmbean.ArrayType) CompositeType(javax.management.openmbean.CompositeType) OpenType(javax.management.openmbean.OpenType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TabularType(javax.management.openmbean.TabularType) 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) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 4 with JMXBeanMapping

use of org.spf4j.jmx.JMXBeanMapping 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)

Example 5 with JMXBeanMapping

use of org.spf4j.jmx.JMXBeanMapping in project spf4j by zolyfarkas.

the class Spf4jOpenTypeMapper method get.

@Override
@Nullable
public JMXBeanMapping get(final Type t) throws NotSerializableException {
    Set<Type> ip = IN_PROGRESS.get();
    if (ip.contains(t)) {
        LOG.log(Level.FINE, "No openType mapping for {0} recursive data structure", t);
        return null;
    }
    try {
        ip.add(t);
        JMXBeanMapping val = cache.get(t);
        if (val == JMXBeanMapping.NOMAPPING) {
            return null;
        } else {
            return val;
        }
    } catch (NotSerializableException ex) {
        TypeToken<?> tt = TypeToken.of(t);
        if (tt.isSubtypeOf(Serializable.class) || tt.getRawType().isInterface()) {
            LOG.log(Level.FINE, "No mapping for type {0}", t);
            LOG.log(Level.FINEST, "Exception Detail", ex);
            return null;
        } else {
            throw ex;
        }
    } catch (RuntimeException ex) {
        TypeToken<?> tt = TypeToken.of(t);
        if (tt.isSubtypeOf(Serializable.class) || tt.getRawType().isInterface()) {
            LOG.log(Level.FINE, "No mapping for type {0}", t);
            LOG.log(Level.FINEST, "Exception Detail", ex);
            return null;
        } else {
            NotSerializableException nsex = new NotSerializableException("Type " + t + "  must be serializable");
            nsex.addSuppressed(ex);
            throw nsex;
        }
    } finally {
        ip.remove(t);
    }
}
Also used : JMXBeanMapping(org.spf4j.jmx.JMXBeanMapping) GenericArrayType(java.lang.reflect.GenericArrayType) OpenType(javax.management.openmbean.OpenType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ArrayMXBeanType(org.spf4j.jmx.mappers.Spf4jJMXBeanMapping.ArrayMXBeanType) NotSerializableException(java.io.NotSerializableException) TypeToken(com.google.common.reflect.TypeToken) Nullable(javax.annotation.Nullable)

Aggregations

JMXBeanMapping (org.spf4j.jmx.JMXBeanMapping)5 NotSerializableException (java.io.NotSerializableException)4 Type (java.lang.reflect.Type)4 ParameterizedType (java.lang.reflect.ParameterizedType)3 CompositeType (javax.management.openmbean.CompositeType)3 OpenDataException (javax.management.openmbean.OpenDataException)3 OpenType (javax.management.openmbean.OpenType)3 MXBeanMapping (com.sun.jmx.mbeanserver.MXBeanMapping)2 MXBeanMappingFactory (com.sun.jmx.mbeanserver.MXBeanMappingFactory)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 Nullable (javax.annotation.Nullable)2 TypeToken (com.google.common.reflect.TypeToken)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 UncheckedIOException (java.io.UncheckedIOException)1 ArrayType (javax.management.openmbean.ArrayType)1 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)1 TabularType (javax.management.openmbean.TabularType)1 Schema (org.apache.avro.Schema)1 ArrayMXBeanType (org.spf4j.jmx.mappers.Spf4jJMXBeanMapping.ArrayMXBeanType)1