use of org.jgroups.annotations.Property in project JGroups by belaban.
the class ProtocolStack method copyProtocols.
public List<Protocol> copyProtocols(ProtocolStack targetStack) throws IllegalAccessException, InstantiationException {
List<Protocol> list = getProtocols();
List<Protocol> retval = new ArrayList<>(list.size());
for (Protocol prot : list) {
Protocol new_prot = prot.getClass().newInstance();
new_prot.setProtocolStack(targetStack);
retval.add(new_prot);
for (Class<?> clazz = prot.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
// copy all fields marked with @Property
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Property.class)) {
Object value = Util.getField(field, prot);
Util.setField(field, new_prot, value);
}
}
// copy all setters marked with @Property
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
String methodName = method.getName();
if (method.isAnnotationPresent(Property.class) && Configurator.isSetPropertyMethod(method, clazz)) {
Property annotation = method.getAnnotation(Property.class);
List<String> possible_names = new LinkedList<>();
if (annotation.name() != null)
possible_names.add(annotation.name());
possible_names.add(Util.methodNameToAttributeName(methodName));
Field field = Util.findField(prot, possible_names);
if (field != null) {
Object value = Util.getField(field, prot);
Util.setField(field, new_prot, value);
}
}
}
}
}
return retval;
}
use of org.jgroups.annotations.Property in project JGroups by belaban.
the class PropertyHelper method getPropertyName.
public static String getPropertyName(Field field, Map<String, String> props) throws IllegalArgumentException {
if (field == null) {
throw new IllegalArgumentException("Cannot get property name: field is null");
}
if (props == null) {
throw new IllegalArgumentException("Cannot get property name: properties map 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 = field.getName();
if (props.containsKey(annotation.name())) {
propertyName = annotation.name();
boolean isDeprecated = !annotation.deprecatedMessage().isEmpty();
if (isDeprecated)
log.warn(Util.getMessage("Deprecated"), propertyName, annotation.deprecatedMessage());
}
return propertyName;
}
use of org.jgroups.annotations.Property in project JGroups by belaban.
the class PropertyHelper method getConvertedValue.
public static Object getConvertedValue(Object obj, Field field, String value, 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");
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 = field.getName();
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, value, check_scope);
} catch (Exception e) {
throw new Exception("Conversion of " + propertyName + " in " + name + " with original property value " + value + " failed", e);
}
return converted;
}
use of org.jgroups.annotations.Property in project JGroups by belaban.
the class ResourceDMBean method dumpStats.
public static void dumpStats(Object obj, final Map<String, Object> map, Log log) {
for (Class<?> clazz = obj.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(ManagedAttribute.class) || (field.isAnnotationPresent(Property.class) && field.getAnnotation(Property.class).exposeAsManagedAttribute())) {
ManagedAttribute attr_annotation = field.getAnnotation(ManagedAttribute.class);
Property prop = field.getAnnotation(Property.class);
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
attr_name = field.getName();
try {
field.setAccessible(true);
Object value = field.get(obj);
map.put(attr_name, value != null ? value.toString() : null);
} catch (Exception e) {
log.warn("Could not retrieve value of attribute (field) " + attr_name, e);
}
}
}
Method[] methods = obj.getClass().getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(ManagedAttribute.class) || (method.isAnnotationPresent(Property.class) && method.getAnnotation(Property.class).exposeAsManagedAttribute())) {
ManagedAttribute attr_annotation = method.getAnnotation(ManagedAttribute.class);
Property prop = method.getAnnotation(Property.class);
String method_name = attr_annotation != null ? attr_annotation.name() : prop != null ? prop.name() : null;
if (method_name != null && !method_name.trim().isEmpty())
method_name = method_name.trim();
else {
String field_name = Util.methodNameToAttributeName(method.getName());
method_name = Util.attributeNameToMethodName(field_name);
}
if (ResourceDMBean.isGetMethod(method)) {
try {
Object value = method.invoke(obj);
String attributeName = Util.methodNameToAttributeName(method_name);
if (value instanceof Double)
value = String.format("%.2f", (double) value);
map.put(attributeName, value != null ? value.toString() : null);
} catch (Exception e) {
log.warn("Could not retrieve value of attribute (method) " + method_name, e);
}
}
}
}
}
}
use of org.jgroups.annotations.Property in project JGroups by belaban.
the class FixedMembershipToken method setMemberList.
@Property(name = "fixed_members_value")
public void setMemberList(String list) throws UnknownHostException {
memberList.clear();
StringTokenizer memberListTokenizer = new StringTokenizer(list, fixed_members_seperator);
while (memberListTokenizer.hasMoreTokens()) {
String tmp = memberListTokenizer.nextToken().trim();
int index = tmp.lastIndexOf('/');
int port = index != -1 ? Integer.parseInt(tmp.substring(index + 1)) : 0;
String addr_str = index != -1 ? tmp.substring(0, index) : tmp;
InetAddress addr = InetAddress.getByName(addr_str);
memberList.add(new InetSocketAddress(addr, port));
}
}
Aggregations