@Property(description="Number of unicast receiver threads, all reading from the same DatagramSocket. " + "If de-serialization is slow, increasing the number of receiver threads might yield better performance.") public int getUcastReceiverThreads() { return unicast_receiver_threads; }
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)); } } } }
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; }
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 ; }
String dependsClause = annotation.dependsUpon() ; if (dependsClause.trim().isEmpty()) continue ; throw new IllegalArgumentException("@Property annotation " + annotation.name() + " has an unresolved dependsUpon property: " + token) ;
public static void setDefaultValues(List<Protocol> protocols, StackType ip_version) throws Exception { InetAddress default_ip_address=Util.getNonLoopbackAddress(); if(default_ip_address == null) { log.warn(Util.getMessage("OnlyLoopbackFound"), ip_version); default_ip_address=Util.getLocalhost(ip_version); String protocolName=protocol.getName(); String defaultValue=ip_version == StackType.IPv4? annotation.defaultValueIPv4() : annotation.defaultValueIPv6(); if(defaultValue != null && !defaultValue.isEmpty()) { log.debug("set property " + protocolName + "." + fields[j].getName() + " to default value " + converted);
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()); 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=Util.methodNameToAttributeName(methodName); if(!atts.containsKey(attr_name)) { 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() + ")"); if(log.isWarnEnabled()) log.warn("getter for \"" + attr_name + "\" is already defined (new method=" + method.getName() + ")");
for(Field field: fields) { if(field.isAnnotationPresent(ManagedAttribute.class) || (field.isAnnotationPresent(Property.class) && field.getAnnotation(Property.class).exposeAsManagedAttribute())) { 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(); log.warn("Could not retrieve value of attribute (field) " + attr_name, e); for(Method method: methods) { if(method.isAnnotationPresent(ManagedAttribute.class) || (method.isAnnotationPresent(Property.class) && method.getAnnotation(Property.class).exposeAsManagedAttribute())) { 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); try { Object value=method.invoke(obj); String attributeName=Util.methodNameToAttributeName(method_name); if(value instanceof Double) value=String.format("%.2f", (double)value); log.warn("Could not retrieve value of attribute (method) " + method_name,e);
@Property(description="Max times to block for the listed messages sizes (Message.getLength()). Example: \"1000:10,5000:30,10000:500\"") public void setMaxBlockTimes(String str) { if(str == null) return; Long prev_key=null, prev_val=null; List<String> vals=Util.parseCommaDelimitedStrings(str); if(max_block_times == null) max_block_times=new TreeMap<>(); for(String tmp: vals) { int index=tmp.indexOf(':'); if(index == -1) throw new IllegalArgumentException("element '" + tmp + "' is missing a ':' separator"); Long key=Long.parseLong(tmp.substring(0, index).trim()); Long val=Long.parseLong(tmp.substring(index +1).trim()); // sanity checks: if(key < 0 || val < 0) throw new IllegalArgumentException("keys and values must be >= 0"); if(prev_key != null && key <= prev_key) throw new IllegalArgumentException("keys are not sorted: " + vals); prev_key=key; if(prev_val != null && val <= prev_val) throw new IllegalArgumentException("values are not sorted: " + vals); prev_val=val; max_block_times.put(key, val); } log.debug("max_block_times: %s", max_block_times); }
for(Protocol prot: list) { Protocol new_prot=prot.getClass().newInstance(); new_prot.setProtocolStack(targetStack); retval.add(new_prot); for(Field field: fields) { if(field.isAnnotationPresent(Property.class)) { Object value=Util.getField(field, prot); Util.setField(field, new_prot, value); 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) {
final String property; final Property r = field.getAnnotation(Property.class); boolean annotationRedefinesName = !r.name().isEmpty() && r.deprecatedMessage().isEmpty(); if (annotationRedefinesName) { property = r.name(); } else { property = field.getName(); documentationElement.setTextContent(r.description()); annotationElement.appendChild(documentationElement); }); if (annotation.name().length() < 1) { name = Util.methodNameToAttributeName(method.getName()); } else { name = annotation.name(); complexType.appendChild(attributeElement); String desc = annotation.description(); if (!desc.isEmpty()) { Element annotationElement = xmldoc.createElement("xs:annotation"); documentationElement.setTextContent(annotation.description()); annotationElement.appendChild(documentationElement);
@Property(description="The fully qualified name of a class implementing MembershipChangePolicy.") public void setMembershipChangePolicy(String classname) { try { membership_change_policy=(MembershipChangePolicy)Util.loadClass(classname, getClass()).newInstance(); } catch(Throwable e) { throw new IllegalArgumentException("membership_change_policy could not be created", e); } }
for(Field field: fields) { if(field.isAnnotationPresent(Property.class)) { Object value=Util.getField(field, prot); if(value != null) { annotation=field.getAnnotation(Property.class); Class<?> conv_class=annotation.converter(); PropertyConverter conv=null; try { 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); if(value != null) { Class<?> conv_class=annotation.converter(); PropertyConverter conv=null; try {
String property = field.getName(); Property annotation = field.getAnnotation(Property.class); String desc = annotation.description(); nameToDescription.put(property, desc); String desc = annotation.description(); String name = annotation.name(); if (name.length() < 1) { name = Util.methodNameToAttributeName(method.getName());
@Property(description="A comma-separated list of GossipRouter hosts, e.g. HostA[12001],HostB[12001]") public void setGossipRouterHosts(String hosts) throws UnknownHostException { gossip_router_hosts.clear(); // if we get passed value of List<SocketAddress>#toString() we have to strip [] if (hosts.startsWith("[") && hosts.endsWith("]")) { hosts = hosts.substring(1, hosts.length() - 1); } gossip_router_hosts.addAll(Util.parseCommaDelimitedHosts2(hosts, 1)); }
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 ; }
@Override public String getName() { if (this.accessible.isAnnotationPresent(ManagedAttribute.class)) { String name = this.accessible.getAnnotation(ManagedAttribute.class).name(); if (!name.isEmpty()) return name; } if (this.accessible.isAnnotationPresent(Property.class)) { String name = this.accessible.getAnnotation(Property.class).name(); if (!name.isEmpty()) return name; } return null; }
@Override public String getDescription() { if (this.accessible.isAnnotationPresent(ManagedAttribute.class)) { return this.accessible.getAnnotation(ManagedAttribute.class).description(); } if (this.accessible.isAnnotationPresent(Property.class)) { return this.accessible.getAnnotation(Property.class).description(); } return this.accessible.toString(); }
private static boolean isManagedAttribute(AccessibleObject object) { return object.isAnnotationPresent(ManagedAttribute.class) || (object.isAnnotationPresent(Property.class) && object.getAnnotation(Property.class).exposeAsManagedAttribute()); } }