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);
}
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);
}
}
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);
}
}
}
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;
}
}
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);
}
}
Aggregations