use of org.jgroups.annotations.Property in project JGroups by belaban.
the class XMLSchemaGenerator method createXMLTree.
private static Element createXMLTree(final Document xmldoc, Class<?> clazz, String pkgname) throws Exception {
Element classElement = xmldoc.createElement("xs:element");
String elementName = pkgname + "." + clazz.getSimpleName();
if (elementName.isEmpty()) {
throw new IllegalArgumentException("Cannot create empty attribute name for element xs:element, class is " + clazz);
}
elementName = elementName.replace(PROT_PACKAGE + ".", "");
classElement.setAttribute("name", elementName);
final Element complexType = xmldoc.createElement("xs:complexType");
classElement.appendChild(complexType);
// the protocol has its own subtree
XmlElement el = Util.getAnnotation(clazz, XmlElement.class);
if (el != null) {
Element choice = xmldoc.createElement("xs:choice");
choice.setAttribute("minOccurs", "0");
choice.setAttribute("maxOccurs", "unbounded");
complexType.appendChild(choice);
Element tmp = xmldoc.createElement("xs:element");
tmp.setAttribute("name", el.name());
tmp.setAttribute("type", el.type());
choice.appendChild(tmp);
}
Map<String, DelayingElementWriter> sortedElements = new TreeMap<>();
XmlAttribute xml_attr = Util.getAnnotation(clazz, XmlAttribute.class);
if (xml_attr != null) {
String[] attrs = xml_attr.attrs();
if (attrs.length > 0) {
// to weed out dupes
Set<String> set = new HashSet<>(Arrays.asList(attrs));
for (final String attr : set) {
sortedElements.put(attr, () -> {
Element attributeElement = xmldoc.createElement("xs:attribute");
attributeElement.setAttribute("name", attr);
attributeElement.setAttribute("type", "xs:string");
complexType.appendChild(attributeElement);
});
}
}
}
// iterate fields
for (Class<?> clazzInLoop = clazz; clazzInLoop != null; clazzInLoop = clazzInLoop.getSuperclass()) {
Field[] fields = clazzInLoop.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Property.class)) {
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();
}
if (property == null || property.isEmpty()) {
throw new IllegalArgumentException("Cannot create empty attribute name for element xs:attribute, field is " + field);
}
sortedElements.put(property, () -> {
Element attributeElement = xmldoc.createElement("xs:attribute");
attributeElement.setAttribute("name", property);
// Agreement with Bela Ban on Jan-20-2009 (Go Obama!!!) to treat all types as
// xs:string since we do not know where users are going to use
// replacement tokens in configuration files. Therefore, the type becomes
// indeterminate.
attributeElement.setAttribute("type", "xs:string");
complexType.appendChild(attributeElement);
Element annotationElement = xmldoc.createElement("xs:annotation");
attributeElement.appendChild(annotationElement);
Element documentationElement = xmldoc.createElement("xs:documentation");
documentationElement.setTextContent(r.description());
annotationElement.appendChild(documentationElement);
});
}
}
}
// iterate methods
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(Property.class)) {
final Property annotation = method.getAnnotation(Property.class);
final String name;
if (annotation.name().length() < 1) {
name = Util.methodNameToAttributeName(method.getName());
} else {
name = annotation.name();
}
sortedElements.put(name, () -> {
Element attributeElement = xmldoc.createElement("xs:attribute");
attributeElement.setAttribute("name", name);
attributeElement.setAttribute("type", "xs:string");
complexType.appendChild(attributeElement);
String desc = annotation.description();
if (!desc.isEmpty()) {
Element annotationElement = xmldoc.createElement("xs:annotation");
attributeElement.appendChild(annotationElement);
Element documentationElement = xmldoc.createElement("xs:documentation");
documentationElement.setTextContent(annotation.description());
annotationElement.appendChild(documentationElement);
}
});
}
}
// write out ordered and duplicates weeded out elements
for (Map.Entry<String, DelayingElementWriter> entry : sortedElements.entrySet()) {
entry.getValue().writeElement();
}
return classElement;
}
use of org.jgroups.annotations.Property in project JGroups by belaban.
the class Configurator method setDefaultValues.
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);
}
for (Protocol protocol : protocols) {
String protocolName = protocol.getName();
// traverse class hierarchy and find all annotated fields and add them to the list if annotated
Field[] fields = Util.getAllDeclaredFieldsWithAnnotations(protocol.getClass(), Property.class);
for (int j = 0; j < fields.length; j++) {
// get the default value for the field - check for InetAddress types
if (InetAddressInfo.isInetAddressRelated(fields[j])) {
Object propertyValue = getValueFromProtocol(protocol, fields[j]);
if (propertyValue == null) {
// add to collection of @Properties with no user specified value
Property annotation = fields[j].getAnnotation(Property.class);
String defaultValue = ip_version == StackType.IPv4 ? annotation.defaultValueIPv4() : annotation.defaultValueIPv6();
if (defaultValue != null && !defaultValue.isEmpty()) {
// condition for invoking converter
Object converted = null;
try {
if (defaultValue.equalsIgnoreCase(Global.NON_LOOPBACK_ADDRESS))
converted = default_ip_address;
else
converted = PropertyHelper.getConvertedValue(protocol, fields[j], defaultValue, true);
if (converted != null)
Util.setField(fields[j], protocol, converted);
} catch (Exception e) {
throw new Exception("default could not be assigned for field " + fields[j].getName() + " in " + protocolName + " with default value " + defaultValue, e);
}
log.debug("set property " + protocolName + "." + fields[j].getName() + " to default value " + converted);
}
}
}
}
}
}
use of org.jgroups.annotations.Property in project JGroups by belaban.
the class Configurator method createInetAddressMap.
/*
* A method which does the following:
* - discovers all Fields or Methods within the protocol stack which set
* InetAddress, IpAddress, InetSocketAddress (and Lists of such) for which the user *has*
* specified a default value.
* - stores the resulting set of Fields and Methods in a map of the form:
* Protocol -> Property -> InetAddressInfo
* where InetAddressInfo instances encapsulate the InetAddress related information
* of the Fields and Methods.
*/
public static Map<String, Map<String, InetAddressInfo>> createInetAddressMap(List<ProtocolConfiguration> protocol_configs, List<Protocol> protocols) throws Exception {
// Map protocol -> Map<String, InetAddressInfo>, where the latter is protocol specific
Map<String, Map<String, InetAddressInfo>> inetAddressMap = new HashMap<>();
// collect InetAddressInfo
for (int i = 0; i < protocol_configs.size(); i++) {
ProtocolConfiguration protocol_config = protocol_configs.get(i);
Protocol protocol = protocols.get(i);
String protocolName = protocol.getName();
// regenerate the Properties which were destroyed during basic property processing
Map<String, String> properties = new HashMap<>(protocol_config.getProperties());
// check which InetAddress-related properties are ***non-null ***, and
// create an InetAddressInfo structure for them
// Method[] methods=protocol.getClass().getMethods();
Method[] methods = Util.getAllDeclaredMethodsWithAnnotations(protocol.getClass(), Property.class);
for (int j = 0; j < methods.length; j++) {
if (methods[j].isAnnotationPresent(Property.class) && isSetPropertyMethod(methods[j], protocol.getClass())) {
String propertyName = PropertyHelper.getPropertyName(methods[j]);
String propertyValue = properties.get(propertyName);
// if there is a systemProperty attribute defined in the annotation, set the property value from the system property
String tmp = grabSystemProp(methods[j].getAnnotation(Property.class));
if (tmp != null)
propertyValue = tmp;
if (propertyValue != null && InetAddressInfo.isInetAddressRelated(methods[j])) {
Object converted = null;
try {
converted = PropertyHelper.getConvertedValue(protocol, methods[j], properties, propertyValue, false);
} catch (Exception e) {
throw new Exception("String value could not be converted for method " + propertyName + " in " + protocolName + " with default value " + propertyValue + ".Exception is " + e, e);
}
InetAddressInfo inetinfo = new InetAddressInfo(protocol, methods[j], properties, propertyValue, converted);
Map<String, InetAddressInfo> m = inetAddressMap.computeIfAbsent(protocolName, k -> new HashMap<>());
m.put(propertyName, inetinfo);
}
}
}
// traverse class hierarchy and find all annotated fields and add them to the list if annotated
for (Class<?> clazz = protocol.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
Field[] fields = clazz.getDeclaredFields();
for (int j = 0; j < fields.length; j++) {
if (fields[j].isAnnotationPresent(Property.class)) {
String propertyName = PropertyHelper.getPropertyName(fields[j], properties);
String propertyValue = properties.get(propertyName);
// if there is a systemProperty attribute defined in the annotation, set the property value from the system property
String tmp = grabSystemProp(fields[j].getAnnotation(Property.class));
if (tmp != null)
propertyValue = tmp;
if ((propertyValue != null || !PropertyHelper.usesDefaultConverter(fields[j])) && InetAddressInfo.isInetAddressRelated(fields[j])) {
Object converted = null;
try {
converted = PropertyHelper.getConvertedValue(protocol, fields[j], properties, propertyValue, false);
} catch (Exception e) {
throw new Exception("String value could not be converted for method " + propertyName + " in " + protocolName + " with default value " + propertyValue + ".Exception is " + e, e);
}
InetAddressInfo inetinfo = new InetAddressInfo(protocol, fields[j], properties, propertyValue, converted);
Map<String, InetAddressInfo> m = inetAddressMap.computeIfAbsent(protocolName, k -> new HashMap<>());
m.put(propertyName, inetinfo);
}
// recompute
}
}
}
}
return inetAddressMap;
}
use of org.jgroups.annotations.Property in project JGroups by belaban.
the class Protocol method setValue.
public <T extends Protocol> T setValue(String name, Object value) {
if (name == null || value == null)
return (T) this;
Field field = Util.getField(getClass(), name);
if (field == null)
throw new IllegalArgumentException("field " + name + " not found");
Property prop = field.getAnnotation(Property.class);
if (prop != null) {
String deprecated_msg = prop.deprecatedMessage();
if (deprecated_msg != null && !deprecated_msg.isEmpty())
log.warn("Field " + getName() + "." + name + " is deprecated: " + deprecated_msg);
}
Util.setField(field, this, value);
return (T) this;
}
use of org.jgroups.annotations.Property in project JGroups by belaban.
the class ProtocolStack method getProps.
private static Map<String, String> getProps(Protocol prot) {
Map<String, String> retval = new HashMap<>();
for (Class<?> clazz = prot.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
// copy all fields marked with @Property
Field[] fields = clazz.getDeclaredFields();
Property annotation;
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 {
conv = (PropertyConverter) conv_class.newInstance();
} catch (Exception e) {
}
String tmp = conv != null ? conv.toString(value) : value.toString();
retval.put(field.getName(), tmp);
}
}
}
// 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)) {
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 {
conv = (PropertyConverter) conv_class.newInstance();
} catch (Exception e) {
}
String tmp = conv != null ? conv.toString(value) : value.toString();
retval.put(field.getName(), tmp);
}
}
}
}
}
return retval;
}
Aggregations