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