use of org.jgroups.annotations.Property in project JGroups by belaban.
the class PropertyHelper method getConvertedValue.
public static Object getConvertedValue(Object obj, Field field, Map<String, String> props, String prop, boolean check_scope) throws Exception {
if (obj == null)
throw new IllegalArgumentException("Cannot get converted value: Object is null");
if (field == null)
throw new IllegalArgumentException("Cannot get converted value: Field is null");
if (props == null)
throw new IllegalArgumentException("Cannot get converted value: Properties is null");
Property annotation = field.getAnnotation(Property.class);
if (annotation == null) {
throw new IllegalArgumentException("Cannot get property name for field " + field.getName() + " which is not annotated with @Property");
}
String propertyName = getPropertyName(field, props);
String name = obj instanceof Protocol ? ((Protocol) obj).getName() : obj.getClass().getName();
PropertyConverter propertyConverter = (PropertyConverter) annotation.converter().newInstance();
if (propertyConverter == null) {
throw new Exception("Could not find property converter for field " + propertyName + " in " + name);
}
Object converted = null;
try {
String tmp = obj instanceof Protocol ? ((Protocol) obj).getName() + "." + propertyName : propertyName;
converted = propertyConverter.convert(obj, field.getType(), tmp, prop, check_scope);
} catch (Exception e) {
throw new Exception("Conversion of " + propertyName + " in " + name + " with original property value " + prop + " failed", e);
}
return converted;
}
use of org.jgroups.annotations.Property in project JGroups by belaban.
the class PropertyHelper method getConvertedValue.
public static Object getConvertedValue(Object obj, Method method, Map<String, String> props, String prop, boolean check_scope) throws Exception {
if (obj == null) {
throw new IllegalArgumentException("Cannot get converted value: Object is null");
}
if (method == null) {
throw new IllegalArgumentException("Cannot get converted value: Method is null");
}
if (!Configurator.isSetPropertyMethod(method, obj.getClass())) {
throw new IllegalArgumentException("Cannot get converted value: Method is not set property method");
}
if (props == null) {
throw new IllegalArgumentException("Cannot get converted value: Properties is null");
}
Property annotation = method.getAnnotation(Property.class);
if (annotation == null) {
throw new IllegalArgumentException("Cannot get property name for method " + method.getName() + " which is not annotated with @Property");
}
String propertyName = getPropertyName(method);
String name = obj instanceof Protocol ? ((Protocol) obj).getName() : obj.getClass().getName();
PropertyConverter propertyConverter = (PropertyConverter) annotation.converter().newInstance();
if (propertyConverter == null) {
throw new Exception("Could not find property converter for method " + propertyName + " in " + name);
}
Object converted = null;
try {
String tmp = obj instanceof Protocol ? ((Protocol) obj).getName() + "." + propertyName : propertyName;
converted = propertyConverter.convert(obj, method.getParameterTypes()[0], tmp, prop, check_scope);
} catch (Exception e) {
throw new Exception("Conversion of " + propertyName + " in " + name + " with original property value " + prop + " failed. Exception is " + e, e);
}
return converted;
}
use of org.jgroups.annotations.Property in project JGroups by belaban.
the class PropertyHelper method getPropertyName.
public static String getPropertyName(Method method) throws IllegalArgumentException {
if (method == null) {
throw new IllegalArgumentException("Cannot get property name: field is null");
}
Property annotation = method.getAnnotation(Property.class);
if (annotation == null) {
throw new IllegalArgumentException("Cannot get property name for method " + method.getName() + " which is not annotated with @Property");
}
String propertyName = !annotation.name().isEmpty() ? annotation.name() : method.getName();
propertyName = Util.methodNameToAttributeName(propertyName);
return propertyName;
}
use of org.jgroups.annotations.Property in project JGroups by belaban.
the class ResourceDMBean method exposeManagedAttribute.
protected void exposeManagedAttribute(Method method, Object instance) {
String methodName = method.getName();
ManagedAttribute attr_annotation = method.getAnnotation(ManagedAttribute.class);
Property prop = method.getAnnotation(Property.class);
boolean expose_prop = prop != null && prop.exposeAsManagedAttribute();
boolean expose = attr_annotation != null || expose_prop;
if (!expose)
return;
boolean writable = (prop != null && prop.writable()) || (attr_annotation != null && attr_annotation.writable());
// Is name of @ManagedAttributed or @Property used?
String attr_name = attr_annotation != null ? attr_annotation.name() : prop != null ? prop.name() : null;
if (attr_name != null && !attr_name.trim().isEmpty())
attr_name = attr_name.trim();
else {
// getFooBar() --> foo_bar
attr_name = Util.methodNameToAttributeName(methodName);
if (!atts.containsKey(attr_name)) {
// hmm, maybe we need to look for an attribute fooBar
String tmp = Util.methodNameToJavaAttributeName(methodName);
if (atts.containsKey(tmp))
attr_name = tmp;
}
}
String descr = attr_annotation != null ? attr_annotation.description() : prop != null ? prop.description() : null;
AttributeEntry attr = atts.get(attr_name);
if (attr != null) {
if (isSetMethod(method)) {
if (attr.setter != null) {
if (log.isWarnEnabled())
log.warn("setter for \"" + attr_name + "\" is already defined (new method=" + method.getName() + ")");
} else
attr.setter = new MethodAccessor(method, instance);
} else {
if (attr.getter != null) {
if (log.isWarnEnabled())
log.warn("getter for \"" + attr_name + "\" is already defined (new method=" + method.getName() + ")");
} else
attr.getter = new MethodAccessor(method, instance);
}
} else {
// create a new entry in atts
boolean is_setter = isSetMethod(method);
String type = is_setter ? method.getParameterTypes()[0].getCanonicalName() : method.getReturnType().getCanonicalName();
MBeanAttributeInfo info = new MBeanAttributeInfo(attr_name, type, descr, true, writable, methodName.startsWith("is"));
AttributeEntry entry = new AttributeEntry(Util.methodNameToAttributeName(methodName), info);
if (is_setter)
entry.setter(new MethodAccessor(method, instance));
else
entry.getter(new MethodAccessor(method, instance));
atts.put(attr_name, entry);
}
}
use of org.jgroups.annotations.Property in project JGroups by belaban.
the class ResourceDMBean method findFields.
protected void findFields(Object instance) {
// traverse class hierarchy and find all annotated fields
for (Class<?> clazz = instance.getClass(); clazz != null && clazz != Object.class; clazz = clazz.getSuperclass()) {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
ManagedAttribute attr = field.getAnnotation(ManagedAttribute.class);
Property prop = field.getAnnotation(Property.class);
boolean expose_prop = prop != null && prop.exposeAsManagedAttribute();
boolean expose = attr != null || expose_prop;
if (expose) {
String fieldName = attr != null ? attr.name() : (prop != null ? prop.name() : null);
if (fieldName != null && fieldName.trim().isEmpty())
fieldName = field.getName();
String descr = attr != null ? attr.description() : prop.description();
boolean writable = attr != null ? attr.writable() : prop.writable();
MBeanAttributeInfo info = new MBeanAttributeInfo(fieldName, field.getType().getCanonicalName(), descr, true, !Modifier.isFinal(field.getModifiers()) && writable, false);
atts.put(fieldName, new AttributeEntry(field.getName(), info));
}
}
}
}
Aggregations